From 8238d4ed24d643c85d3654246a8f2f3ab5bf710a Mon Sep 17 00:00:00 2001 From: Remco Date: Sat, 14 Jan 2017 19:06:27 +0100 Subject: [PATCH] First revision of connection logic --- .eslintrc.json | 8 ++++++ Bezerk.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 29 ++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 .eslintrc.json create mode 100644 Bezerk.js create mode 100644 package.json diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..dc65063 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,8 @@ +{ + "extends": "standard", + "installedESLint": true, + "plugins": [ + "standard", + "promise" + ] +} \ No newline at end of file diff --git a/Bezerk.js b/Bezerk.js new file mode 100644 index 0000000..58b25e9 --- /dev/null +++ b/Bezerk.js @@ -0,0 +1,66 @@ +const Debug = require('debug') +const Logger = new Debug('bezerk') +const WSS = require('ws').Server +const Config = require('./config.json') + +let shards = [] +let receivers = [] + +const BezerkWS = new WSS({ + port: Config.port +}) + +BezerkWS.on('connection', (socket) => { + Logger('New websocket.') + socket.send(JSON.stringify({ + op: 'HELLO' + })) + socket.on('message', (msg) => process(socket, msg)) +}) + +function process (socket, message) { + Logger('Attempting to process a message.') + var msg + try { + JSON.parse(message) + } catch (e) { + socket.close() + Logger('Closing socket, invalid data received.') + } + if (!msg.op) { + socket.close() + Logger('Closing socket, no OP code received.') + return + } + if (msg.op === 'IDENTIFY_SHARD') { + Logger('A socket is trying to connect as a shard.') + if (!msg.c) { + socket.close() + Logger('Closing socket, no sharding info recieved.') + return + } else { + if (!Array.isArray(msg.c)) { + socket.close() + Logger('Closing connection, invalid sharding info') + return + } // We're assuming only wildbeast is going to connect to bezerk as a shard, so we are not going to check for valid data + socket.shardInfo = msg.c + shards.push(socket) + socket.send(JSON.stringify({ + op: 'OK' + })) + } + } else if (msg.op === 'IDENTIFY_LISTENER') { + Logger('A socket is trying to connect as a listener') + if (!msg.c) { + socket.close() + Logger('Closing socket, no subscriptions.') + } else { + socket.subscriptions = msg.c + receivers.push(socket) + socket.send(JSON.stringify({ + op: 'OK' + })) + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c5170b1 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "bezerk", + "version": "1.0.0", + "description": "WildBeast websocket mmanager", + "main": "Bezerk.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/TheSharks/Bezerk.git" + }, + "author": "Remco Jongschaap ", + "license": "GPL-3.0", + "bugs": { + "url": "https://github.com/TheSharks/Bezerk/issues" + }, + "homepage": "https://github.com/TheSharks/Bezerk#readme", + "dependencies": { + "debug": "^2.6.0", + "ws": "^1.1.1" + }, + "devDependencies": { + "eslint": "^3.13.1", + "eslint-config-standard": "^6.2.1", + "eslint-plugin-promise": "^3.4.0", + "eslint-plugin-standard": "^2.0.1" + } +}