-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
9cbe4ef
commit 9ba5072
Showing
11 changed files
with
599 additions
and
433 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
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 |
---|---|---|
@@ -1,66 +1,52 @@ | ||
const WebSocket = require('isomorphic-ws'); | ||
const WebSocket = require("isomorphic-ws"); | ||
const logger = require("./logger"); | ||
|
||
const websocket = { | ||
wss: null, | ||
init: function (server) { | ||
logger( | ||
"info", | ||
"Trying to init websocket...", | ||
"websocket", | ||
null, | ||
"" | ||
); | ||
|
||
if (!server === null) { | ||
logger( | ||
"websocket_error", | ||
"server parameter not defined.", | ||
"error", | ||
null, | ||
"" | ||
); | ||
return null | ||
} | ||
|
||
logger( | ||
"info", | ||
"Server is valid so still trying to init websocket...", | ||
"websocket", | ||
null, | ||
"" | ||
); | ||
|
||
const wss = new WebSocket.Server({ server }); | ||
websocket.wss = wss; | ||
|
||
// Broadcast to all clients | ||
wss.broadcast = function broadcast(data, isBinary) { | ||
wss.clients.forEach(client => { | ||
if (client.readyState === WebSocket.OPEN && data !== undefined) { | ||
client.send(data, { binary: isBinary }); | ||
} | ||
}); | ||
}; | ||
|
||
wss.on('connection', (ws) => { | ||
ws.on('message', (message) => { | ||
wss.broadcast(message); | ||
}); | ||
}); | ||
|
||
wss.on('close', () => { | ||
logger( | ||
"info", | ||
"Websocket disconnected...", | ||
"websocket", | ||
null, | ||
"" | ||
); | ||
websocket.wss = null | ||
}); | ||
wss: null, | ||
init: function (server) { | ||
logger("info", "Trying to init websocket...", "websocket", null, ""); | ||
|
||
if (!server === null) { | ||
logger( | ||
"websocket_error", | ||
"server parameter not defined.", | ||
"error", | ||
null, | ||
"" | ||
); | ||
return null; | ||
} | ||
} | ||
|
||
logger( | ||
"info", | ||
"Server is valid so still trying to init websocket...", | ||
"websocket", | ||
null, | ||
"" | ||
); | ||
const wss = new WebSocket.Server({ server }); | ||
websocket.wss = wss; | ||
|
||
// Broadcast to all clients | ||
wss.broadcast = function broadcast(data, isBinary) { | ||
wss.clients.forEach((client) => { | ||
if (client.readyState === WebSocket.OPEN && data !== undefined) { | ||
client.send(data, { binary: isBinary }); | ||
} | ||
}); | ||
}; | ||
|
||
wss.on("connection", (ws) => { | ||
ws.on("message", (message) => { | ||
wss.broadcast(message); | ||
}); | ||
}); | ||
|
||
wss.on("close", () => { | ||
logger("info", "Websocket disconnected...", "websocket", null, ""); | ||
websocket.wss = null; | ||
}); | ||
}, | ||
}; | ||
|
||
module.exports = { websocket }; |
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
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
Oops, something went wrong.