Skip to content

Instantly share code, notes, and snippets.

@stevenleeg
Created July 19, 2013 14:53
Show Gist options
  • Save stevenleeg/6039684 to your computer and use it in GitHub Desktop.
Save stevenleeg/6039684 to your computer and use it in GitHub Desktop.
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