4

I have the following app with express/socket.io (the app is listening and live without any errors). When I http-request it I get the following error:

GET http://xxxxxxx.com:3035/socket.io/1/?t=1449090610579 400 (Bad Request)

And on the socket.io reponse at http://xxxxxxx.com:3035/socket.io I get:

Cannot GET /socket.io

app.js:

var express = require('express'),
    http = require('http'),
    sio= require('socket.io'),
    fs=require('fs'),
    app = express();

var mysql      = require('mysql');
var connection = mysql.createConnection({
        host: 'localhost',
        user: 'xxxxxxx',
        password: 'xxxxxxx',
        database: 'xxxxxxxxx'
    }
);
connection.connect();

// Start the server
var server=app.listen(3035, function () {
    console.log("Express server listening on port %d",3035);
});
app.io=io=sio.listen(server);
.
.
.

on the client side:

var socket = io.connect('http://xxxxxx.com:3035');
5
  • Looks like it can't get the static script file because you're missing a basic static route. Commented Dec 2, 2015 at 21:21
  • This was running well before I migrate to a new server. not sure what is missing exactly! Commented Dec 2, 2015 at 21:22
  • Then it's probably not the static route thing, Socket.io creates it's own routes if I remember correctly. Something changed when migrating, and the URL is no longer correct, you probably have to do some fault searching on your own. Commented Dec 2, 2015 at 21:25
  • Yeah I am trying to figure out. thank you for the hint. Commented Dec 2, 2015 at 21:26
  • var express = require('express')(); var http = require('http').Server(express); var sio = require('socket.io')(http); Commented Dec 2, 2015 at 22:05

3 Answers 3

1

Your sio is a function. You need to call it with your server as an argument.

Add server=require('http').Server(app) sio=require('socket.io')(server)

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried the same in vain. Thank you. I will try around with this!
1

io.connect() takes a URL such as http://example.com:3035.

You are passing xxxxxx.com:3035 which is not a proper URL form.


Also, note that if you're just trying to connect to the same server and port as the web page came from, you can just use:

io()

or

io.connect()

And the socket.io library will connect back to the same host and port as the web page.

2 Comments

I was using the full URL with http. Sorry I did a typing mistake while masking the domain, I will correct it. using io() broke my app not sure why!
Thank you by the way for your answer. more hints allows me to figure out the problem!
0

You should initialize default connection on client side by

io.connect( "/" );

According to documentation by default socket.io will connect to the same host and port where rendered webpage is. The / in the path states to connect to default namespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.