node.js socket.io, io 未定义(JS 错误)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5636461/
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, io is not defined (JS Error)
提问by Mark
I have just started with socket.io, its giving JS Error on client page
我刚刚开始使用 socket.io,它在客户端页面上给出了 JS 错误
io is not defined
io 未定义
How to fix this ?
如何解决这个问题?
采纳答案by Emmerman
put <script src="http://yournodeserver/socket.io/socket.io.js"></script>into your code
放入<script src="http://yournodeserver/socket.io/socket.io.js"></script>你的代码
回答by Nev Stokes
Alternatively you can use the Socket.io CDN:
或者,您可以使用Socket.io CDN:
<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
回答by deven98602
I faced the same problem when using express. Even putting the server:port inside the script would not work.After the server started i would make socket listen to that port, that was mistake i guess.Changing it to below works fine
我在使用 express 时遇到了同样的问题。即使将 server:port 放在脚本中也不起作用。服务器启动后,我会让套接字侦听该端口,我猜这是错误的。将其更改为下面可以正常工作
var app = express();
app.set('port', process.env.PORT || 3000);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'));
On Client side I just include the script
在客户端,我只包含脚本
<script src="/socket.io/socket.io.js"></script>
回答by ABCD.ca
I have a socket app where my server (not a CDN) is serving up the socket.io.js script. So, while Emmerman is right in saying that you need to include the script tag in your client HTML code, the asset won't be loaded if your back-end is down. One option is for you to write a client-side JS script that checks for io before you try and use socket.io. If it's not present (undefined/null) then you can either conditionally show something else like, "server down" or in my case, I'm going to set a timer that keeps checking periodically until the server is restored.
我有一个套接字应用程序,我的服务器(不是 CDN)正在提供 socket.io.js 脚本。因此,虽然 Emmerman 说您需要在客户端 HTML 代码中包含脚本标记是正确的,但如果您的后端关闭,则不会加载资产。一种选择是您编写一个客户端 JS 脚本,在您尝试使用 socket.io 之前检查 io。如果它不存在(未定义/空),那么您可以有条件地显示其他内容,例如“服务器关闭”,或者在我的情况下,我将设置一个计时器,该计时器会定期检查直到服务器恢复。
[UPDATE 2] Ended up having to include the script tag, check for existence of ioobject and doing a window.location.reload() after 10 seconds (using setTimeout) (which eventually will hopefully find that the script loaded and ioexists, after which I can connect to the socket server.)
[更新 2] 最终不得不包含脚本标签,检查io对象是否存在,并在 10 秒后执行 window.location.reload()(使用setTimeout)(最终将有希望发现脚本加载并io存在,之后我可以连接到套接字服务器。)
[UPDATE] I'm loading the script with an ajax call rather than using an html script tag. Then with the timer I'm checking periodically if the script will load?– eventually it will when the server is restored/rebooted. jQuery ref to load JS scripts dynamically: http://api.jquery.com/jQuery.getScript/
[更新] 我正在使用 ajax 调用加载脚本,而不是使用 html 脚本标记。然后使用计时器,我会定期检查脚本是否会加载? - 最终会在服务器恢复/重新启动时加载。jQuery ref 动态加载 JS 脚本:http: //api.jquery.com/jQuery.getScript/
回答by Sangram Singh
<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
this is the latest version of socket.io to be included.
这是要包含的最新版本的 socket.io。
回答by Garlou
Wrap your client code on a '$(document).ready()' for jQuery or another library similar function. This way you'll be sure your code runs after the library beeing loaded.
将您的客户端代码包装在 '$(document).ready()' 用于 jQuery 或其他类似函数的库中。这样你就可以确保你的代码在库被加载后运行。
回答by Tally Barak
http://socket.io/download/- the official page for latest cdn.
http://socket.io/download/- 最新 CDN 的官方页面。
回答by Ownslo
I had to do this. (For the client.)
我不得不这样做。(对于客户。)
function setup() {
var socket;
socket = io.connect('http://localhost:3000');
}
/*
This solved my error.
*/

