Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

node.js - How to close a socket.io connection

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

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

1 Reply

0 votes
by (71.8m points)

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 });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...