node.js socket.io:无法加载资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11653237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.io: Failed to load resource
提问by gaggina
I'm trying to getting started with socket.io and node.js.
我正在尝试开始使用 socket.io 和 node.js。
Following the first example on the socket.io's site I'm getting the following error in the browser's console:
按照 socket.io 站点上的第一个示例,我在浏览器的控制台中收到以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3001/socket.io/socket.io.js
Uncaught ReferenceError: io is not defined
This is my server.js
这是我的 server.js
var app = require('express').createServer()
, io = require('socket.io').listen(app);
app.listen(3001);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
And this is my index.html
这是我的 index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8" />
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</body>
</html>
I've already installed socket.io..
我已经安装了socket.io..
回答by Michael Zaporozhets
The Issues
问题
First of all you need to be looking at the server port that the server is bound on (
app.listen(3001);) on the client side in order to reach the server at all.As for socket.io, adding
http://localhost:3001before the rest of the source in the link tag solves this problem. This is apparently due to the way the network binds ports to localhost, however I will try to find some more information on the cause;
首先,您需要
app.listen(3001);在客户端查看服务器绑定在 ( ) 上的服务器端口,以便完全访问服务器。至于socket.io,
http://localhost:3001在link标签中的其余源之前添加解决了这个问题。这显然是由于网络将端口绑定到 localhost 的方式造成的,但是我会尝试找到有关原因的更多信息;
What to change:
改变什么:
The port binding for the server:
服务器的端口绑定:
var socket = io.connect('http://localhost');
var socket = io.connect('http://localhost');
should be change to
应该改为
var socket = io.connect('http://localhost:3001');
var socket = io.connect('http://localhost:3001');
Making socket.io behave:
使 socket.io 行为:
<script src="/socket.io/socket.io.js"></script>
<script src="/socket.io/socket.io.js"></script>
should be change to
应该改为
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
回答by Marius Butuc
If you are using expressversion 3.x there are Socket.IO compatibility issues that require a bit of fine tuning to migrate:
如果您使用的是express3.x 版,则存在Socket.IO 兼容性问题,需要进行一些微调才能迁移:
Socket.IO's
.listen()method takes anhttp.Serverinstance as an argument.
As of 3.x, the return value ofexpress()is not anhttp.Serverinstance. To get Socket.IO working with Express 3.x, make sure you manually create and pass yourhttp.Serverinstance to Socket.IO's.listen()method.
Socket.IO 的
.listen()方法将http.Server实例作为参数。
从 3.x 开始, 的返回值express()不是http.Server实例。要让 Socket.IO 与 Express 3.x 一起工作,请确保您手动创建http.Server实例并将其传递给 Socket.IO 的.listen()方法。
Here is a quick example:
这是一个快速示例:
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
server.listen(3000);
回答by tfmontague
Firstly, the Socket.io "How To Use" documentation is vague (perhaps misleading); especially when documenting code for the Socket.io client.
首先,Socket.io“如何使用”文档含糊不清(可能具有误导性);特别是在为 Socket.io 客户端编写代码文档时。
Secondly the answers you've been given here on StackOverflow are too specific. You shouldn't have to manually hardcode the protocol, hostname, and port number for the Socket.io client; that's not a scalable solution. Javascript can handle this for you with the location object - window.location.origin.
其次,您在 StackOverflow 上得到的答案太具体了。您不必为 Socket.io 客户端手动硬编码协议、主机名和端口号;这不是一个可扩展的解决方案。Javascript 可以使用位置对象 - 为您处理此问题window.location.origin。
Getting started with Socket.io
Socket.io 入门
Installation
安装
Socket.io requires the installation of a server and client.
Socket.io 需要安装服务器和客户端。
To install the servernpm install socket.io
安装服务器npm install socket.io
To use the client, add this script to your document (index.html)<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
要使用客户端,请将此脚本添加到您的文档 (index.html)<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
Implementation with Express 3/4
使用 Express 3/4 实现
Create the following files:
创建以下文件:
Server (app.js)
服务器 (app.js)
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client (index.html)
客户端 (index.html)
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
// origin = http(s)://(hostname):(port)
// The Socket.io client needs an origin
// with an http(s) protocol for the initial handshake.
// Web sockets don't run over the http(s) protocol,
// so you don't need to provide URL pathnames.
var origin = window.location.origin;
var socket = io.connect(origin);
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
回答by Laky
If you're trying to make this run on a different computer and you are getting this error, you may find this useful.
如果您尝试在另一台计算机上运行此程序并且遇到此错误,您可能会发现这很有用。
var host = window.location.hostname;
var socket = io.connect('http://' + host);
If you're using https, then obviously you need to change the protocol as well. In my case I needed to create a server that would run on several local computers, so putting here a fixed address would not work, as the socket would need to connect to a different address depending on what server you are loading the page from. Adding this here as it may help someone else too.
如果您使用的是 https,那么显然您也需要更改协议。在我的情况下,我需要创建一个可以在多台本地计算机上运行的服务器,因此在这里放置固定地址是行不通的,因为套接字需要连接到不同的地址,具体取决于您从哪个服务器加载页面。在此处添加此内容,因为它也可能对其他人有所帮助。
回答by Abdul Hamid
Try this
尝试这个
var app = express()
,http = require('http');
var server = http.createServer(app);
server.listen(3001);
io = require('socket.io').listen(server);
io.set('log level', 1);
Hope it helps
希望能帮助到你
回答by thavith
I know this is very very late, but I found a solution for those who might have previously set node.js up. My machine needed get hardware replaced, when it came back, I noticed quite a few settings were back to factory default. I was also getting the error discussed above.
我知道这已经很晚了,但我为那些之前可能已设置 node.js 的人找到了解决方案。我的机器需要更换硬件,当它回来时,我注意到很多设置都恢复到出厂默认值。我也遇到了上面讨论的错误。
Running
跑步
sudo apachectl start
须藤 apachectl 开始
from the terminal fixed up my issue.
从终端解决了我的问题。

