Created
July 19, 2013 14:53
-
-
Save stevenleeg/6039684 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 WebSocketServer = require('websocket').server; | |
var http = require('http'); | |
var WebsocketExporter = function(stat_manager) { | |
var self = this; | |
self.stat_manager = stat_manager; | |
self.connections = []; | |
self.server = http.createServer(self.handleServerRequest); | |
self.server.listen(8080); | |
self.ws_server = new WebSocketServer({ | |
httpServer: self.server | |
}); | |
self.ws_server.on("request", function(request) { | |
console.log("Connection opened!"); | |
var connection = request.accept("strike-0.1", request.origin); | |
connection.on("message", function(message) { | |
console.log("Message from ws connection: " + message); | |
}); | |
connection.on("close", function() { | |
console.log("Connection closed"); | |
}); | |
self.connections.append(connection); | |
}); | |
self.stat_manager.on("statUpdate", function(name, data) {; | |
console.log("New stat update from " + self.handleServerRequest); | |
self.sendAll({ name: name, data: data }); | |
}); | |
}; | |
WebsocketExporter.prototype.sendAll = function(data) { | |
this.connections.forEach(function(connection) { | |
connection.sendUTF(JSON.stringify(data)); | |
}); | |
}; | |
WebsocketExporter.prototype.handleServerRequest = function(req, res) { | |
res.writeHead(404); | |
res.end(); | |
}; | |
exports.Exporter = WebsocketExporter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment