代码拉取完成,页面将自动刷新
const WebSocket = require('ws');
const http = require('http');
const express = require('express');
const path = require('path');
const reducers = require('./reducers');
const app = express();
app.use(express.static(path.join(__dirname, '/public')));
const server = http.createServer(app);
const wss = new WebSocket.Server({server: server});
wss.on('connection', function (ws) {
// stage1: start
console.log('start service for a client');
for (let key in reducers) {
if(reducers.hasOwnProperty(key)) {
const start = reducers[key]['start'];
start && start(ws);
}
}
// stage2: message
ws.on('message', function (message) {
try {
const req = JSON.parse(message);
console.log(req);
for (let key in reducers) {
if(reducers.hasOwnProperty(key)) {
const message = reducers[key]['message'];
message && message(req, ws);
}
}
} catch (e) {
console.log('there is a unknow message');
}
});
// stage3: close
ws.on('close', function () {
console.log('stop service for a client');
});
});
server.listen(8000, function listening() {
console.log('Listening on %d', server.address().port);
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。