Android XHR longpoll "reconnects" each time
Hello guys, I've seen that there are couple more topics on the subject, but I still couldn't find the andswer to the question. So here's the problem: Android continiously reconnects to the server as the new client.
Little bit more details on the subject.
Client
- Device: Samsung Galaxy (GT-I9000)
- Android version: 2.2
- Build number: FROYO.XWJP6
Server
- Node.js version: v0.6.1 running on Windows 7
- Socket IO version: 0.8.7
List of installed modules (npm ls)
+-- [email protected]
+-- [email protected]
+-T [email protected]
¦ +-- [email protected]
¦ +-- [email protected]
¦ +-- [email protected]
¦ L-- [email protected]
L-T [email protected]
+-- [email protected]
+-- [email protected]
L-T [email protected]
+-- [email protected]
+-- [email protected]
L-- [email protected]
The server code:
var express = require("express");
var app = express.createServer();
var io = require('socket.io').listen(app);
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.get("/", function(req, res) {
res.sendfile(__dirname + "/public/dbg.html");
});
var maxUserId = 0;
// This limit is here to check if desktop browser behaves the same
// way. No it is not.
io.set('transports', ['xhr-polling']);
io.sockets.on('connection', function (socket) {
var userId = maxUserId++;
var userName = "User " + userId;
socket.emit('message', { text: "You have connected" });
socket.broadcast.emit('user-joined', {name: userName});
socket.on('disconnect', function () {
socket.broadcast.emit('user-left', {name: userName});
});
});
app.listen(80);
The client code
dbg.html that is served as static from the server
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
<script src="/socket.io/socket.io.js"></script>
<script>
function init() {
var socket = io.connect();
socket.on('message', function (message) {
console.log(message.text);
});
socket.on('user-joined', function (user) {
console.log(user.name + " joined")
});
socket.on('user-left', function (user) {
console.log(user.name + " left");
});
}
</script>
</head>
<body onload="init()"></body>
</html>
Observed behaviour:
When I connect to the server with the desktop browser, everything is running fine, I'm getting single "You have connected" message to the console, no matter how much time I leave the page opened. When I connect from the device, it reconnects as the different client all the time. So, device log is full of "You have connected" messages. At the same time my desktop browser is reporting that clients are connecting and disconnecting after 20 seconds:
You have connected
http://localhost/:18User 2 joined
http://localhost/:22User 2 left
http://localhost/:18User 3 joined
http://localhost/:22User 3 left
http://localhost/:18User 4 joined
Logcat output:
> adb.exe logcat -v raw browser:I *:S
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
...
Note that I'm not navigating around or reloading the page... It just stays opened
My blind guess (was wrong).
It seems that android client code fails to provide the same identifier when he makes the new connection to the server. The server thinks that this is the entirely new client, and fires the "new client" routine. That's a blind guess. I'm trying to debug the client code now, but it is kinda slow on Android.
If someone knows how to get rid of this issue, please let me know.
After playing a little more with Socket.IO I found what is the root cause of this behavior. After a certain time (around 15 seconds) my Android simply drops XHR connection. Server catches this event in socket.on('end' ... ) listener and treats it like the client is lost forever (thus removing him from the list of known clients). Next client gets the [7:::1+0] packet, [error:client not handshaken:reconnect] and reconnects.
So... I got the workaround for the issue - decrease the 'polling duration' if you have this kind of problem. 10 seconds worked for me.
I'm wondering if the same problem will happen on the different setups, for example, when you receive the GSM call, your 3G connection may be interrupted. Will it be treated by Socket.IO as the disconnected client? I will further investigate this issue, and will send my findings to the mailing list.
I have similar issues with a HTC Sense and a Sharp IS03 using port 80 (japanese carrier softbank only allows http requests over port 80 in their android phones..).
Mine disconnects far sooner than 15 seconds though, more like 0-2. Only happens over xhr-transport.
Any ideas as to why this might happen?
Try to debug in on server-side and see what is the reason of disconnects. Put few breakpoints in Transport class: Transport.prototype.onSocketEnd, Transport.prototype.onSocketClose, etc. Also try to check the connectivity on WiFi - does it show the same problems?
As the last resort - try to simplify the case even further and write the test case without socket.io, but with plaint http server (or express).
I know that this is not the answer that you're looking for, but if you follow these directions you could probably investigate another problematic case and describe the root cause of the problem to the other people who may be facing it.
@faeldt, try using wireshark + tcpdump(on server side) and you'll get precious answer on your question.
Thank you for your answers. I will investigate further and try to come up with something useful.
I have same issue on Android. socket.io client 0.8.7
Founded the cause of this problem - simultaneous posts!
Here is my fix - https://github.com/LearnBoost/socket.io-client/pull/388