From c6f4969b766c10674911d6fed034cc792333dc23 Mon Sep 17 00:00:00 2001 From: Matt Kohls Date: Tue, 29 Oct 2019 00:05:34 -0400 Subject: Starting to add bits for the server Really not excited about the quality of example code out in the wild to learn from --- rogue-server.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 rogue-server.js (limited to 'rogue-server.js') diff --git a/rogue-server.js b/rogue-server.js new file mode 100644 index 0000000..a8d553a --- /dev/null +++ b/rogue-server.js @@ -0,0 +1,44 @@ +/* + * Server for rogue.js + * + * Matt Kohls + * 2019 + * GPL3 + */ + +const http = require('http'); +const WebSocket = require('ws'); +const uuid = require('uuid/v4'); + +// Used to keep track of all the players and their connection +const players = new Map(); + +const httpServer = http.createServer(); +const wsServer = new WebSocket.Server({ noServer: true }); + +wsServer.on('connection', function connection(ws, request) { + players.set(ws, uuid()); + console.log(new Date().toUTCString() + ' | ' + players.get(ws) + ' joins'); + + ws.on('open', function join() { + // Do things on an open, doesn't seem to get hit though + }); + + ws.on('message', function message(msg) { + // Do thing with message from client + console.log(new Date().toUTCString() + ' | ' + players.get(ws) + ' says \"' + msg + '\"'); + }); + + ws.on('close', function leave() { + console.log(new Date().toUTCString() + ' | ' + players.get(ws) + ' leaves'); + clients.delete(ws); + }); +}); + +httpServer.on('upgrade', function upgrade(request, socket, head) { + wsServer.handleUpgrade(request, socket, head, function done(ws) { + wsServer.emit('connection', ws, request); + }); +}); + +httpServer.listen(8080); -- cgit v1.2.3