2016-03-29 104 views
0

所以我認爲這是一個角度+ socket.io問題,我不斷收到在我的瀏覽器的控制檯,這個錯誤:節點+ AngularJs + Socket.io:網:: ERR_CONNECTION_REFUSED

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

Eventho我跟着它告訴意見改變角度var io像:

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

沒有工作!

這是我的角控制器的外觀:

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); 

      }); 

     }] 
    ); 

我的index.html路徑都指向了迪爾斯:

<!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> 

正如你可以在我的目錄結構路徑看是正確指向socket.io.js:

enter image description here

UPD吃了

我使用的是數字海洋VPS,當我運行netstat -a -p時,似乎沒有使用PORT 3000。

更新2:

在我的應用程序

一個服務試圖連接到本地主機:3000,所以我擦除的部分,因爲我現在不需要插座有我越來越:

enter image description here

更新3後端代碼:

'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); 
}); 
+0

有沒有端口不匹配?您正在使用端口'3000'和'3001'。 –

+0

@MukeshSharma,我在節點端使用:'var portNum = process.env.PORT || 3001;'當我打電話給'netstat -a -p'時,我也使用數字海洋虛擬專用服務系統似乎沒有使用PORT 3000 – Jeka

+0

由於您的後端正在監聽端口'3001 ',你的前端應用程序試圖連接到'3000'。 –

回答

1

你得到404因爲你socket.io模塊沒有正確初始化。
請嘗試下面的代碼。

'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); 
}); 
+0

非常感謝您的善意幫助! :) – Jeka

相關問題