Created
February 26, 2015 00:52
-
-
Save SKART1/b0635f07d1b5799880d7 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 config = require('config'); | |
var log = require('libs/log')(module); | |
var WebSocketServer = require('ws').Server; | |
/* Запус websocket-сервера */ | |
var wsServer = new WebSocketServer({ | |
host: config.get('ipAddress'), | |
port: config.get('wsServerPort') | |
}); | |
var wsArray = []; | |
function patchEmitter(emitter) { | |
var oldEmit = emitter.emit; | |
emitter.emit = function() { | |
log.info("event " + arguments[0]); | |
var emitArgs = arguments; | |
oldEmit.apply(emitter, arguments); | |
} | |
} | |
/* Обработчик установления соединения */ | |
wsServer.on('connection', function (websocket) { | |
log.info('New connection'); | |
wsArray.push(websocket); | |
/* Обработчик полученного сообщения */ | |
websocket.on('message', function (message) { | |
log.info('Received message ' + message + ' from client with id:'); | |
}); | |
/* Обработчик разрыва соединения */ | |
websocket.on('close', function (code, reason, wasClean) { | |
log.info("Buffered"+ websocket.bufferedAmount); | |
log.info('User disconnected. with code: ' + code+ ' and reason' + reason+ " wasClean" + wasClean); | |
}); | |
/* Обработчик штатного соединения */ | |
websocket.on('error', function (code) { | |
log.info('!!!!!!!!!!User error. User id:'); | |
}); | |
patchEmitter(websocket); | |
}); | |
wsServer.on('error', function (err) { | |
log.info('WEBSOCKET_SERVER ERROR:' + err); | |
}); | |
wsServer.on('close', function () { | |
log.info('WEBSOCKET_SERVER'); | |
}); | |
setInterval(function () { | |
for (var wsArrayIndex = 0; wsArrayIndex < wsArray.length; wsArrayIndex++) { | |
var date = new Date(); | |
log.info(date.toISOString() + " Websocket № " + wsArrayIndex + " state is " + wsArray[wsArrayIndex].readyState); | |
try { | |
wsArray[wsArrayIndex].send(date.toISOString(), function ack(error) { | |
if (error) { | |
log.error("Send error" + error); | |
} | |
}); | |
} | |
catch (err) { | |
if (err) { | |
log.error("Error"); | |
} | |
} | |
} | |
}, 20000); | |
log.info('Listening to ' + config.get('ipAddress') + ':' + config.get('wsServerPort') + ' ...'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment