简体   繁体   中英

Node + AngularJs + Socket.io: net::ERR_CONNECTION_REFUSED

I keep getting this error in my browser's console so I think it is an angular + socket.io problem:

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LF3gkk7 net::ERR_CONNECTION_REFUSED

Eventho I followed advices which told to change angular var io like:

ctrl.socketIo = io();
ctrl.socketIo = io.connect();
ctrl.socketIo = io.connect('http://localhost:3001');

none of those worked!

This is how my Angular controller looks:

angular.module("app")
    .controller("chatController", [
        'userService',
        function (userService) {

            var ctrl = this;
            ctrl.messages = [];

            ctrl.socketIo = io();
            ctrl.userName = userService.name;

            ctrl.submit = function () {

               ctrl.socketIo.emit("chat_message", ctrl.inputMessage);

               ctrl.messages.push(ctrl.inputMessage);           

               ctrl.inputMessage = null;
            };


            ctrl.socketIo.on("update_clients", function (msg) {

                ctrl.messages.push(msg);

            });

         }]
     );

My index.html path are pointing to the dirs:

<!doctype html>
<html>
<head>
 //boilerplate...
</head>

<body ng-app="app">

 //boilerplate...

   <script src="/bower_components/socket.io-client/socket.io.js"></script>

//boilerplate...

</body>
</html>

As you can see in my dir structure path is pointing correctly to socket.io.js:

在此处输入图片说明

Update

I'm using Digital ocean VPS, and when I run netstat -a -p there does NOT seem that PORT 3000 is being used.

UPdate 2:

One service in my app was trying to connect to localhost:3000 so I erased that part since I don't need socket there now I'm getting:

在此处输入图片说明

Update 3 back end code:

'use strict';

var express = require("express");
var app = express();
var http = require("http");
var server = http.createServer(app);
var io = require("socket.io")(http);

//use environment var PORT or 3001
var portNum = 3001;


//route handler
app.get("/", function (req, res) {

    res.sendFile("./index.html", {root: "./client/"});
});

app.use(express.static('./client'));

io.on("connection", function (socket) {

    socket.on("chat_message", function (msg) {

        socket.broadcast.emit('update_clients', msg);

    });
});


server.listen(portNum, function () {
    console.log("listening on port: " + portNum);
});

You are getting 404 because your socket.io module was not properly initialized.
Try the following code.

'use strict';

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

//use environment var PORT or 3001
var portNum = 3001;


//route handler
app.get("/", function (req, res) {
   res.sendFile("./index.html", {root: "./client/"});
});

app.use(express.static('./client'));

io.on("connection", function (socket) {
   socket.on("chat_message", function (msg) {
      socket.broadcast.emit('update_clients', msg);
   });
});


http.listen(portNum, function () {
   console.log("listening on port: " + portNum);
});

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