Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io 400 (Bad Request)

I have this piece of code on my server

    var express = require('express');
    var routes = require('./routes');
    var user = require('./routes/user');
    var http = require('http');
    var path = require('path');

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

    server.listen(3000);

    io.sockets.on('connection', function (socket) {
      console.log("Socket connected");
    });

I just want to create a connection

and on the client -

<script src="public/javascripts/socket.io.js"></script>
     <script>
        var socket = io.connect('http://127.0.0.1:3000');
     </script>

And when I open my browser I get this error in console:

GET http://127.0.0.1:3000/socket.io/1/?t=1404410309733 400 (Bad Request) socket.io.js:1659
XHR finished loading: GET "http://127.0.0.1:3000/socket.io/1/?t=1404410309733". 

I've already done this like 10 times and I never get this. Does anyone know how to fix it?

like image 874
Markuz Shultz Avatar asked Jul 03 '14 18:07

Markuz Shultz


1 Answers

I think an older version of socket-io could be the reason.

I was getting this problem when I was using 0.9.16 version. I upgraded to following and it worked.

<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
like image 182
Mritunjay Avatar answered Sep 26 '22 20:09

Mritunjay