Last active
August 26, 2016 06:03
-
-
Save thirdknife/6e9f8c5a3cb80c5369f644141bd3eaa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class Communication { | |
constructor(url) { | |
if(!instance){ | |
instance = this; | |
} | |
this.url = url; | |
this.connection = null; | |
this.status = false; | |
this.listeners = new Array(); | |
return instance; | |
} | |
init () { | |
this.connection = new WebSocket(this.url); | |
this.connection.onopen = function(evt) { onOpen(evt) }; | |
this.connection.onclose = function(evt) { onClose(evt) }; | |
this.connection.onmessage = function(evt) { onMessage(evt) }; | |
this.connection.onerror = function(evt) { onError(evt) }; | |
} | |
subscribe (type , callback){ | |
register = { | |
type : type, | |
callback : callback | |
} | |
this.listeners.push(register); | |
return; | |
} | |
sendMessage(msg){ | |
this.connection.send(JSON.stringify(msg)); | |
} | |
onOpen (){ | |
console.log('connected!'); | |
this.status = true; | |
} | |
onMessage (evt){ | |
console.log(evt.data); | |
switch (evt.data.type){ | |
case WatcherTypes.NDO_UPDATE: | |
this.listeners.filter(function (listener) { | |
if(listener.type == WatcherTypes.NDO_UPDATE){ | |
listener.callback(); | |
}; | |
}); | |
break; | |
case WatcherTypes.MESH_GROUP: | |
this.listeners.filter(function (listener) { | |
if(listener.type == WatcherTypes.MESH_GROUP){ | |
listener.callback(); | |
}; | |
}); | |
break; | |
case WatcherTypes.COLOR_SELECTION: | |
this.listeners.filter(function (listener) { | |
if(listener.type == WatcherTypes.MESH_GROUP){ | |
listener.callback(); | |
}; | |
}); | |
break; | |
default: | |
break; | |
} | |
} | |
onClose (){ | |
this.status = false; | |
} | |
onError () { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment