socket.io 自动断开socket [英] socket.io automatically disconnects socket

查看:294
本文介绍了socket.io 自动断开socket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在电子中运行 socket.io 时,我在开发人员控制台中不断收到此错误.

I keep getting this error in the developer console when I run socket.io in electron.

engine.io-client:socket probe transport "websocket" failed because of error: transport closed +6s

当我建立它时,套接字总是立即断开连接.我的客户甚至没有得到 socket.emit 我一建立连接就做,直到一两分钟后.

When I establish it the socket always disconnects right away. My client doesn't even get the socket.emit I do as soon as the connection is established until after a minute or two.

客户端和服务器使用相同版本的 socket.io.

The client and server are on the same version of socket.io.

服务器:

io.on('connection', function(socket){
  socket.emit('got it','');
  console.log('Socket connection established');
});

客户:

var socket = require('socket.io-client')('http://localhost:3000');
console.log(socket);
socket.on('got it', function(a){
  console.log('connected');
  console.log(socket);
})

这不是完整的代码,但除了一些不应该影响连接的其他侦听器之外,没有其他任何东西可以与 websockets 交互.

This isn't the complete code but there's nothing else that interacts with websockets except some other listeners which shouldn't affect the connection.

服务器没有显示任何问题.每次有人连接时它都会告诉我,但是当我尝试以任何一种方式发出任何内容时,收件人都不会立即得到它.每次单击发送发射的按钮时,我都会看到发射带有 ack id x 的数据包",但服务器看不到它.这个问题是间歇性的,好像我稍等一下,服务器和客户端将能够连接,但一段时间后它们仍然会随机断开连接.

The server isn't showing any issues. It tells me every time someone connects but when I try to emit anything either way the recipient won't get it right away. I see "emitting packet with ack id x" every time I click the button that sends an emit, but the server doesn't see it. This issue is intermittent as if I wait a bit the server and client will be able to connect, but they still disconnect randomly after some time.

推荐答案

我通过添加解决了这个问题

I fixed this by adding

{transports: ['websocket'], upgrade: false}

作为实例化客户端时的选项.

as options when instantiating the client.

来自https://stackoverflow.com/a/28240802/4726265

这篇关于socket.io 自动断开socket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆