简体   繁体   English

断开并手动重新连接Socket.IO

[英]Disconnecting and manually reconnecting Socket.IO

I'm initiating a socket connection using Socket.IO to a Node.js server and it's as simple as follows: 我正在使用Socket.IO启动到Node.js服务器的套接字连接,它很简单,如下所示:

var socket = io.connect(url, options);

At some point, I need to disconnect this socket when it's no longer need and reconnect it. 在某个时候,我需要在不再需要此套接字时断开连接,然后重新连接。 I'm having some issues initiating a completely new socket and I thought it would be much better to simply reconnect the same socket rather then rebuilding another socket and adding listeners to it. 我在启动一个全新的套接字时遇到了一些问题,我认为简单地重新连接同一套接字而不是重新构建另一个套接字并向其添加侦听器会更好。

Based on the socket.io documentation, I should be able to manually reconnect a socket by calling: 根据socket.io文档,我应该能够通过调用以下命令来手动重新连接套接字:

socket.connect();

But when I call that method, I'm getting an error in the console: Uncaught TypeError: Object # has no method 'connect' 但是,当我调用该方法时,在控制台中出现错误:Uncaught TypeError:Object#没有方法'connect'

Am I missing anything? 我有什么想念的吗?

You should call the socket in your socket object : 您应该在socket对象中调用socket

var socket = io.connect(url, options);
socket.socket.connect();

See this question it is related : Socket IO reconnect? 看到这个问题是否相关: 套接字IO重新连接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM