This repository has been archived by the owner on Dec 2, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "standard", | ||
"installedESLint": true, | ||
"plugins": [ | ||
"standard", | ||
"promise" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
})) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |