socket.io xhr-轮询断开事件 [英] socket.io xhr-polling disconnect event

查看:193
本文介绍了socket.io xhr-轮询断开事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 socket.io 节点脚本:

I have a socket.io node script with:

socket.on('disconnect', function(data) {    
  console.log('disconnect!');
});

当我连接 Chrome/Safari 并关闭页面时,我看到断开连接!"在我的服务器控制台中.

When I connect with Chrome / Safari and close the page, I see 'disconnect!' in my server console.

但是,当我连接 iPhone 并关闭页面时,我看不到此消息.我看到 debug - xhr-polling 由于超出持续时间而关闭

However, when I connect with my iPhone and close the page, I don't see this message. I see debug - xhr-polling closed due to exceeded duration

我如何通过 iOS 接收断开连接事件?

How do I receive the disconnect event with iOS?

推荐答案

当您在 iPhone 中查看页面时,Socket.io 会切换到 xhr 轮询传输.这可能是由 socket.io 的配置引起的,或者是因为您 iPhone 中的浏览器不(完全)支持 websocket.

Socket.io switches to the xhr-polling transport when you are viewing the page in your iPhone. This might be caused by the configuration of socket.io or because the browser in your iPhone does not (fully) support websockets.

socket.io 中的 xhr-polling 实现在连接关闭时不会发出断开连接事件,参见 github 问题 #431.您可以通过强制 socket.io 服务器仅使用 xhr-polling 传输在 Chrome 浏览器中重现此问题:

The xhr-polling implementation in socket.io does not emit disconnect event when the connection is closed, see github issue #431. You can reproduce this issue in your Chrome browser by forcing the socket.io server to use the xhr-polling transport only:

// the server side
var io = require('socket.io').listen(httpServer);
io.set('transports', ['xhr-polling']);

好消息:您可以通过打开卸载时同步断开标志,让socket.io的客户端通知服务器断开连接:

Good news: you can ask socket.io's client to notify the server about disconnect by turning on the sync disconnect on unload flag:

// the browser (HTML) side
var socket = io.connect('http://localhost', {
  'sync disconnect on unload': true
});

警告:当网络和/或您的服务器速度较慢时,此选项可能会使用户体验变差,请参阅此 拉取请求了解更多信息.

Warning: this option can worsen the user experience when the network and/or your server are slow, see this pull request for more information.

更新

根据 socket.io 通过 XHR 轮询强制断开连接,设置 sync disconnect on unload 可能不足以解决 iPhone/iPad 上的问题.

According to socket.io force a disconnect over XHR-polling, setting sync disconnect on unload might not be enough to fix the problem on iPhone/iPad.

正如你在 socket.io-client 中看到的 源代码sync disconnect on unloadbeforeunload 事件设置了一个监听器,根据iOS Safari 不支持.

As you can see in socket.io-client source code, sync disconnect on unload sets up a listener for beforeunload event, which is not supported by iOS Safari according.

解决方案可能是修复 socket.io-client 以侦听 unloadpagehide 事件,因为卸载事件可能无法按预期进行前后优化.改用 pageshow 和 pagehide 事件. [Apple 网页内容指南].

The solution is probably to fix socket.io-client to listen for both unload and pagehide events, because the unload event may not work as expected for back and forward optimization. Use the pageshow and pagehide events instead. [Apple Web Content Guide].

这篇关于socket.io xhr-轮询断开事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆