-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
28 lines (21 loc) · 698 Bytes
/
server.js
File metadata and controls
28 lines (21 loc) · 698 Bytes
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
var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var app = express();
var port = process.env.PORT || 5000;
app.use(express.static(__dirname));
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");
ws.on("close", function() {
console.log("websocket connection close");
clearInterval(id);
});
});