Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I can't figure out hot to disconnect an established socket.io connection. It is 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 send data via:

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

and read it in the browser with:

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

I try with io.disconnect('//localhost:3001') on the client, but does not work. My problem is that as the connection is not closed, the messages are kept, instead of destroyed. If I can destroy the messages, that would also work for me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
563 views
Welcome To Ask or Share your Answers For Others

1 Answer

I solve it: I added this to the server code

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

and edited this on the client:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...