Skip to content

Instantly share code, notes, and snippets.

@StasKoval
Created December 22, 2014 04:37
Show Gist options
  • Save StasKoval/77e2706635134dd4f062 to your computer and use it in GitHub Desktop.
Save StasKoval/77e2706635134dd4f062 to your computer and use it in GitHub Desktop.
var roomid = moderator.channel;
var SIGNALING_SERVER = 'wss://wsnodejs.nodejitsu.com:443';
var websocket = new WebSocket(SIGNALING_SERVER);
websocket.onmessage = function (event) {
var data = JSON.parse(event.data);
if (data.isChannelPresent == false) {
moderator.open();
} else {
moderator.join(roomid);
}
};
websocket.onopen = function () {
websocket.send(JSON.stringify({
checkPresence: true,
channel: roomid
}));
};
function onMessageCallbacks(data) {
data = JSON.parse(e.data);
if (data.sender == connection.userid) return;
if (onMessageCallbacks[data.channel]) {
onMessageCallbacks[data.channel](data.message);
};
}
moderator.openSignalingChannel = function (config) {
var channel = config.channel || this.channel;
onMessageCallbacks[channel] = config.onmessage;
if (config.onopen) setTimeout(config.onopen, 1000);
return {
send: function (message) {
websocket.send(JSON.stringify({
sender: connection.userid,
channel: channel,
message: message
}));
},
channel: channel
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment