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 receiving and sending
Browse files Browse the repository at this point in the history
  • Loading branch information
Dougley committed Jan 14, 2017
1 parent 8238d4e commit 7aaf912
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
52 changes: 51 additions & 1 deletion Bezerk.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ BezerkWS.on('connection', (socket) => {

function process (socket, message) {
Logger('Attempting to process a message.')
var msg
let msg
try {
JSON.parse(message)
} catch (e) {
Expand All @@ -44,7 +44,9 @@ function process (socket, message) {
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
Logger('Accepted shard.')
socket.shardInfo = msg.c
socket.type = 'shard'
shards.push(socket)
socket.send(JSON.stringify({
op: 'OK'
Expand All @@ -55,12 +57,60 @@ function process (socket, message) {
if (!msg.c) {
socket.close()
Logger('Closing socket, no subscriptions.')
return
}
if (!Array.isArray(msg.c)) {
socket.close()
Logger('Closing socket, invalid subscriptions.')
} else {
Logger('Accepted listener')
socket.subscriptions = msg.c
socket.type = 'listener'
receivers.push(socket)
socket.send(JSON.stringify({
op: 'OK'
}))
}
} else {
// This is where it's going to get fun.
if (receivers.indexOf(socket) === -1 && shards.indexOf(socket) === -1) {
socket.close()
Logger('Socket tried sending events without being identified first.')
return
}
if (socket.type === 'listener') {
if (msg.shard) {
Logger('Listener event defined a shard, trying to find it and send the message.')
for (let shard of shards) {
if (shard.shardInfo[0] === msg.shard) {
Logger('Shard found, sending payload.')
shard.send(msg)
}
}
} else {
Logger('Listener event did not define a shard, falling back to sending to all shards.')
for (let shard of shards) {
shard.send(msg)
}
}
} else {
if (!msg.op) {
socket.close()
Logger('Closing shard connection, no event passed.')
return
}
if (!msg.c) {
socket.close()
Logger('Closing shard connection, no data.')
} else {
Logger('Request accepted, attempting to send data to subscribed listeners.')
for (let listener of receivers) {
if (listener.indexOf(msg.c) > -1) {
Logger('Sending data.')
listener.send(msg)
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"port": 1337
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bezerk",
"version": "1.0.0",
"description": "WildBeast websocket mmanager",
"description": "WildBeast websocket manager",
"main": "Bezerk.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit 7aaf912

Please sign in to comment.