socket.io:无法加载资源(socket.io: Failed to load resourc

2019-06-26 12:56发布

我想入门socket.io和node.js中

继上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 

这是我的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);
  });
});

这是我的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>

我已经安装了socket.io ..

Answer 1:

问题

  • 首先你需要看在服务器上绑定的服务器端口( app.listen(3001);以达到在服务器上的所有客户端)。

  • 至于socket.io,将http://localhost:3001中的链接标签源的其余部分解决了这个问题之前。 这显然是由于网络端口绑定到本地主机的方式,但是我会尽量找到原因的一些详细信息;

要改变什么:


端口服务器绑定:

var socket = io.connect('http://localhost');

应改变

var socket = io.connect('http://localhost:3001');



制作socket.io的行为:

<script src="/socket.io/socket.io.js"></script>

应改变

<script src="http://localhost:3001/socket.io/socket.io.js"></script>




Answer 2:

如果您使用的express 3.x版本有需要一点微调的迁移Socket.IO兼容性问题 :

Socket.IO的.listen()方法接受一个http.Server实例作为参数。
作为3.X的 ,返回值express()不是一个http.Server实例。 为了让Socket.IO与快递3.X工作,请确保您手动创建和你传递http.Server实例Socket.IO的.listen()方法。

下面是一个简单的例子:

var app = express()
  , http = require('http')
  , server = http.createServer(app)
  , io = require('socket.io').listen(server);

server.listen(3000);


Answer 3:

首先,Socket.io“ 如何使用 ”文档是模糊的(也许是误导性的); 对于Socket.io客户尤其是记录代码时。

其次,你一直在这里给出在计算器上的答案都太具体。 你不应该手动硬编码协议,主机名,并为客户Socket.io端口号; 这不是一个可扩展的解决方案。 JavaScript可以与位置对象您处理该问题- window.location.origin

入门Socket.io

安装

Socket.io需要服务器和客户端的安装。

要安装服务器
npm install socket.io

要使用客户端,这个脚本添加到您的文档(的index.html)
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>

与快速实现3/4

创建以下文件:

服务器(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);
  });
});

客户端(的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>


Answer 4:

如果你试图让不同的计算机上运行这和你得到这个错误,你可能会发现这很有用。

var host = window.location.hostname; 
var socket = io.connect('http://' + host);

如果您使用HTTPS,那么显然你需要改变协议为好。 在我来说,我需要创造条件,在几个本地计算机上运行的服务器,所以把这里固定的地址是行不通的,因为插座将需要连接到这取决于服务器正在从中装载页面不同的地址。 添加此这里,因为它可能帮助别人了。



Answer 5:

试试这个

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

希望能帮助到你



Answer 6:

我知道这是非常非常晚了,但我发现那些谁可能以前设置的node.js了一个解决方案。 我的机器需要采用硬件更换,当它回来的时候,我注意到不少设置是恢复到出厂默认值。 我也越来越上文所讨论的错误。

运行

须藤的apachectl启动

从终端固定了我的问题。



文章来源: socket.io: Failed to load resource