-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (37 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var http = require("http")
var path = require("path")
var fs = require("fs")
var wsock = require("ws")
var express = require("express")
var nunjucks = require("nunjucks")
var settings = require("./be/settings")
var app = express()
app.use('/scripts/', express.static(settings.STATIC + "/scripts/"));
nunjucks.configure('fe/templates', {
express: app,
noCache: settings.DEBUG
})
app.get('/', (req, res) => {
res.render("index.html", {
WEBSOCKETS_URL: `ws:${settings.WS_HOST}:${settings.WS_PORT}`
})
})
var httpServer = http.createServer(app);
httpServer.listen(settings.PORT, settings.IP)
var WebSocketServer = wsock.Server
var wss = new WebSocketServer({
server: httpServer,
autoAcceptConnections: false
})
let wsConnections = []
wss.on('connection', (ws) => {
wsConnections.push(ws)
ws.on('message', (message) => {
console.log('received: ' + message)
wsConnections.forEach((w) => {
if (w.readyState == w.OPEN) {
w.send(message)
}
})
})
})