node.js 节点服务器,Socket.io 'io 未定义'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7603224/
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
Node server, Socket.io 'io is not defined'?
提问by Hyman
I've followed the exact same steps which have always previously worked for me, create application through express, place the module dependencies in the node_modules folder. It appears that the socket.io client-side javascript file isn't being found.
我遵循了以前一直对我有用的完全相同的步骤,通过 express 创建应用程序,将模块依赖项放在 node_modules 文件夹中。似乎没有找到 socket.io 客户端 javascript 文件。
(I've looked at other peoples fixes, which is to include the JavaScript file in a script tab. I have not had to do this for my previous node + socket.io projects).
(我查看了其他人的修复程序,即在脚本选项卡中包含 JavaScript 文件。我以前的 node + socket.io 项目不必这样做)。
JavaScript on client:
客户端上的 JavaScript:
var socket = io.connect('http://localhost');
JavaScript on server:
服务器上的 JavaScript:
var io = require('socket.io').listen(app);
node_modules folder:
node_modules 文件夹:
socket.io, which has an internal node_modules folder containing socket.io-client
Error Message:
错误信息:
Uncaught ReferenceError: io is not defined
(anonymous function)
When I include the socket.io client manually:http://cdn.socket.io/stable/socket.io.js
当我手动包含 socket.io 客户端时:http : //cdn.socket.io/stable/socket.io.js
I get a different error which is:
我得到一个不同的错误是:
Uncaught TypeError: Object #<Object> has no method 'connect'
(anonymous function)
采纳答案by Jorge Israel Pe?a
On the client, did you do:
在客户端,您是否执行了以下操作:
<script src="/socket.io/socket.io.js"></script>
before you set the socketvariable?
在设置socket变量之前?
回答by Mat-e
I managed to blunder through this, and squandered about an hour, on something that turned out to be a very basic error.
我设法犯了这个错误,浪费了大约一个小时,结果证明这是一个非常基本的错误。
When an function is not defined? Such as " Uncaught ReferenceError: io is not defined ". Does that not mean that the function is getting "used" before it is "created"?
什么时候没有定义函数?比如“Uncaught ReferenceError: io is not defined”。这不意味着函数在“创建”之前就被“使用”了吗?
In the part of my HTML file, that "calls" the javaScript files, it look like this :
在我的 HTML 文件的一部分中,它“调用”了 javaScript 文件,它看起来像这样:
<script src='./js/playerChatter.js'></script> <!-- this one calls io -->
<script src="http://localhost:2019/socket.io/socket.io.js"></script><!--ThisCreatesio-->
and i changed it to this
我把它改成了这个
<script src="http://localhost:2019/socket.io/socket.io.js"></script> <!--ThisCreates io-->
<script src='./js/playerChatter.js'></script> <!-- this on calls io -->
So now the item "io", whether it is an object or function... Is actually getting created before it is getting used :D
所以现在项目“io”,无论是对象还是函数......实际上是在它被使用之前被创建:D
Have FUN!
玩得开心!
回答by adityah
Node.js newbie here!
I am pretty sure this has been answered. Yet I kept finding problems with the src for the socket. Apparently this : <script src="/socket.io/socket.io.js">was not working for me on the client side.
Node.js 新手来了!我很确定这已经得到了回答。但是我一直在寻找套接字 src 的问题。显然这 :<script src="/socket.io/socket.io.js">在客户端对我不起作用。
I've replaced the above line with this and it seems to work fine.
我已经用这个替换了上面的行,它似乎工作正常。
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
(Edit: although this is obvious, this link may not work depending on when you are reading this answer. Please pick the latest link from: https://cdnjs.com/libraries/socket.io)
(编辑:虽然这很明显,但根据您阅读此答案的时间,此链接可能不起作用。请从以下位置选择最新链接:https: //cdnjs.com/libraries/socket.io)
Here is a working client side code:
这是一个有效的客户端代码:
<body>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function(){
var socket = io('http://localhost:8080');
console.log("Socket connected"+socket.connected);
socket.on('notification', function(value){
//insert your code here
});
});
</script>
On the server side(handles only 1 socket)
在服务器端(仅处理 1 个套接字)
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 8080;
app.get('/', function(req, res){
console.log("app works");
});
io.on('connection', function(socket){
socket.emit('notification', {message:"hi"});
});
http.listen(port, function(){
console.log('listening on :' + port);
});
回答by Kaushik Makwana
On the client:
在客户端:
<head>
<script src="http://localhost/socket.io/socket.io.js"></script>
</head>
回答by user7257009




- Use server.listen() can fixed it;
- 使用 server.listen() 可以修复它;

