Xhr poll error is thrown when using socket.io

I wrote a very simple demo about socket.io. I will pack her using phone records. I found that there is a problem. After I open my application for about ten seconds, the connection will be disconnected due to xhr poll error. If I refresh the page in a disabled case, the error will not appear again. I am using version 1.2.0. Here is my code. I am already simplifying this.

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 the help .my english is not very good

+5
source share
2 answers

You need to open 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

+2
source

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

0
source

Source: https://habr.com/ru/post/1205920/


All Articles