parent
f2eca30432
commit
080643d8ac
4 changed files with 38 additions and 9 deletions
@ -1,11 +1,30 @@ |
|||||||
const WebSocket = require('ws'); |
const WebSocket = require('ws'); |
||||||
|
const uuid = require('node-uuid'); |
||||||
|
|
||||||
const wss = new WebSocket.Server({ port: 8080 }); |
const wss = new WebSocket.Server({ port: 8080 }); |
||||||
|
|
||||||
wss.on('connection', function connection(ws) { |
const DEBUG = true; |
||||||
ws.on('message', function incoming(message) { |
|
||||||
console.log('received: %s', message); |
const Server = { |
||||||
|
messageAll: (ws, message) => {
|
||||||
|
wss.clients.forEach((client) => { |
||||||
|
if (client !== ws && client.readyState === WebSocket.OPEN) { |
||||||
|
client.send(ws.id + ' ' + message); |
||||||
|
} |
||||||
}); |
}); |
||||||
|
}, |
||||||
|
|
||||||
|
onMessage: (ws, message) => { |
||||||
|
DEBUG && console.log('Received: %s', message); |
||||||
|
Server.messageAll(ws, message); |
||||||
|
}, |
||||||
|
}; |
||||||
|
|
||||||
ws.send('something'); |
wss.on('connection', function connection(ws, req) { |
||||||
|
ws.id = uuid.v4(); |
||||||
|
DEBUG && console.log(req.url + ' connected to ' + ws.id); |
||||||
|
|
||||||
|
ws.on('message', Server.onMessage.bind(null, ws)); |
||||||
}); |
}); |
||||||
|
|
||||||
|
console.log("Websocket server listening on :8080") |
Loading…
Reference in new issue