parent
a7a26a9837
commit
9b62ec7336
7 changed files with 5 additions and 108 deletions
After Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 30 KiB |
@ -1,48 +0,0 @@ |
|||||||
const WebSocket = require('ws'); |
|
||||||
const DEBUG = (process.env.NODE_ENV !== "production"); |
|
||||||
|
|
||||||
const Messenger = function() { |
|
||||||
this.clients = {}; |
|
||||||
}; |
|
||||||
|
|
||||||
Messenger.prototype.messageOne = function(ws, message) {
|
|
||||||
DEBUG && console.log(`Sending to only ${ws.id}:`); |
|
||||||
DEBUG && console.log(message); |
|
||||||
|
|
||||||
ws.send(JSON.stringify(message)); |
|
||||||
}; |
|
||||||
|
|
||||||
Messenger.prototype.messageOthers = function(ws, message) {
|
|
||||||
DEBUG && console.log(`Sending to other client(s):`); |
|
||||||
DEBUG && console.log(message); |
|
||||||
|
|
||||||
Object.values(this.clients).forEach((client) => { |
|
||||||
if (client !== ws && client.readyState === WebSocket.OPEN) { |
|
||||||
client.send(JSON.stringify(message)); |
|
||||||
} |
|
||||||
}); |
|
||||||
}; |
|
||||||
|
|
||||||
Messenger.prototype.messageAll = function(message) {
|
|
||||||
const clients = Object.values(this.clients); |
|
||||||
|
|
||||||
DEBUG && console.log(`Sending to all ${clients.length} client(s):`); |
|
||||||
DEBUG && console.log(message); |
|
||||||
|
|
||||||
clients.forEach((client) => { |
|
||||||
if (client.readyState === WebSocket.OPEN) { |
|
||||||
client.send(JSON.stringify(message)); |
|
||||||
} |
|
||||||
}); |
|
||||||
}; |
|
||||||
|
|
||||||
Messenger.prototype.subscribe = function(ws) { |
|
||||||
this.clients[ws.id] = ws; |
|
||||||
}; |
|
||||||
|
|
||||||
Messenger.prototype.unsubscribe = function(ws) { |
|
||||||
delete this.clients[ws.id]; |
|
||||||
}; |
|
||||||
|
|
||||||
|
|
||||||
module.exports = Messenger; |
|
@ -1,52 +0,0 @@ |
|||||||
const WebSocket = require('ws'); |
|
||||||
const UrlParser = require('url'); |
|
||||||
const uuid = require('node-uuid'); |
|
||||||
const Messenger = require('./messenger'); |
|
||||||
|
|
||||||
const RicochetApp = require(`${__dirname}/../server/ricochet.js`); |
|
||||||
|
|
||||||
const apps = { |
|
||||||
'/ricochet': new RicochetApp({ messenger: new Messenger() }) |
|
||||||
}; |
|
||||||
|
|
||||||
// Make sure all apps follow the messaging contract.
|
|
||||||
Object.keys(apps).forEach((key) => { |
|
||||||
const app = apps[key]; |
|
||||||
|
|
||||||
const valid = app && app.messenger && app.onConnect && app.onMessage && app.onDisconnect; |
|
||||||
|
|
||||||
if (!valid) { |
|
||||||
delete apps[key]; |
|
||||||
console.log(`==================== \n\nCRITICAL ERROR! \n\nApplication at ${key} is missing one or more: messenger, onConnect, onMessage, onDisconnect.\n\n====================\n`); |
|
||||||
} |
|
||||||
}); |
|
||||||
|
|
||||||
const onConnect = (ws, req) => { |
|
||||||
// Store an ID on the socket connection.
|
|
||||||
ws.id = uuid.v4(); |
|
||||||
|
|
||||||
const url = UrlParser.parse(req.url, true); |
|
||||||
const app = apps[url.pathname]; |
|
||||||
|
|
||||||
if (app) { |
|
||||||
app.messenger.subscribe(ws); |
|
||||||
|
|
||||||
app.onConnect(ws, req); |
|
||||||
|
|
||||||
ws.on('message', (rawBody) => { |
|
||||||
app.onMessage(ws, rawBody); |
|
||||||
}); |
|
||||||
|
|
||||||
ws.on('close', () => { |
|
||||||
app.onDisconnect(ws); |
|
||||||
app.messenger.unsubscribe(ws); |
|
||||||
}); |
|
||||||
} |
|
||||||
}; |
|
||||||
|
|
||||||
//===== Generic socket server for any application instantiated above.
|
|
||||||
const wss = new WebSocket.Server({ port: 8080 }); |
|
||||||
wss.on('connection', onConnect); |
|
||||||
|
|
||||||
console.log("Websocket server listening on :8080"); |
|
||||||
|
|
Loading…
Reference in new issue