Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xhr poll error come out is throwing while use socket.io

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

like image 332
user3898462 Avatar asked Oct 20 '22 23:10

user3898462


1 Answers

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

like image 75
sbaglieri Avatar answered Nov 15 '22 05:11

sbaglieri