-
Notifications
You must be signed in to change notification settings - Fork 4
/
index-websockets.js
35 lines (27 loc) · 973 Bytes
/
index-websockets.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
const SocketServer = require('ws').Server;
var express = require('express');
var path = require('path');
var connectedUsers = [];
//init Express
var app = express();
//init Express Router
var router = express.Router();
var port = process.env.PORT || 80;
//return static page with websocket client
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/static/index-with-websockets.html'));
});
var server = app.listen(port, function () {
console.log('node.js static server listening on port: ' + port + ", with websockets listener")
})
const wss = new SocketServer({ server });
//init Websocket ws and handle incoming connect requests
wss.on('connection', function connection(ws) {
console.log("connection ...");
//on connect message
ws.on('message', function incoming(message) {
console.log('received: %s', message);
connectedUsers.push(message);
});
ws.send('message from server at: ' + new Date());
});