Android socket.io - ReferenceError: io 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11995406/
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 - ReferenceError: io is not defined
提问by Ness
I am writing an application for Android 2.3.5 (that will also be compatible with iOS). I wish to transfer data from the app's HTML/Javascript to a Python program on a server (which uses the Twisted engine to retrieve the data).
我正在为 Android 2.3.5(也将与 iOS 兼容)编写一个应用程序。我希望将数据从应用程序的 HTML/Javascript 传输到服务器上的 Python 程序(使用 Twisted 引擎检索数据)。
I've tried many things and looked at various forums, answers, tutorials, and web pages--including most of them here--and can't find an answer. Here's the relevant Javascript I have in my index.html file:
我尝试了很多东西,并查看了各种论坛、答案、教程和网页——包括这里的大部分内容——但找不到答案。这是我在 index.html 文件中的相关 Javascript:
<script src="socket-lib/socket.io.js"></script>
<script type="text/javascript" charset="utf-8">
function sendData() {
try {
var socket = io.connect('http://mywebsite.com:12345');
socket.on('connect', function(data) {
socket.send('Hello.');
socket.on('message', function (msg) {
socket.send('This is where I send data?');
});
});
}
catch(err) {
alert('ERROR: socket.io encountered a problem:\n\n' + err);
}
} // end of sendData
If you can't tell, I'm still pretty confused how this works; I can't even test anything. The error that keeps coming up is ReferenceError: io is not defined
. Some sites used something like var io = require('socket.io');
. But then it results in the same error: ReferenceError: require is not defined
.
如果你不知道,我仍然很困惑这是如何工作的;我什至无法测试任何东西。不断出现的错误是ReferenceError: io is not defined
。一些网站使用类似var io = require('socket.io');
. 但随后它会导致相同的错误:ReferenceError: require is not defined
.
I put the socket-lib folder in assets/www, where any other Javascript source should go. This is also where the index.html file is. Many sites use <script src="/socket.io/socket.io.js"></script>
, but this makes no sense to me. Many sites also imply the use of node.js, but I never see it anywhere.
我将 socket-lib 文件夹放在 assets/www 中,任何其他 Javascript 源都应该放在那里。这也是 index.html 文件所在的位置。许多网站都使用<script src="/socket.io/socket.io.js"></script>
,但这对我来说毫无意义。许多站点也暗示使用 node.js,但我从未在任何地方看到它。
How can I make this work?
我怎样才能使这项工作?
Reply edits:
回复编辑:
I tried it in Chrome, and it's giving me an Uncaught ReferenceError: require is not defined
for the socket.io.js file. So I decide to source in require.js right before it. Then it gives the error Uncaught Error: Module name "socket.io-client" has not been loaded yet for context
. Since I'm not using this, I care not. When I try the connection, though, it gives the same io is not defined
error. When I define it as var io = require('socket.io')
, the error is Error: Module name "socket.io" has not been loaded yet for context: _ http://requirejs.org/docs/errors.html#notloaded
. I looked at the website, and it doesn't help me at all. When I try to put "require" as a function argument, another error occurs: TypeError: undefined is not a function
.
我在 Chrome 中试过了,它给了我一个Uncaught ReferenceError: require is not defined
socket.io.js 文件。所以我决定在它之前使用 require.js。然后它给出了错误Uncaught Error: Module name "socket.io-client" has not been loaded yet for context
。因为我不使用这个,所以我不在乎。但是,当我尝试连接时,它给出了相同的io is not defined
错误。当我将其定义为 时var io = require('socket.io')
,错误是Error: Module name "socket.io" has not been loaded yet for context: _ http://requirejs.org/docs/errors.html#notloaded
。我查看了网站,它根本没有帮助我。当我试图把“规定”作为函数的参数,出现另一个错误:TypeError: undefined is not a function
。
回答by Ness
I found the answer for anyone who gets immensely confused by the horrible lack of documentation of socket.io
.
我为那些因socket.io
.
You cannot source /socket-lib/socket.io.js
,
你不能来源/socket-lib/socket.io.js
,
you must source http://yourwebsite.com:12345/socket.io/socket.io.js
.
你必须来源http://yourwebsite.com:12345/socket.io/socket.io.js
。
The server automatically does the rest for you.
服务器会自动为您完成剩下的工作。
回答by shacharsol
I solved it myself by changing index.html to import the socket io client from bower, first i installed the bower component:
我自己通过更改 index.html 来从 bower 导入 socket io 客户端解决了这个问题,首先我安装了 bower 组件:
bower install socket.io-client
then i changed the reference in index.html to :
然后我将 index.html 中的引用更改为:
<script src="bower_components/socket.io-client/socket.io.js"></script>
Or file could be found at - lib/socket.io-client/dist/socket.io.js
或者文件可以在 - lib/socket.io-client/dist/socket.io.js 找到
回答by Sudhakar
write server side code in socket.io.js file and try src="/socket.io/socket.io.js"
在 socket.io.js 文件中编写服务器端代码并尝试 src="/socket.io/socket.io.js"
hope this will solve your problem
希望这能解决你的问题
回答by guerrerocarlos
When getting socket.io to work with many other libraries using require.jsI had same error, it turned out to be caused because of trying to load the socket.io.js file from the same /js folder than the rest of the other files.
当使用require.js使 socket.io 与许多其他库一起工作时,我遇到了同样的错误,结果是因为尝试从与其他其他文件相同的 /js 文件夹加载 socket.io.js 文件文件。
Placing it in a separated folder, fixed it for me, you can see the code in this gistbut all I changed for making it work, was this:
将它放在一个单独的文件夹中,为我修复它,你可以在这个要点中看到代码,但我为了使它工作而改变的是:
instead of:
代替:
socketio: 'socket.io',
socketio: 'socket.io',
Use:
用:
socketio: '../socket.io/socket.io',
socketio: '../socket.io/socket.io',
Not sure about the reason of this behavior, but I hope it helps you.
不确定这种行为的原因,但我希望它可以帮助你。
回答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 lookedlike this :
在我的 HTML 文件的一部分中,它“调用”了 javaScript 文件,它看起来像这样:
<script src='./js/playerChatter.js'></script> <!-- this one calls io -->
<script src="http://localhost:2019/socket.io/socket.io.js"></script><!-- This Creates io -->
and i changedit to this
我把它改成了这个
<script src="http://localhost:2019/socket.io/socket.io.js"></script> <!-- This Creates 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 Tim T
For me after debugging through all of the very helpful suggestions, it turned out to be simply that my node server had stopped. I had been running it manually in a terminal window during dev.
对我来说,在调试了所有非常有用的建议之后,结果只是我的节点服务器停止了。在开发期间,我一直在终端窗口中手动运行它。
Make sure your node [yourservercode].js is running on the specified port! :-]
确保您的节点 [yourservercode].js 正在指定端口上运行!:-]
回答by Vitaliy Demchuk
I use jspm.
我使用 jspm。
Add this:
添加这个:
import 'btford/angular-socket-io/mock/socket-io';
回答by Stefan Seemayer
This looks like your browser cannot find the socket.io.js file. You could try opening the index.html on your computer with Firefox+Firebug or the Chrome Web Developer Tools and look at how the .js file is requested. On the other side, you could check the logs on the webserver serving the .js file whether there are any file not found errors.
这看起来像您的浏览器找不到 socket.io.js 文件。您可以尝试使用 Firefox+Firebug 或 Chrome Web Developer Tools 在您的计算机上打开 index.html 并查看 .js 文件是如何被请求的。另一方面,您可以检查为 .js 文件提供服务的网络服务器上的日志是否有任何文件未找到错误。
The require
function would be provided by e.g. RequireJS, but you would still need to configure the paths to your scripts correctly for it to work.
该require
功能将由例如RequireJS提供,但您仍然需要正确配置脚本的路径才能使其工作。