Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will handle all stuff to do with websockets
- Loading branch information
Showing
2 changed files
with
31 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
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,27 @@ | ||
const log = require("simple-console-logger").getLogger("LiveMap Sockets"); | ||
const WS = require("ws"); | ||
|
||
const SocketController = (wss, access) => { | ||
wss.broadcast = function broadcast(data) { | ||
wss.clients.forEach(function each(client) { | ||
if (client.readyState === WS.OPEN) { | ||
client.send(data); | ||
} | ||
}); | ||
}; | ||
|
||
wss.on("connection", function connection(ws, req) { //ws, res | ||
log.trace("Access is: %s", access); | ||
if (access !== "*") { // We don't want to accept all requests | ||
if (req.headers.origin !== access) { | ||
ws.close(); | ||
log.warn("Someone tried connecting from an invalid origin: %s", req.headers.origin); | ||
} | ||
} | ||
log.debug("connection made from", req.headers.origin); | ||
}); | ||
|
||
return {}; | ||
}; | ||
|
||
module.exports = SocketController; |