1

I have a problem with this.

This is the code in my node.js server:

var bodyParser = require("body-parser");
var express    = require('express');
var http       = require('http').Server(app);
var io         = require('socket.io')(http);
var app        = express();

......    

io.sockets.on('connection', function(socket){
  socket.emit('welcome', { message: 'Welcome!', id: socket.id });

    socket.on('i am client', console.log);
});

app.listen(port);

And my angular page:

var modulo_angular = angular.module('app', ['btford.socket-io', 'ngRoute']);
modulo_angular.factory('socket', function (socketFactory) {
    var myIoSocket = io.connect('http://myurl:8000/');
    var mySocket = socketFactory({
        ioSocket: myIoSocket
    });
   return mySocket;
});

modulo_angular.controller("appCtrl",['factoryController', 'socket',controladorSecundario]);

The error I get is:

GET http://myurl:8000/socket.io/?EIO=3&transport=polling&t=1435131076378-14 404 (Not Found)

If I use 'http://localhost' instead an url, I get ERR_CONNECTION_REFUSED. I am in the same network, connected through a vpn and ports are opened. Do I need something else in my code?

1 Answer 1

1

Apparently the browser trying to get http://myurl:8000/socket.io/ which is missing. I believe this is due to your server setup.

Try this:

var port = 8000;
var app = express();
var server = app.listen(port); // start the server
var io = require('socket.io').listen(server); // start socketIO
Sign up to request clarification or add additional context in comments.

6 Comments

I think port is ok. (Sorry I forgot to add 'var port = 8000;'), because I can access to my webpage which is served by my node.js server.
By saying "this is due to your server setup" I didn't mean the port. I meant setting up express with socketIo is not correct. Try opening http://myurl:8000/socket.io/ in your browser
I get {"code":0,"message":"Transport unknown"}. I guess you are right. How can I check my server setup?
Try replacing the first 5 lines in your node.js code with the lines in the answer.
same problem. Anyway, I have to add bodyParser and require('express') line to avoid errors when I run the server. I installed socket.io through npm and I didn't see any error in log.
|

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.