websocket 消息不同步 || websocket message out of sync
davidheartdev opened this issue · comments
1、文字频道发送消息不能同步,只能通过刷新
下图为手机发送消息
电脑端没有同步接收到消息,但是开发者模式看,是收到了消息没有更新到页面上。
- The message sent by the text channel cannot be synchronized, only by refreshing
The picture below shows the mobile phone sending a message
The computer did not receive the message synchronously, but in the developer mode, the message was received but not updated to the page.
这是因为之前的一个feature修改了消息同步策略,本人发送的消息只会在发送端出现在屏幕上。
我再想想怎么优化一下
This is because a previous feature modified the message synchronization strategy, and the message sent by me will only appear on the screen at the sending end.
Let me think about how to optimize
最好是本人不同端也能同步。
我把这应用到团队开发工作当中去,相应的工作开相应的房间。
还想要房间加锁功能。
前几年做过国外项目,就是discord来管理不同工作,非常方便。
这个项目已经很接近了,把这个用起来。
有时间我也来贡献代码。
It is best that I can synchronize different terminals.
I apply this to team development work, and open corresponding rooms for corresponding work.
Also want the room lock function.
I have done foreign projects a few years ago, and it is very convenient to use discord to manage different tasks.
This project is close, put this to use.
I will also contribute code when I have time.
`const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('message', (message) => {
console.log(Received message: ${message}
);
// 向所有连接的客户端广播消息
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
ws.on('close', () => {
console.log('Client disconnected');
});
});
客户端代码(浏览器):
```javascript
const ws = new WebSocket('ws://localhost:8080');
ws.onopen = () => {
console.log('Connected to server');
};
ws.onmessage = (event) => {
const message = event.data;
console.log(`Received message: ${message}`);
// 在此处处理接收到的消息
};
ws.onclose = () => {
console.log('Disconnected from server');
};
// 发送消息到服务器
ws.send('Hello, server!');`
这样似乎可以实现websocket多端同步
This seems to be able to achieve websocket multi-terminal synchronization
在nightly 应该已经解决这个问题了,辛苦看一下
This problem should have been solved in nightly, take a hard look