简体   繁体   English

无法使用 node.js 加载 socket.io:io 未定义

[英]Can't load socket.io using node.js: io is not defined

I generated an express app by using express-generator and when including socket.io I've noticed that socket.io client is not being loading by the browser.我使用 express-generator 生成了一个 express 应用程序,当包含 socket.io 时,我注意到浏览器没有加载 socket.io 客户端。 I've been 2 days searching on the internet for a solution with no success.我已经在互联网上搜索了 2 天的解决方案,但没有成功。

I followed socket.io instructions for express 3/4 and in fact the socket.io client that is exposed by the server on /socket.io/socket.io.js is being served (no 404 errors).我遵循了 express 3/4 的 socket.io 说明,实际上正在提供服务器在 /socket.io/socket.io.js 上公开的 socket.io 客户端(没有 404 错误)。 The problem is that when javascript code that tries to use socket io client fails because "io is not defined"问题是,当尝试使用套接字 io 客户端的 javascript 代码由于“io 未定义”而失败时

Any ideas about why the browser is not loading the client although it states that HTTP GET to http://localhost:8080/socket.io/socket.io.js returns HTTP 200?关于为什么浏览器没有加载客户端的任何想法,尽管它指出 HTTP GET 到http://localhost:8080/socket.io/socket.io.js返回 HTTP 200?

By the way, the app is also using Angular.js.顺便说一下,该应用程序也在使用 Angular.js。

index.html索引.html

<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script type="text/javascript">
 // Uncaught ReferenceError: io is not defined
 var socket = io.connect('http://localhost:8080');
</script>

app.js (no error logs and socket.io server debug message says " socket.io:server serve client source +3s") app.js(没有错误日志和 socket.io 服务器调试消息说“socket.io:server serve client source +3s”)

// setup server
app.set('port', process.env.PORT || 8080);

var server = require('http').Server(app);
var io = require('socket.io')(server);

console.log(app.get('port'));

server.listen(app.get('port'),  function() {
  console.log('Express server listening on port ' + server.address().port);

});


io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Update:更新:

By using socket.io CDN the result is the same.通过使用 socket.io CDN,结果是一样的。 It does not work它不起作用

<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>

I'm answering my own question.我在回答我自己的问题。 I finally found a solution for this problem but I still don't know what was the cause.我终于找到了这个问题的解决方案,但我仍然不知道是什么原因。

The solution was to move <script src="/socket.io/socket.io.js"></script> before any other tags (there are 16 script declarations: angular, bootstrap, jquery, moment, etc.).解决方案是将<script src="/socket.io/socket.io.js"></script>到任何其他标签之前(有 16 个脚本声明:angular、bootstrap、jquery、moment 等)。

So if you get "Uncaught ReferenceError: io is not defined" message on the browser move first "" at the beginning of the page inside tags .因此,如果您在浏览器上收到“Uncaught ReferenceError: io is not defined”消息,请在标签内的页面开头首先移动“” It will save you a lot of effort trying to make it work.它将为您节省大量努力使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM