node.js - socket.io xhr-polling disconnect event -
i have socket.io node script with:
socket.on('disconnect', function(data) { console.log('disconnect!'); }); when connect chrome / safari , close page, see 'disconnect!' in server console.
however, when connect iphone , close page, don't see message. see debug - xhr-polling closed due exceeded duration
how receive disconnect event ios?
socket.io switches xhr-polling transport when viewing page in iphone. might caused configuration of socket.io or because browser in iphone not (fully) support websockets.
the xhr-polling implementation in socket.io not emit disconnect event when connection closed, see github issue #431. can reproduce issue in chrome browser forcing socket.io server use xhr-polling transport only:
// server side var io = require('socket.io').listen(httpserver); io.set('transports', ['xhr-polling']); good news: can ask socket.io's client notify server disconnect turning on sync disconnect on unload flag:
// browser (html) side var socket = io.connect('http://localhost', { 'sync disconnect on unload': true }); warning: option can worsen user experience when network and/or server slow, see pull request more information.
update
according socket.io force disconnect on xhr-polling, setting sync disconnect on unload might not enough fix problem on iphone/ipad.
as can see in socket.io-client source code, sync disconnect on unload sets listener beforeunload event, not supported ios safari according.
the solution fix socket.io-client listen both unload , pagehide events, because the unload event may not work expected , forward optimization. use pageshow , pagehide events instead. [apple web content guide].
Comments
Post a Comment