socket.io automatically disconnects socket
Asked Answered
D

3

7

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

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.

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

Server:

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

Client:

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

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.

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.

Donner answered 6/12, 2016 at 9:12 Comment(3)
I don't think anyone would be able to answer your question at this state, as there is not enough information (e.g no code) to determine the problem. Please add further information if you would like to solve this problem.Antoineantoinetta
I edited the OP with some more data but there's not really anything unusual about the code.Donner
Possible duplicate of Socket.io 1.x: use WebSockets only?Kiva
D
19

I fixed this by adding

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

as options when instantiating the client.

Answer from https://mcmap.net/q/88616/-socket-io-1-x-use-websockets-only

Donner answered 6/12, 2016 at 10:48 Comment(2)
Thank you, this worked in my case. I have given an up vote to you.Roadblock
Hi, i added that to the client and now connection works, but my API calls stopped workingFlaherty
B
0

Change the socket.io-client version correctly.

npm i socket.io-client@v2-latest --save
Brio answered 29/4, 2021 at 6:17 Comment(0)
K
0

android:usesCleartextTraffic="true"

add this in your AndroidManifest.xml in application tag

Example:

<application
...
android:usesCleartextTraffic="true">
...
</application>
Kremer answered 4/9 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.