socket.io:connect_error事件未发出

发布于 2025-01-28 21:13:58 字数 747 浏览 3 评论 0原文

我有一个简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文