socket.io - 的ReferenceError:IO是没有定义
我写为Android 2.3.5的应用程序(也将与iOS的兼容)。我想从应用程序的HTML / Javascript的数据传输到Python程序的服务器上(它使用双绞线引擎检索数据)。
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).
我试过很多东西,看着各种论坛,解答,教程和网页 - 包括大部分在这里 - 并不能找到答案。下面是相应的JavaScript我在我的index.html文件:
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:
<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
如果你看不出来,我仍然在pretty的困惑是如何工作的;我甚至无法测试任何东西。那不断上来的误差的ReferenceError:IO是没有定义
。有些网站用类似于 VAR IO =需要('socket.io');
。但随后它会导致同样的错误:的ReferenceError:要求没有定义
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
.
我把资产/ WWW,其中任何其他JavaScript源应该插座-lib文件夹。这也是其中的index.html文件。很多网站使用&LT;脚本的src =/ socket.io/socket.io.js"></script>
,但是这是没有意义的我。许多网站还意味着使用Node.js的,但我从来没有看到它的任何地方。
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.
我怎样才能使这项工作?
How can I make this work?
回复编辑:
我试了一下在Chrome和它给了我一个未捕获的ReferenceError:要求没有定义
的socket.io.js文件。所以,我决定权之前,它require.js到源。然后,它提供了错误未捕获的错误:模块名称socket.io客户还没有被加载的情况下
。由于我使用的不是这个,我不在乎。当我尝试连接,不过,它给出了相同的 IO没有定义
错误。当我把它定义为 VAR IO =需要('socket.io')
,错误是错误:模块名称socket.io还没有被加载的情况下:_ http://requirejs.org/docs/errors.html#notloaded
。我看了看网站,它并不能帮助我。当我尝试把规定作为函数参数,另一个错误出现:类型错误:未定义不是一个函数
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
.
我发现谁得到极大的可怕缺乏 socket.io
的文件混淆的人的答案
I found the answer for anyone who gets immensely confused by the horrible lack of documentation of socket.io
.
您不能源 /socket-lib/socket.io.js;
你必须源http://yourwebsite.com:12345/socket.io/socket.io.js".
服务器自动完成剩下的给你。
You cannot source /socket-lib/socket.io.js;
you must source "http://yourwebsite.com:12345/socket.io/socket.io.js".
The server automatically does the rest for you.