diff --git a/index.js b/index.js index c26b840..8dc770f 100644 --- a/index.js +++ b/index.js @@ -2,15 +2,70 @@ const WebSocket = require('ws'); const config = require('./config'); const http = require('http'); -var server = http.createServer(); +var wss = null; -const wss = new WebSocket.Server( +var server = http.createServer( + + function(req, res) { + + res.setHeader('Content-Type', 'application/json'); + + wss.clients.forEach( + function(client){ + if (client.readyState === WebSocket.OPEN) { + var cache = []; + + res.write( + + JSON.stringify(client, + + function(key, value) { + + + if (typeof value === 'object' && value !== null) { + if (cache.indexOf(value) !== -1) { + // Duplicate reference found + try { + // If this value does not reference a parent it can be deduped + return JSON.parse(JSON.stringify(value)); + } catch (error) { + // discard key if value cannot be deduped + return; + } + } + // Store value in our collection + cache.push(value); + } + return value; + } + + ) + ); + + cache = null; + } + } + ); + + + + + + res.end(); + } + +); + + +wss = new WebSocket.Server( { server : server, perMessageDeflate: false } ); + + server.on( 'listening', function() {