2

事实上,我曾尝试过,但失败了。现在代码是:

function include(path){ 
var a=document.createElement("script");
a.type = "text/javascript"; 
a.src=path; 
var head=document.getElementsByTagName("head")[0];
head.appendChild(a);
} 
include("app/controller/node_modules/socket.io-client/dist/socket.io.js");

Ext.define('WGTalk.controller.guest', {
extend: 'Ext.app.Controller',

config:{
    refs:{
        viewMsg:'#viewMsg',
        txtMsg:'#txtMsg'
    },
    control:{
        '#btSend':{
            tap:'doSend'
        }
    }
},

launch:function(){
        var socket = io.connect('http://localhost:8080');

        socket.on('connect',function(){
            console.log("connected server");
        });

        socket.on('disconnect',function(){
            console.log("server closed");
        });

        socket.on('message',function(data) { 
            console.log('Received a message from the server: ',data); 
        });
},

doSend:function(){
    var msg = this.getTxtMsg().getValue();
    var msgStore = this.getViewMsg().getStore();
    msgStore.add({name:msg,age:'180'});
    this.getTxtMsg().setValue("");
}
});

错误是:*ReferenceError: Can't find variable: io @"var socket = io.connect('http://localhost:8080'); " *

我该如何解决这个错误?

4

2 回答 2

1

在你的 app.json 中,要加载像 socket.io 这样的外部 javascript 资源,修改如下:

/**
 * List of all JavaScript assets in the right execution order.
 * Each item is an object with the following format:
 * ...
 */
"js": [
    {
        "path": "socket.io.js",
        "x-bootstrap": true
    },
    {
        "path": "json.js",
        "x-bootstrap": true
    },
    ... 
 ]

如果您的移动应用程序是客户端,则上述 .js 是客户端 socket.io。

于 2013-09-02T02:11:22.597 回答
0

像这样在 index.html 中声明 io socket

<script src="http://yourip:port/socket.io/socket.io.js"></script>
于 2012-12-19T14:47:24.880 回答