Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io client connect disconnect

Tags:

I am unable to figure out why disconnecting / connecting a socket.io connection multiple times is not working ?

Server side code:

io.on('connection', function(socket){     console.log('a user connected');     socket.on('disconnect', function(){         console.log('user disconnected');     }); }); 

Client side code:

var socket = io(); socket.on('connect', function() {     console.log("connected from the client side"); }); $('#connect_button').click(function(){     socket.connect(); }); $('#disconnect_button').click(function(){     socket.disconnect(); }); 

It disconnects alright. But doesn't reconnect. I'm using Socket.io 1.0. Please help.

like image 483
Kaya Toast Avatar asked Jul 04 '14 04:07

Kaya Toast


People also ask

Why does socket Io keep disconnecting from the server?

anything between the user and the Socket.IO server may encounter a temporary failure or be restarted the user may lose connection or switch from WiFi to 4G, in case of a mobile browser ... That being said, the Socket.IO client will always try to reconnect, unless specifically told otherwise. Possible explanations for a disconnection:

Why can't the socket Io client connect to a WebSocket server?

As explained in the "What Socket.IO is not" section, the Socket.IO client is not a WebSocket implementation and thus will not be able to establish a connection with a WebSocket server, even with transports: ["websocket"]: Please make sure the Socket.IO server is actually reachable at the given URL. You can test it with:

How do I deal with a client disconnecting from a socket?

Instructor: [00:00] To deal with a client disconnecting, we'll go into our socket server and open our io.js file. This will be where a client has actually closed their browser and therefore the socket has disconnected. [00:14] We're going to listen for an inbuilt event, specifically on the socket, called disconnect.

Can socket disconnect be used on server side?

socket.disconnect() can be used only on the client side, not on the server side. Client.emit('disconnect') triggers the disconnection event on the server, but does not effectively disconnect the client. The client is not aware of the disconnection.


2 Answers

Have you tried this config in client ?

// 0.9  socket.io version io.connect(SERVER_IP, {'force new connection': true});  // 1.0 socket.io version io.connect(SERVER_IP, {'forceNew': true}); 
like image 149
Jujuleder Avatar answered Sep 19 '22 13:09

Jujuleder


This solution, based on Jujuleder's answer works. Apparently in socket.io 1.0, it is "forceNew" instead of "force new connection" - both work though.

Server:

var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http);  app.get('/', function(req, res){     res.sendfile('s.html'); });  io.on('connection', function(socket){     console.log('a user connected: ' + socket.id);     socket.on('disconnect', function(){         console.log( socket.name + ' has disconnected from the chat.' + socket.id);     });     socket.on('join', function (name) {         socket.name = name;         console.log(socket.name + ' joined the chat.');     }); });  http.listen(3000, function(){     console.log('listening on *:3000'); }); 

Client (s.html):

<!DOCTYPE html> <html> <head lang="en">     <meta charset="UTF-8">     <title></title>     <style>button{width: 100px;}input{width: 300px;}</style> </head> <body>  <ul id="messages"></ul>  <button id="disconnect">disconnect</button> <button id="connect">connect</button>  <script src="/socket.io/socket.io.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>      var socket = io.connect('http://localhost:3000');      $('#disconnect').click(function(){         socket.disconnect();     });     $('#connect').click(function(){ //        socket.socket.reconnect(); //        socket = io.connect('http://localhost:3000',{'force new connection':true });         socket = io.connect('http://localhost:3000',{'forceNew':true });         socket.on('connect', function(msg){             socket.emit('join', prompt('your name?'));         });     });     socket.on('connect', function(msg){         socket.emit('join', prompt('your name?'));     });     socket.on("disconnect", function(){         console.log("client disconnected from server");     });  </script> </body> </html> 
like image 35
Kaya Toast Avatar answered Sep 20 '22 13:09

Kaya Toast