updates
This commit is contained in:
parent
94c83e9e50
commit
cc5213b09e
79 changed files with 1341 additions and 357 deletions
61
5-network/11-websocket/chat.view/server.js
Normal file
61
5-network/11-websocket/chat.view/server.js
Normal file
|
@ -0,0 +1,61 @@
|
|||
/**
|
||||
Before running:
|
||||
> npm install ws
|
||||
Then:
|
||||
> node server.js
|
||||
> open http://localhost:8080 in the browser
|
||||
*/
|
||||
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const ws = new require('ws');
|
||||
|
||||
const wss = new ws.Server({noServer: true});
|
||||
|
||||
const clients = new Set();
|
||||
|
||||
function accept(req, res) {
|
||||
|
||||
if (req.url == '/ws' && req.headers.upgrade &&
|
||||
req.headers.upgrade.toLowerCase() == 'websocket' &&
|
||||
// can be Connection: keep-alive, Upgrade
|
||||
req.headers.connection.match(/\bupgrade\b/i)) {
|
||||
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onSocketConnect);
|
||||
} else if (req.url == '/') { // index.html
|
||||
fs.createReadStream('./index.html').pipe(res);
|
||||
} else { // page not found
|
||||
res.writeHead(404);
|
||||
res.end();
|
||||
}
|
||||
}
|
||||
|
||||
function onSocketConnect(ws) {
|
||||
clients.add(ws);
|
||||
log(`new connection`);
|
||||
|
||||
ws.on('message', function(message) {
|
||||
log(`message received: ${message}`);
|
||||
|
||||
message = message.slice(0, 50); // max message length will be 50
|
||||
|
||||
for(let client of clients) {
|
||||
client.send(message);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', function() {
|
||||
log(`connection closed`);
|
||||
clients.delete(ws);
|
||||
});
|
||||
}
|
||||
|
||||
let log;
|
||||
if (!module.parent) {
|
||||
log = console.log;
|
||||
http.createServer(accept).listen(8080);
|
||||
} else {
|
||||
// to embed into javascript.info
|
||||
log = function() {};
|
||||
// log = console.log;
|
||||
exports.accept = accept;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue