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

Commit

Permalink
QOL imporvements
Browse files Browse the repository at this point in the history
And changes specifically for Rush
  • Loading branch information
Dougley committed Apr 16, 2017
1 parent e3ead04 commit 50da1f3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
27 changes: 13 additions & 14 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}\\Bezerk.js",
"cwd": "${workspaceRoot}",
"env": {
"DEBUG": "bezerk*"
}
},

{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"processId": "${command:PickProcess}",
"port": 5858
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/Bezerk.js",
// "env": {
// "DEBUG": "bezerk*"
// }
}
]
],
"compounds": []
}
42 changes: 32 additions & 10 deletions Bezerk.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ BezerkWS.on('connection', (socket) => {
socket.on('message', (msg) => process(socket, msg))
})

setInterval(() => {
for (let socket of shards) {
if (socket.readyState !== 1) {
shards.splice(shards.indexOf(socket), 1)
send({
op: "SHARD_LEFT",
c: socket.shardInfo
})
}
}
}, 1000)

function process (socket, message) {
Logger('Attempting to process a message.')
Logger(message)
let msg
var msg
try {
msg = JSON.parse(message)
} catch (e) {
Expand All @@ -35,23 +47,23 @@ function process (socket, message) {
}
if (msg.op === 'IDENTIFY_SHARD') {
Logger('A socket is trying to connect as a shard.')
if (!msg.c) {
if (msg.c == undefined) {
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
// 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'
}))
send({
op: "SHARD_JOINED",
c: socket.shardInfo
})
}
} else if (msg.op === 'IDENTIFY_LISTENER') {
Logger('A socket is trying to connect as a listener')
Expand Down Expand Up @@ -85,13 +97,13 @@ function process (socket, message) {
for (let shard of shards) {
if (shard.shardInfo === msg.shard) {
Logger('Shard found, sending payload.')
shard.send(JSON.stringify(msg))
if (shard.readyState === 1) shard.send(JSON.stringify(msg))
}
}
} else {
Logger('Listener event did not define a shard, falling back to sending to all shards.')
for (let shard of shards) {
shard.send(JSON.stringify(msg))
if (shard.readyState === 1) shard.send(JSON.stringify(msg))
}
}
} else {
Expand All @@ -100,13 +112,14 @@ function process (socket, message) {
Logger('Closing shard connection, no event passed.')
return
}
if (!msg.c) {
if (msg.c === undefined && msg.op !== 'EVAL_REPLY') {
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.subscriptions.indexOf(msg.op) > -1 || listener.subscriptions.indexOf('*') > -1 && listener.readyState === 1) {
msg.shard = socket.shardInfo
Logger('Sending data.')
listener.send(JSON.stringify(msg))
}
Expand All @@ -115,3 +128,12 @@ function process (socket, message) {
}
}
}

function send(msg) {
for (let listener of receivers) {
if (listener.subscriptions.indexOf(msg.op) > -1 || listener.subscriptions.indexOf('*') > -1 && listener.readyState === 1) {
Logger('Sending data.')
listener.send(JSON.stringify(msg))
}
}
}

0 comments on commit 50da1f3

Please sign in to comment.