socket.io - ReferenceError: io 未定义 [英] socket.io - ReferenceError: io is not defined

查看:35
本文介绍了socket.io - ReferenceError: io 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 2.3.5(也将与 iOS 兼容)编写应用程序.我希望将数据从应用程序的 HTML/Javascript 传输到服务器上的 Python 程序(使用 Twisted 引擎检索数据).

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).

我尝试了很多方法并查看了各种论坛、答案、教程和网页(包括此处的大部分内容),但找不到答案.这是我的 index.html 文件中的相关 Javascript:

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:

' + err);
    }
} // end of sendData

如果你不知道,我仍然很困惑这是如何工作的;我什至无法测试任何东西.不断出现的错误是ReferenceError: io is not defined.一些网站使用了类似 var io = require('socket.io'); 的东西.但随后会导致相同的错误:ReferenceError: require is not defined.

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.

我将 socket-lib 文件夹放在 assets/www 中,任何其他 Javascript 源都应该放在那里.这也是 index.html 文件所在的位置.许多站点使用 <script 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 中尝试过,它给了我一个 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.我看了网站,它根本没有帮助我.当我尝试将require"作为函数参数时,出现另一个错误:TypeError: undefined is not a function.

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.

服务器会自动为您完成剩下的工作.

The server automatically does the rest for you.

这篇关于socket.io - ReferenceError: io 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆