首页 > 解决方案 > Socket.io 传输关闭和 ping 超时错误

问题描述

由于传输关闭pingtimeout错误,套接字客户端正在断开连接。它是随机发生的。有时套接字客户端会稳定几个小时,然后开始随机断开连接。谁能帮我找到问题。

套接字客户端版本:2.1.0 套接字服务器版本:2.1.0,

客户代码

const socket = require('socket.io-client')
let url = 'http://localhost:5050'
let clientSocket = socket.connect(url, {
  reconnection: true,
  forceNew: true,
  secure: true
})
 clientSocket.on("connect", function (data) {
  // console.log(clientSocket)
  console.log("connection established");
 });
 clientSocket.on("event", function(data) {
  console.log(data)
})

服务器代码

const socketio = require('socket.io');      
this.io = socketio.listen(this.server, 
{ 
    'pingInterval': PING_INTERVAL, 
    'pingTimeout': PING_TIMEOUT 
});
this.io.on('connection', function (socket) {
    // const consumer = new ConsumerGroup(options, topic);
    // reading data from add event and sending back the same data
    console.log('Connected', socket.id);
    const token = socket.handshake.query.token;
    socket.on('disconnect', function () {
        console.log(socket.id + ' -> Disconnected');
    });

    consumer.on('ready', function (message) {
        console.log('Ready');
    });
    consumer.on('message', function (message) {
        // sending message on socket when we recieve the message from kafka\
        socket.emit('alarm', message);
    });

    consumer.on('error', function (err) {
        console.log('error', err);
    });

});

标签: socket.io

解决方案


推荐阅读