问题
in fact, I had try it,and failed. now the code is:
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("");
}
});
and the error is: *ReferenceError: Can't find variable: io @"var socket = io.connect('http://localhost:8080'); " *
how can I resolve this error?
回答1:
In your app.json, to load external javascript resources like socket.io, modify as follows:
/**
* 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
},
...
]
If your mobile app is a client, the above .js are client side socket.io.
回答2:
Declare io socket inside index.html like this
<script src="http://yourip:port/socket.io/socket.io.js"></script>
来源:https://stackoverflow.com/questions/11354974/senchatouch2-0-socket-io-how-to-use-socket-io-js-in-senchtouch2-0