From 7155b370a1747dca696d0b5391127ca478cd9155 Mon Sep 17 00:00:00 2001 From: Jacqui Manzi Date: Sat, 3 Dec 2016 23:14:11 -0800 Subject: [PATCH 1/2] ENG - Switching out websockets with persisten TCP connection. --- controllers/TwitchController.js | 4 +- src/connections/ConnectionManager.js | 79 +++++++++++----------------- src/server.js | 11 +++- 3 files changed, 43 insertions(+), 51 deletions(-) diff --git a/controllers/TwitchController.js b/controllers/TwitchController.js index 15bb75e..ea65742 100644 --- a/controllers/TwitchController.js +++ b/controllers/TwitchController.js @@ -64,7 +64,9 @@ function registerMessageListener() { if (server.cm.socketConnections.length) { console.log(message); - server.cm.socketConnections[0].sendUTF(message); + server.cm.socketConnections.forEach((socket) => { + socket.write(message); + }); } return StreamParser.parseTwitchContent(userstate, message); diff --git a/src/connections/ConnectionManager.js b/src/connections/ConnectionManager.js index 68747cc..42628ff 100644 --- a/src/connections/ConnectionManager.js +++ b/src/connections/ConnectionManager.js @@ -1,68 +1,49 @@ /* eslint new-cap: 0 */ /* eslint padded-blocks: 0 */ /* eslint brace-style: 0 */ -const WebSocketServer = require('websocket').server; const logger = require('winston'); +const net = require('net'); /** * ConnectionManager - Web Sockets client manager. * - * @param server * @constructor */ -function ConnectionManager(server) { - - this.Server = new WebSocketServer({ - httpServer: server, - autoAcceptConnections: false - }); +function ConnectionManager() { this.socketConnections = []; - this.setupListeners(); + // Create a TCP socket listener + this.Server = net.Server(connectionListeners.bind(this)); + this.Server.listen(8000); + console.log('System waiting at http://localhost:8000'); } -ConnectionManager.prototype.setupListeners = function setupListeners() { - this.Server.on('request', request => { - - if (!originIsAllowed(request.origin)) { - // Make sure we only accept requests from an allowed origin - request.reject(); - logger.log('info', (new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); - return; +function connectionListeners(socket) { + + this.socketConnections.push(socket); + logger.log('info', new Date() + ' Connection accepted: ' + socket.remoteAddress + ":" + socket.remotePort); + + // 'data' is an event that means that a message was just sent by the + // client application + socket.on('data', function (msg_sent) { + console.log('hit here'); + // Loop through all of our sockets and send the data + for (var i = 0; i < this.socketConnections.length; i++) { + // Don't send the data back to the original sender + if (this.socketConnections[i] == socket) // don't send the message to yourself + continue; + // Write the msg sent by chat client + this.socketConnections[i].write(msg_sent); } - - const connection = request.accept(undefined, request.origin); - this.socketConnections.push(connection); - - logger.log('info', new Date() + ' Connection accepted.'); - connection.on('message', message => { - if (message.type === 'utf8') { - connection.sendUTF(message.utf8Data); - } - else if (message.type === 'binary') { - connection.sendBytes(message.binaryData); - } - }); - - connection.on('close', () => { - - const index = this.socketConnections.indexOf(connection); - if (index > -1) { - this.socketConnections.splice(index, 1); - } - - logger.log('info', (new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); - }); - }); - -}; - -function originIsAllowed(origin) { - // @TODO: Set the origin of the web socket and verify here - // return config.get('allowedOrigins').indexOf(origin) !== -1; - //return origin !== undefined; - return true; + }.bind(this)); + // Use splice to get rid of the socket that is ending. + // The 'end' event means tcp client has disconnected. + socket.on('end', function () { + var i = this.socketConnections.indexOf(socket); + this.socketConnections.splice(i, 1); + logger.log('info', (new Date()) + ' Peer ' + socket.remoteAddress + ' disconnected.'); + }.bind(this)); } module.exports = ConnectionManager; diff --git a/src/server.js b/src/server.js index 7025d62..30e79d6 100644 --- a/src/server.js +++ b/src/server.js @@ -38,7 +38,16 @@ const server = http.listen(config.get('port'), config.get('ip'),() => { Twitch.setupConnection(config.get('twitch').events); + +}).on('error', function(err) { + logger.log('info', 'Server error: ' + err + ' ' + new Date()); + console.log(err); +}); + +http.on('exit', function () { + logger.log('info', 'Server is exiting. ' + new Date()); + console.log('exiting...'); }); -const connectionManager = new ConnectionManager(server); +const connectionManager = new ConnectionManager(); module.exports.cm = connectionManager; From 666d42cf251a2c64ceed30e293f3049ffec2b167 Mon Sep 17 00:00:00 2001 From: Jacqui Manzi Date: Sun, 4 Dec 2016 22:43:26 -0800 Subject: [PATCH 2/2] ENG - Working prototype tcp socket messages. --- controllers/TwitchController.js | 2 +- package.json | 3 ++- src/connections/ConnectionManager.js | 33 ++++++++++++---------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/controllers/TwitchController.js b/controllers/TwitchController.js index ea65742..c3623cb 100644 --- a/controllers/TwitchController.js +++ b/controllers/TwitchController.js @@ -65,7 +65,7 @@ function registerMessageListener() { if (server.cm.socketConnections.length) { console.log(message); server.cm.socketConnections.forEach((socket) => { - socket.write(message); + socket.sendEndMessage({message: message}); }); } diff --git a/package.json b/package.json index c5dd999..1c4a790 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "mysql" :"^2.12.0", "websocket": "^1.0.23", "winston": "^2.2.0", - "socket.io": "^1.4.5" + "socket.io": "^1.4.5", + "json-socket": "^0.2.0" } } diff --git a/src/connections/ConnectionManager.js b/src/connections/ConnectionManager.js index 42628ff..6c2519c 100644 --- a/src/connections/ConnectionManager.js +++ b/src/connections/ConnectionManager.js @@ -3,6 +3,8 @@ /* eslint brace-style: 0 */ const logger = require('winston'); const net = require('net'); +const JsonSocket = require('json-socket'); +const config = require('../../config/config'); /** * ConnectionManager - Web Sockets client manager. @@ -16,34 +18,27 @@ function ConnectionManager() { // Create a TCP socket listener this.Server = net.Server(connectionListeners.bind(this)); this.Server.listen(8000); - console.log('System waiting at http://localhost:8000'); + + logger.log('info', 'Unity socket TCP connection waiting at: ' + config.get('ip') + ':8000'); } function connectionListeners(socket) { + socket = new JsonSocket(socket); this.socketConnections.push(socket); logger.log('info', new Date() + ' Connection accepted: ' + socket.remoteAddress + ":" + socket.remotePort); - // 'data' is an event that means that a message was just sent by the - // client application socket.on('data', function (msg_sent) { - console.log('hit here'); - // Loop through all of our sockets and send the data - for (var i = 0; i < this.socketConnections.length; i++) { - // Don't send the data back to the original sender - if (this.socketConnections[i] == socket) // don't send the message to yourself - continue; - // Write the msg sent by chat client - this.socketConnections[i].write(msg_sent); - } - }.bind(this)); - // Use splice to get rid of the socket that is ending. - // The 'end' event means tcp client has disconnected. + //console.log(msg_sent); + }.bind(this) + ); + socket.on('end', function () { - var i = this.socketConnections.indexOf(socket); - this.socketConnections.splice(i, 1); - logger.log('info', (new Date()) + ' Peer ' + socket.remoteAddress + ' disconnected.'); - }.bind(this)); + const i = this.socketConnections.indexOf(socket); + this.socketConnections.splice(i, 1); + logger.log('info', (new Date()) + ' Peer ' + socket.remoteAddress + ' disconnected.'); + }.bind(this) + ); } module.exports = ConnectionManager;