在实际项目中是第一次收到包含中文消息时连接中断,重连后正常,为了提问我把相关代码提取出来发现前端一收到含中文消息就不断报错重连了
server
import http from 'http';
import socket from 'socket.io';
const server = http.createServer();
const io = socket(server);
io.on('connection', (client) => {
console.log('SOCKET->connection:' + client.id);
client.on('message', async function (message) {
console.log(message);
client.send({message: 'success'})
});
client.on('disconnect', async function () {
console.log('SOCKET->disconnect:' + client.id);
});
});
server.listen(3001);
console.log('server running http://localhost:3001');
client
import io from 'socket.io-client';
const socket = io('ws://localhost:3001');
socket.on('connect', () => {
console.log('connect');
socket.send({message: 'hello'})
});
socket.on('disconnect', () => {
console.log('disconnect')
});
socket.on('error', err => {
console.log(err)
});
socket.on('message', res => {
console.log(res)
});
此时是英文消息是正常的
可是一旦把服务器的
client.send({message: 'success'})
替换成包含中文的
client.send({message: '你好'})
立刻就崩溃了,前端不断的报错重连
socket.io
socket.io-client
都已升级到 2.1.1
有大神知道什么原因吗
相关分类