How to close socket.io connection

I cannot figure out the hot state to disconnect the established socket.io connection. This is a simple socket.io configuration:

var app_rf = http.createServer(function(req, res) {}); var io = require('socket.io').listen(app_rf); app_rf.listen(3001); 

I am sending data via:

  io.sockets.emit('series_results', result_rf ); 

and read it in a browser using

  var socket = io.connect('//localhost:3001'); socket.on('series_results', function(data) { $('.results_rf').append("<p class='output_rf'>Gender- "+data+"</p>"); }); 

I am trying to use io.disconnect ('// localhost: 3001') on the client, but it does not work. My problem is that since the connection is not closed, messages are saved, not destroyed. If I can destroy messages, this will work for me too.

0
source share
1 answer

I solve this: I added this to the server code

 io.sockets.on('disconnect', function() { // handle disconnect io.sockets.disconnect(); io.sockets.close();}); 

and edited it on the client:

  var socket = io.connect('//localhost:3001',{'forceNew':true }); 
0
source

Source: https://habr.com/ru/post/1236186/


All Articles