【发布时间】:2020-08-23 06:28:47
【问题描述】:
我尝试使用此链接的教程http://web-engineering.info/node/57
但是当我执行 node server.js 并打开浏览器http://localhost:3434 时,它说需要升级。 server.js 文件是:
var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 3434});
wss.broadcast = function (data) {
var i = 0, n = this.clients ? this.clients.length : 0, client = null;
for (; i < n; i++) {
client = this.clients[i];
if (client.readyState === client.OPEN) {
client.send(data);
}
else console.error('Error: the client state is ' + client.readyState);
}
};
wss.on('connection', function (ws) {
ws.on('message', function (message) {
wss.broadcast(message);
});
});
【问题讨论】: