-
Notifications
You must be signed in to change notification settings - Fork 0
/
wss.js
34 lines (28 loc) · 949 Bytes
/
wss.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
const assert = require("assert"),
os = require("os"),
path = require("path")
const http = require('http');
const ws = require('ws');
const express = require("express");
function makeServer(handleMessage, public_path = path.join(__dirname, 'public'), PORT = process.env.PORT || 3000) {
const app = express();
app.use(express.static(public_path));
const server = http.createServer(app)
const wss = new ws.Server({ server: server });
wss.on('connection', (socket) => {
console.log('client connected');
socket.send(JSON.stringify({ cmd:"handshake" }))
socket.on('close', () => { console.log('client disconnected'); });
socket.on('message', handleMessage)
});
server.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
function broadcast(msg) {
wss.clients.forEach(function (client) {
if (client.readyState == ws.OPEN) {
client.send( msg );
}
});
}
return broadcast
}
module.exports = makeServer