diff options
author | Matt Kohls <mattkohls13@gmail.com> | 2019-10-29 00:05:34 -0400 |
---|---|---|
committer | Matt Kohls <mattkohls13@gmail.com> | 2019-10-29 00:05:34 -0400 |
commit | c6f4969b766c10674911d6fed034cc792333dc23 (patch) | |
tree | 7ce944e0d7d3003d3496787b7095a6251cdf8993 | |
parent | ae7f2b5ef9c724a2e346e351cb9bd7906c236361 (diff) | |
download | rogue.js-c6f4969b766c10674911d6fed034cc792333dc23.tar.gz rogue.js-c6f4969b766c10674911d6fed034cc792333dc23.tar.bz2 rogue.js-c6f4969b766c10674911d6fed034cc792333dc23.zip |
Starting to add bits for the server
Really not excited about the quality of example code out in the wild to
learn from
-rw-r--r-- | package-lock.json | 5 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | rogue-server.js | 44 |
3 files changed, 50 insertions, 0 deletions
diff --git a/package-lock.json b/package-lock.json index 01460b6..49475b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,11 @@ "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" }, + "uuid": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.3.tgz", + "integrity": "sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==" + }, "ws": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.0.tgz", diff --git a/package.json b/package.json index 67d07dd..bc4c55b 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "Matt Kohls", "license": "GPL-3.0-only", "dependencies": { + "uuid": "^3.3.3", "ws": "^7.2.0" } } 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); |