Discuss / JavaScript / app.js有个很困惑的替代的地方

app.js有个很困惑的替代的地方

Topic source

function onMessage(message) { console.log(message); if (message && message.trim()) { let msg = createMessage('chat', this.user, message.trim()); //this.wss.broadcast(msg); this.send(msg); } }

这里 this.wss.broadcast(msg); 替换为 this.send(msg);

消息就会值刷新单个页面的

function onConnect() { let user = this.user; let msg = createMessage('join', user, ${user.name} joined.); this.wss.broadcast(msg); // build user list: let users = this.wss.clients.map(function (client) { return client.user; }); //this.wss.broadcast(createMessage('list', user, users)); this.send(createMessage('list', user, users)); }

这里 his.wss.broadcast(createMessage('list', user, users)); 替换为 this.send(createMessage('list', user, users));

消息就会刷新所有页面的,两句完全相等


  • 1

Reply