5

I wrote a very simple demo about socket.io And I package it by using phonegap. I found there is problem. After I open my app about ten seconds ,the connection will disconnect because of xhr poll error.if I refresh the page in disconnect event the error won't come again. I use 1.2.0 version.here is my code. I already simplify it.

server:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');

var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());


app.use(express.static(path.join(__dirname, 'public')));


io.sockets.on('connection', function (socket) {

    console.log("disconnect--"+socket.id+"--"+io.sockets.server.eio.clientsCount);

    socket.on('disconnect', function () {
        console.log("disconnect--"+io.sockets.server.eio.clientsCount);
    });

});

http.listen(80, function () {
    console.log("server statrt");
});

client:

 $(document).ready(function () {

                var socket = io("http://192.168.0.106:80");

                socket.on('connect', function () {
                    alert("connect");
                });

                socket.on('error', function (data) {
                    alert(data);
                });

                socket.on('disconnect', function () {
                    alert("disconnect");
                });

                socket.on("reconnect", function () {
                    alert("reconnect");
                })

            });

thanks for help.my English is not very good

user3898462
  • 51
  • 1
  • 5

2 Answers2

2

You have to open the socket.io connection when the deviceready event is fired.

document.addEventListener('deviceready', function() {

    var socket = io("http://192.168.0.106:80");

    socket.on('connect', function() {
        alert("connect");
    });

    socket.on('error', function (data) {
        alert(data);
    });

    socket.on('disconnect', function () {
        alert("disconnect");
    });

    socket.on("reconnect", function () {
        alert("reconnect");
    });

});

Socket.io example

sbaglieri
  • 319
  • 2
  • 10
1

For those of you using Google Chrome, FYI Chrome does not fire 'deviceready'. Instead you should use 'DOMContentLoaded'.