socket.io:connect_error事件未发出
我有一个简单的socket.io服务器,并且一个连接到它的客户端:
socket = io('http://localhost:' + port + '/chat', {
reconnection: false,
query: {
name: username
}
})
它的工作正常,但是当我尝试在找不到服务器时尝试实现该客户端的行为(我只是通过加载页面即可在不启动页面上进行测试,而无需启动即可服务器),我面临一个简单的问题:connect_error
事件不是发射器。在我初始化套接字后,我有此代码:
socket.on('connect_error', ()=>{
console.log("Error! ")
})
正如我所说,此回调从未被调用,我在控制台中收到一条错误消息,指出请求返回404(http:// localhost:5454/socket。 io/?name = aa& eio = 4& transport = polling& t = o2__7jm
),但我的自定义错误消息永远不会打印。
那么,我在做什么错?如果无法建立与服务器的连接,难道是否应该解雇此事件?如果没有,我该如何处理这种情况(即,如果连接失败,我该如何使socket.io调用某个回调?)
i have a simple socket.IO server, and a client that connects to it :
socket = io('http://localhost:' + port + '/chat', {
reconnection: false,
query: {
name: username
}
})
it works perfectly fine, but when i tried implementing the behavior of that client when the server cannot be found (which i simply test by just loading the page without starting the server), i faced a simple issue : the connect_error
event is not emitter. Right after i initialize the socket, i have this code :
socket.on('connect_error', ()=>{
console.log("Error! ")
})
as i said, this callback is never called, i get an error message in the console stating that the request returned 404 (http://localhost:5454/socket.io/?name=AA&EIO=4&transport=polling&t=O2__7JM
), but my custom error message is never printed.
So, what am i doing wrong ? Isn't this event supposed to be fired if the connection with the server couldn't be established ? If not, how do i handle this situation (i.e. how do i make socket.io call a certain callback if the connection failed ?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论