Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transport polling error with Socket.Io

I'm getting a transport error when using socket.io v1.3.2. It's just meant to be a test for socket.io so I can familiarize myself with it.

I have a file app.js (taken directly from socket.io docs):

var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(3000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
});

And a file index.html:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">
</head>
<body>
    <h1>Socket Test</h1>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://localhost');
        socket.on('news', function (data) {
            console.log(data);
        });
    </script>

</body>
</html>

In the console, I get the following error:

GET http://localhost/socket.io/?EIO=3&transport=polling&t=1422782880286-40 
(index):1 XMLHttpRequest cannot load http://localhost/socket.io/?EIO=3&transport=polling&t=1422782880286-40. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.
socket.io.js:3715 engine.io-client:socket socket error {"type":"TransportError","description":0} +37ms
socket.io.js:1402 socket.io-client:manager connect_error +38ms
socket.io.js:1402 socket.io-client:manager reconnect attempt error +1ms
socket.io.js:1402 socket.io-client:manager will wait 5000ms before reconnect attempt +0ms
socket.io.js:3715 engine.io-client:socket socket close with reason: "transport error" +2ms
socket.io.js:3715 engine.io-client:polling transport not open - deferring close +1ms
socket.io.js:1402 socket.io-client:manager attempting reconnect +5s
socket.io.js:1402 socket.io-client:manager readyState closed +0ms
socket.io.js:1402 socket.io-client:manager opening http://localhost +0ms
socket.io.js:3715 engine.io-client:socket creating transport "polling" +5s
socket.io.js:3715 engine.io-client:polling polling +0ms
socket.io.js:3715 engine.io-client:polling-xhr xhr poll +0ms
socket.io.js:3715 engine.io-client:polling-xhr xhr open GET: http://localhost/socket.io/?EIO=3&transport=polling&t=1422782885332-41 +1ms
socket.io.js:3715 engine.io-client:polling-xhr xhr data null +0ms
socket.io.js:3715 engine.io-client:socket setting transport polling +1ms
socket.io.js:1402 socket.io-client:manager connect attempt will timeout after 20000 +3ms

Does anyone know how to fix this error. I found this discussion, but have been unable to solve the problem.

like image 551
fraxture Avatar asked Feb 01 '15 10:02

fraxture


People also ask

Does Socket.IO use polling?

First, Socket.IO creates a long-polling connection using xhr-polling. Then, once this is established, it upgrades to the best connection method available. In most cases, this will result in a WebSocket connection.

Why is Socket.IO not working?

First and foremost, please note that disconnections are common and expected, even on a stable Internet connection: anything between the user and the Socket.IO server may encounter a temporary failure or be restarted. the server itself may be killed as part of an autoscaling policy.

What is polling in Socket?

The poll() API allows simultaneous connection with all descriptors in the queue on the listening socket. The accept() and recv() APIs are completed when the EWOULDBLOCK is returned. The send() API echoes the data back to the client. The close() API closes any open socket descriptors.

How do I fix a WebSocket issue?

Solution 1 Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.


1 Answers

The difference between your code and the sample on socket.io's site is that the socket.io example code is listening on port 80 and connecting the webSocket on port 80. You are listening on port 3000, but trying to connect on port 80, thus the connection does not work. io.connect('http://localhost'); does not specify a port number so it will use the default http port which is port 80. But, your websocket server is not listening on port 80 so the connection fails.

The simplest way to fix this is to change the line of code in the client from this:

var socket = io.connect('http://localhost');

to this:

var socket = io.connect();

When you leave out the URL, it will, by default, use the domain and port from the current web page (which is what you want).

like image 165
jfriend00 Avatar answered Oct 14 '22 12:10

jfriend00