简体   繁体   中英

Angular page to node.js server socket connection

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM