You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
838 B
33 lines
838 B
const WebSocket = require('ws');
|
|
const UrlParser = require('url');
|
|
const uuid = require('node-uuid');
|
|
const messenger = require('./messenger');
|
|
|
|
const jsDir = `${__dirname}/server`;
|
|
|
|
const RicochetApp = require(`${jsDir}/ricochet.js`);
|
|
|
|
const clientApps = {
|
|
'/ricochet': RicochetApp.new({ messenger })
|
|
};
|
|
|
|
const onConnect = (ws, req) => {
|
|
// Store an ID on the socket connection.
|
|
ws.id = uuid.v4();
|
|
|
|
const url = UrlParser.parse(req.url, true);
|
|
|
|
if (clientApps[url.pathname]) {
|
|
app.onConnect(ws, req);
|
|
|
|
ws.on('message', app.onMessage);
|
|
ws.on('close', app.onDisconnect);
|
|
}
|
|
};
|
|
|
|
//===== 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");
|
|
|
|
|