Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Commit

Permalink
First revision of connection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dougley committed Jan 14, 2017
1 parent 6e07ea9 commit 8238d4e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "standard",
"installedESLint": true,
"plugins": [
"standard",
"promise"
]
}
66 changes: 66 additions & 0 deletions Bezerk.js
Original file line number Diff line number Diff line change
@@ -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'
}))
}
}
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <hello@dougley.com>",
"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"
}
}

0 comments on commit 8238d4e

Please sign in to comment.