SocketIO ERR_CONNECTION_REFUSED [英] SocketIO ERR_CONNECTION_REFUSED
问题描述
我开始使用NodeJS和Socket.IO。我正在尝试使用NodeJS http服务器建立一个基本示例,并建立与服务器的Socket.IO连接。
I am starting to use NodeJS and Socket.IO. I am trying to set up a basic example with a NodeJS http server and establish a Socket.IO connection to the server.
我也使用角度JS,基本上我想要的是当用户按下按钮时,建立与服务器的连接。但是,当我尝试它时,我收到此错误
I am also using angular JS and basically what I want is that when a user presses a button then the connection to the server is established. However, when I try it I get this error
GET http://localhost/socket.io/?EIO = 2& transport = polling& ; t = 1404288173776-3 net :: ERR_CONNECTION_REFUSED
这是我的代码:
server.js
var http = require('http');
var server= http.createServer(handler);
var io = require('socket.io')(server);
server.listen(8080);
function handler(req, res) {
res.writeHead(200);
res.end('Hello Http');
}
io.on('connection', function (socket){
socket.emit('news', { hello: 'world' });
console.log('connected!');
});
app.js
var app = angular.module('testApp', ['ngRoute']);
app.controller('TestCtrl', function ($scope){
$scope.msg= "";
$scope.try = function (){
$scope.msg= "ALO"
var socket = io('http://localhost');
socket.on('news', function (data) {
console.log(data);
});
};
});
在我的test.html文件中:
And on my test.html file:
<body ng-controller="TestCtrl">
<h2>{{msg}}</h2>
<button ng-click="try()">Try</button>
<script src="../js/angular.min.js"></script>
<script src="../js/angular-route.min.js"></script>
<script src="../js/app.js"></script>
<script src="../node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
<!-- <script src="/socket.io/socket.io.js"></script> -->
</body>
我认为错误可能与包含socket.io时使用的路径有关。我试过在路径中直接使用localhost:8080,因为我读过它可能是一个解决方案,但是没有用。所以,请感谢任何帮助。谢谢!
I think that the error might be something related to the path I use when including the socket.io.js I have tried also using directly localhost:8080 in the path because I read it could be a solution but that didn't work. So please, I appreciate any help given. Thanks!
推荐答案
我遇到了完全相同的问题。我没有在socket.io@~0.9中看到ERR_CONNECTION_REFUSED。我将socket.io升级到1.3后出现错误。
I faced exactly the same issue. I did not see the ERR_CONNECTION_REFUSED in socket.io@~0.9. The error surfaced after I upgraded socket.io to 1.3.
我通过简单地从客户端构造函数中删除URL解决了这个问题:
I solved it by simply removing the URL from the client side constructor:
更改
var socket = io('http://localhost');
到
var socket = io();
正如socket.io教程所示: http://socket.io/get-started/chat/
As the socket.io tutorial showed: http://socket.io/get-started/chat/
这篇关于SocketIO ERR_CONNECTION_REFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!