-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (37 loc) · 1.15 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
48
49
50
var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
const path = require('path')
var app = express()
var port = process.env.PORT || 5000
app.use(express.static(path.join(__dirname, 'public')))
.set('view engine', 'ejs')
.set('views', path.join(__dirname, 'views'))
.get('/', (req, res) => res.render('pages/prototype'))
var server = http.createServer(app)
server.listen(port)
console.log("http server listening on %d", port)
var wss = new WebSocketServer({server: server})
console.log("websocket server created")
wss.on("connection", function(ws) {
var id = setInterval(function() {
ws.send(JSON.stringify(new Date()), function() { })
}, 1000)
console.log("websocket connection open")
wss.broadcast = function(data, sender) {
console.log(data +": " + sender)
wss.clients.forEach(function(client) {
if (client !== sender) {
client.send(data)
}
})
}
ws.on('message', function(message) {
console.log('message recived: ' + message )
wss.broadcast(message, ws);
})
ws.on("close", function() {
console.log("websocket connection close")
clearInterval(id)
})
})