Last active
February 10, 2017 20:06
-
-
Save SeanChristopherConway/c76447e85328e860a2af 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
var socket_lib = require("./websocket"); | |
var socket_connector = new socket_lib(); | |
const qs = require("qs"); | |
/* | |
Current socket server socket.io library is 0.9.16 | |
*/ | |
/* | |
Connection parameters | |
*/ | |
Test server example to connect to the organization 221 room: | |
https://zayftirouter-staging.herokuapp.com/socket.io/1/?token=8dde2e7eb6c4e6971af49dbe9e93c892115111d8&&rooms%5B0%5D=221 | |
/* | |
A room is defined as org_id[project_id][site_id][user_id]/[asset_id][form_id] | |
Eg.To all members of an org: | |
rooms: ["001"] //numeric id needs to be stringified | |
Eg.To all members of an org project: | |
rooms: ["0011"] //numeric id needs to be stringified | |
Eg.To a specific org user: | |
rooms: ["001001234"] //numeric id needs to be stringified | |
*/ | |
const rooms_to_join = { | |
"rooms": ["001", "001001234"] //the org_id does not have to be a string in JS but the concatenated versions do of course, please do concatenation on socket clients | |
}; //any sub organization rooms need to have the org_id concatenated in front of them, e.g. project 1 in org 1 would be "11" | |
const token = "[enter access token here]"; | |
const uri = "wss://[enter your url here]/chat?token=" + token + "&&" + qs.stringify(rooms_to_join); | |
var connect_to_socket = socket_connector.getConnectedSocket(uri, rooms_to_join, token); | |
socket_connector.on('ready', function (socket) { | |
console.log("Socket is authenticated and ready for messaging"); | |
socket.emit("msg", { | |
"processing": false, | |
"type": "checkin", | |
"rooms": ["001"] //message will only be sent to this specific org | |
}); | |
socket.emit("msg", { | |
"processing": false, | |
"type": "somethingelse", | |
"data" : {"abcd":["a","b","c","d"]}, //this is an example key value pair you can sent containing a JSON object | |
"rooms": ["001001234"] //message will only be sent to this specific user, add in user organization, project id, etc | |
}); | |
socket.emit("msg", { | |
"processing": true, //processing to be done on rules-engine | |
"type": "event_reporting", //this field is required if processing is true | |
"data" :{...}//required fields defined by rules-engine, TODO: make a confluence document of these fields | |
"rooms": ["001"] //message will only be sent to this specific user, add in user organization, project id, etc | |
}); | |
}); |
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
var socketAvailable = false; | |
var events = require("events"), | |
util = require("util"), | |
io = require('socket.io-client', { | |
reconnect: true | |
}), | |
socket; | |
console.log("Starting socketStuff"); | |
function websocket() { | |
events.call(this); | |
} | |
util.inherits(websocket, events); | |
websocket.prototype.getConnectedSocket = function (uri, roomstojoin, token) { | |
const self = this; | |
socket = io.connect(uri); | |
socket.on('connect', function () { | |
console.log("You are connected"); | |
}); | |
socket.on('authorized', function () { | |
self.emit('ready', socket); | |
}); | |
socket.on('unauthorized', function () { | |
socketAvailable = false; | |
console.log("You do not have valid authorization. Please try logging out and logging back in."); | |
}); | |
/* | |
Attempt to reconnect upon disconnection. | |
*/ | |
socket.on('disconnect', function () { | |
console.log('disconnected!'); | |
socketAvailable = false; | |
socket = io.connect(uri); | |
}); | |
/* | |
Receive a 'msg' event from the server. | |
*/ | |
socket.on('msg', function (data) { | |
if (data) { | |
//Here is where you can manipulate the JSON object returned from the socket server | |
console.log("Message received is: %s", JSON.stringify(data)); | |
} else { | |
console.log("Message received is empty: so it is %s", JSON.stringify(data)); | |
} | |
}); | |
/* | |
When the server disconnects a user, the server sends out a 'reconnect' message, | |
and the client attempts reconnection upon receipt of this message. | |
*/ | |
socket.on('reconnect', function () { | |
console.log('Trying to reconnect after receiving message from server'); | |
socket = io.connect(uri); | |
}); | |
}; | |
websocket.prototype.isSocketAvailable = function () { | |
return socketAvailable; | |
}; | |
websocket.prototype.addUser = function () { | |
if (socketAvailable) { | |
this.webSocket() | |
.emit("add-user", protocolObj); | |
} | |
}; | |
module.exports = websocket; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment