流程有些復(fù)雜,QML不支持調(diào)用很多常見的js引擎,我們可以利用webview來達(dá)到。不過在使用socket.io,發(fā)現(xiàn)必須要在安卓4.4版本或更高才行,這個(gè)不是安卓的問題,是最新版的Qt沒有優(yōu)化老版本的WebView,唉。
開發(fā)流程圖:
從發(fā)送到回調(diào):?QML?->?WEBVIEW中的socke.io??->?node.js服務(wù)器??->WEBVIEW??->?(標(biāo)注)C++?->?QML
標(biāo)注:由于socket.io是異步的,QML調(diào)用JS允許有回調(diào),但是不支持在WEBVIEW中的異步回調(diào)。因此用C++來轉(zhuǎn)發(fā)。
#ifndef?TQMLHELPER_H
#define?TQMLHELPER_H
#include#include#includeclass?TQmlHelper?:?public?QObject
{
????Q_OBJECT
public:
????explicit?TQmlHelper(QObject?*parent?=?0);
//????代碼規(guī)范:??set開頭???供QML調(diào)用??用來設(shè)置本類的QML對(duì)象
//???????????????do開頭????給HTTP服務(wù)器調(diào)用??用來間接通信QML
????Q_INVOKABLE?void??setroot(QObject*?obj);
????Q_INVOKABLE?void??setTXL(QObject*?obj);
????//從聊天服務(wù)器獲取好友?請(qǐng)求Qml刷新好友列表
????void?do_updataFlist(QByteArray&?b);
signals:
public?slots:
?private:
QObject*??root;
//PageTongxunlv.qml:
//Component.onCompleted:?{
//????????myapp.setTXL(gen);
//?????????console.log("cout:"+model.count);
//?????????model.append({name:"代碼統(tǒng)計(jì)行數(shù):"+model.count,tip:"代碼生成的行"});
//????}
//PageTongxunlv中的跟節(jié)點(diǎn)?包含好友列表的一些成員函數(shù)
QObject*??gen;
};
#endif?//?TQMLHELPER_H
#include?"tqmlhelper.h"
#include#includeTQmlHelper::TQmlHelper(QObject?*parent)?:?QObject(parent)
{
????????root?=?NULL;
}
void??TQmlHelper::setroot(QObject*?obj)
{
????qDebug()<>>:"<<?obj->objectName();
}
?void?TQmlHelper::do_updataFlist(QByteArray&?b)
?{
????????????qDebug()<<"do_updataFlist:"<<b;
????????????QString?a?=?b;?;
????????????QMetaObject::invokeMethod(gen,?"刷新好友列表",?Qt::ConnectionType::BlockingQueuedConnection,?Q_ARG(QVariant,?a));
?}
#include"myhttp.h"
#includeextern?TQmlHelper*?tmphelp;
Helloworldcontroller3::Helloworldcontroller3(QObject*?parent):HttpRequestHandler(parent)
{
}
#includevoid?Helloworldcontroller3::service(HttpRequest?&request,?HttpResponse?&response)?{
????//允許跨域
??????response.setHeader("Access-Control-Allow-Origin",?"*");?
?????????QByteArray?path=request.getPath();
????????qDebug("RequestMapper:?path=%s",path.data());
????????//登錄狀態(tài)
????????if?(path=="/loging")?{
????????????QByteArray?a?=?request.getParameter("p1");
????????????qDebug()<<a;
????????????if(a.toInt()==1)
????????????{
????????????????qDebug()<<"loging?ok";
????????????}else{
????????????????qDebug()<<"loging?erro";
????????????}
?????????????response.setStatus(200,"OK");
???????????//?HelloWorldController().service(request,?response);
????????}
?????????//獲取好友列表的返回結(jié)果
????????else?if?(path=="/Flist")?{
?????????????QByteArray?a?=?request.getParameter("p1");
?????????????tmphelp->do_updataFlist(a);
?????????????response.write("ok",true);
?????????????response.setStatus(200,"ok");
????????}
????????else?{
????????????response.setStatus(404,"oo!Not?found");
????????????QString??s?=QString::fromLocal8Bit("請(qǐng)請(qǐng)不要跨域哦");
????????????QByteArray?s2(s.toStdString().c_str());
????????????response.write(s2,true);
????????}
????????qDebug("RequestMapper:?finished?request");
}
*****main.qml
TQmlHelper*??tmphelp;
void??RegFunc(QQmlApplicationEngine&??engine)
{
???????tmphelp?=?new?TQmlHelper(&engine);
????????engine.rootContext()->setContextProperty("myapp",?(QObject*)tmphelp);
}
#includeint?main(int?argc,?char?*argv[])
{
????QApplication::setApplicationName("Myapp");
???QApplication::setOrganizationName("QtProject");
?????QApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
//?????qputenv("QT_SCALE_FACTOR",?b);
//?????system("su");
????QApplication?app(argc,?argv);
//????MyWait*??label?=?new?MyWait();
//????label->show();
???//
qDebug("********開始*********");
????QSettings*?listenerSettings=?new?QSettings("assets:/demo.ini",QSettings::IniFormat,&app);
????qDebug("config?file?loaded");
????listenerSettings->beginGroup("listener");
???????//?Start?the?HTTP?server
???????new?HttpListener(listenerSettings,?new?Helloworldcontroller3(&app),?&app);
?????QQuickStyle::setStyle("Material");
????QQmlApplicationEngine?engine;
???RegFunc(engine);
????engine.load(QUrl(QLatin1String("qrc:/main.qml")));
????if?(engine.rootObjects().isEmpty())
????????????return?-1;
//????label->hide();
qDebug("********結(jié)束*********");
????return?app.exec();
}
由于如果用C++主動(dòng)獲取QML對(duì)象,可能存在QML對(duì)象尚未初始化問題,因此,在qml初始化后調(diào)用C++來設(shè)置QML對(duì)象指針。
import?QtQuick?2.7
import?QtQuick.Controls?2.0
import?QtQuick.Dialogs?1.2
import?QtQuick.Layouts?1.1
import?QtQuick.Window?2.0
import?QtQuick.Controls.Styles?1.4
import?QtQuick.Controls.Material?2.0
import?QtGraphicalEffects?1.0
import?QtQuick.Particles?2.0
import?"Ndk.js"???as??Ndk
import?"./code"
import?QtWebView?1.1
?ApplicationWindow?{
????id:root;
????visible:?true;
?????height:?480;
?????width:?320;
????Component.onCompleted:?{
????????//把窗口對(duì)象傳給Qt?之所以加載完畢后才傳遞?很多對(duì)象提前是無內(nèi)存塊
????????myapp.setroot(root);
????}
????property?real?pixelDensity:?4.46
????property?real?multiplierH:?root.height/480?//default?multiplier,?but?can?be?changed?by?user
????property?real?multiplierW:?root.width/320?//default?multiplier,?but?can?be?changed?by?user
????function?dpH(numbers)?{
????????return?Math.round(numbers*((pixelDensity*25.4)/160)*multiplierH);
????}
????function?dpW(numbers)?{
????????return?Math.round(numbers*((pixelDensity*25.4)/160)*multiplierW);
????}
????function??dpi2(px)
????{
????????return?Math.round(px*((Screen.pixelDensity*25.4)/160))
????}
????/**
????????*?將px值轉(zhuǎn)換為dip或dp值,保證尺寸大小不變
????????*
????????*?@param?pxValue
????????*?@param?scale
????????*????????????(DisplayMetrics類中屬性density)
????????*?@return
????????*/
????function???px2dip(pxValue)?{
????????var?scale?=?myapp.getdensity();
????????return??(pxValue?/?scale?+?0.5);
????}
????property?color??accentcol:"red"
????property?color??backgroundcol:"white"
????property?color??foregroundcol:"#000000"
????property?color??primarycol:"blue"
????Material.accent:accentcol
????Material.background:backgroundcol
????Material.foreground:foregroundcol
????Material.primary:?primarycol
//消息框
????????????MessageDialog?{
????????????????id:dlg;
????????????????objectName:?"xiaoxikuang";
????????????????width:?Screen.width;
????????????????height:?Screen.hight;
????????????????visible:?false;
??????????????????title:?"進(jìn)度"
????????????????????icon:?StandardIcon.Question
????????????????????text:?"file.txt?already?exists.??Replace?"
????????????????????detailedText:?"To?replace?a?file?means?that?its?existing?contents?will?be?lost.?"?+
????????????????????????"The?file?that?you?are?copying?now?will?be?copied?over?it?instead."
????????????????????standardButtons:?StandardButton.Yes?|?StandardButton.No?
????????????}
????????????function?messaggeBox(title,txt)
????????????{
????????????????dlg.text?=?txt;
????????????????dlg.title?=?title;
????????????????dlg.visible?=?true;
????????????}
//調(diào)試打印
????????????Item?{
????????????????visible:?true
????????????????width:parent.width;
????????????????height:dpH(100);
????????????????z:100
//????????????????????color:Ndk.SDColor_warning;
????????????????????????ListView{
????????????????????????????id:dbgtxt;
????????????????????????????width:parent.width;
????????????????????????????height:dpH(100);
????????????????????????????model:?ListModel{
????????????????????????????????id:dbgmodel;
????????????????????????????????ListElement{txt:"123"}
????????????????????????????}
????????????????????????????delegate:?Label{
????????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????????font.bold:?true;
????????????????????????????????color:?Ndk.green_1
????????????????????????????????text:txt
????????????????????????????}
????????????????????????????displaced:?Transition?{
??????????????????????????????????????NumberAnimation?{?properties:?"x,y";?duration:?1000?}
??????????????????????????????????}
????????????????????????????move:?Transition?{
??????????????????????????????????????NumberAnimation?{?properties:?"x,y";?duration:?1000?}
??????????????????????????????????}
????????????????????????}
????????????????}
????????????function?dbg(txt)
????????????{
????????????????if(dbgmodel.count>=5)
????????????????{
????????????????????dbgmodel.clear();
????????????????}
????????????????dbgmodel.append({txt:txt});
????????????}
????//////////////////////加載控制器//////////////////////////
????property?alias?mwebview:?mywebview;
????????????property?var?txtprogress_var:?0
????????????property?var?txtmainpage_var:?0
?????????????property?var?txtsubpage1_var:?0
????????????Item?{
????????????????width:?Screen.width;
????????????????height:?Screen.height
????????????????visible:?true
????????????????z:100
????????????????id:?bk;
????????????????Rectangle{
????????????????????id:zezao;
????????????????????opacity:?0.9;
????????????????????anchors.fill:?parent;
????????????????????color:"#cccccc"
????????????????????Column{
????????????????????????width:?parent.width;
????????????????????????spacing:?2;
????????????????????????//WebView加載進(jìn)度
????????????????????????Label{
????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????font.bold:?true;
????????????????????????????color:?Ndk.文字色偏白
????????????????????????????id:txtprogress
????????????????????????????text:"WebKit加載進(jìn)度:"+txtprogress_var
????????????????????????}
????????????????????????Rectangle{
????????????????????????????anchors.margins:?dpW(5);
????????????????????????????width:?parent.width;
????????????????????????????height:?dpH(20);
????????????????????????????color:?Ndk.SDColor_success;
????????????????????????????radius:?dpW(2)
????????????????????????????Rectangle{
?????????????????????????????????radius:?dpW(2)
????????????????????????????????property?var?pass:?0
????????????????????????????????id:bartxt;
????????????????????????????????width:?parent.width/100*pass;
????????????????????????????????height:?dpH(20);
????????????????????????????????color:?Ndk.SDColor_info;
?????????????????????????????????Behavior?on?width?{NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}}
????????????????????????????}
????????????????????????}
?????????????????????????//mainpage加載進(jìn)度
????????????????????????Label{
????????????????????????????id:txtmainpage;
????????????????????????????color:?Ndk.文字色偏白
????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????font.bold:?true;
????????????????????????????text:"主頁面加載進(jìn)度:"+txtmainpage_var
????????????????????????}
????????????????????????Rectangle{
????????????????????????????anchors.margins:?dpW(5);
????????????????????????????width:?parent.width;
????????????????????????????height:?dpH(20);
????????????????????????????color:?Ndk.SDColor_success;
????????????????????????????radius:?dpW(2)
????????????????????????????Rectangle{
?????????????????????????????????radius:?dpW(2)
????????????????????????????????property?var?pass:?0
????????????????????????????????id:barmainpage;
????????????????????????????????width:?parent.width/100*pass;
????????????????????????????????height:?dpH(20);
????????????????????????????????color:?Ndk.SDColor_info;
?????????????????????????????????Behavior?on?width?{NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}}
????????????????????????????}
????????????????????????}
????????????????????????//子頁面加載進(jìn)度
????????????????????????Label{
????????????????????????????id:txtsubpage1;
????????????????????????????color:?Ndk.文字色偏白
????????????????????????????font.pixelSize:?dpW(18);
????????????????????????????font.bold:?true;
????????????????????????????text:"首頁加載進(jìn)度:"+txtmainpage_var
????????????????????????}
????????????????????????Rectangle{
????????????????????????????anchors.margins:?dpW(5);
????????????????????????????width:?parent.width;
????????????????????????????height:?dpH(20);
????????????????????????????color:?Ndk.SDColor_success;
????????????????????????????radius:?dpW(2)
????????????????????????????Rectangle{
?????????????????????????????????radius:?dpW(2)
????????????????????????????????property?var?pass:?0
????????????????????????????????id:barsubpage1;
????????????????????????????????width:?parent.width/100*pass;
????????????????????????????????height:?dpH(20);
????????????????????????????????color:?Ndk.SDColor_info;
?????????????????????????????????Behavior?on?width?{NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}}
????????????????????????????}
????????????????????????}
????????????????????}
????????????????????Behavior?on?opacity?{
??????????????????????NumberAnimation{?easing.type:?Easing.InOutBounce;duration:?2000}
????????????????????}
????????????????????onOpacityChanged:?{
????????????????????????if(opacity==0)
????????????????????????{
????????????????????????????bk.visible?=?false;
????????????????????????????bk.deleteLater();
????????????????????????????dbg("onOpacityChanged");
????????????????????????}
????????????????????}
????????????????}
????????????????//?禁止事件穿透
???????????????????MouseArea{
???????????????????????anchors.fill:?parent;
???????????????????????onPressed:{
????????????????????????????mouse.accepted?=?true
???????????????????????}
?????????????????????//??drag.target:?root??//?root可拖動(dòng)
???????????????????}
???????????????????//外部調(diào)用刷新更新狀態(tài)?并且判斷是否全部加載成功
????????????????function?changgepro(type,pro)
????????????????{
????????????????????if(type?==?0)//webview加載進(jìn)度
????????????????????{
????????????????????????bartxt.pass?=?pro;
????????????????????????txtprogress_var?=?pro;
????????????????????}else??if(type?==?1)//mainpage加載進(jìn)度
????????????????????{
?????????????????????????barmainpage.pass?=?pro;
????????????????????????txtmainpage_var?=?pro;
????????????????????}else??if(type?==?2)//mainpage加載進(jìn)度
????????????????????{
?????????????????????????barsubpage1.pass?=?pro;
????????????????????????txtsubpage1_var?=?pro;
????????????????????}
????????????????????if(txtprogress_var?==?100??&&?txtmainpage_var?==?100?&&?txtsubpage1_var?==100)
????????????????????{
????????????????????????oninitOk();
????????????????????}
????????????????}
????????????????//前臺(tái)頁面加載完畢后的行為
????????????????function?oninitOk()
????????????????{
????????????????????foot.visible?=?true;
????????????????????zezao.opacity?=?0;
?????????????????????benavShow?=?true;
????????????????????messaggeBox("狀態(tài)","加載完畢");
//?????????????????????bk.visible?=?false;
????????????????}
????????????}
????WebView{
????????//加載完畢?loading變成false
????????visible:?false;
????????id:mywebview;
????????objectName:?"webview"
??????????url:"file:///android_asset/index.html";
????????onLoadProgressChanged:?{
//????????????????txtprogress.text="WebKit加載進(jìn)度:"+mywebview.loadProgress;
?????????????bk.changgepro(0,mywebview.loadProgress);
????????}
????????onLoadingChanged:?{
????????}
????}
????//頁頭
????property?alias?roothd:?hd
?????header:?ToolBar{
????????id:hd;
????????states:?[
????????????State?{
????????????????name:?"hide"
????????????????PropertyChanges?{
????????????????????target:?hd;opacity:0;height:0;width:0;
????????????????}
????????????????PropertyChanges?{
????????????????????target:?lisetview;opacity:0;rotation:360;height:0;
????????????????}
????????????}
????????]
????????transitions:?Transition?{
????????????//?Make?the?state?changes?smooth
????????????ParallelAnimation?{
????????????????NumberAnimation?{?duration:?500;?properties:?"opacity,x,contentY,height,width"?}
????????????????ColorAnimation?{?property:?"color";?duration:?888?}
????????????????NumberAnimation?{?duration:?888;?properties:?"rotation"?}
????????????}
????????}
????????height:dpH(60);
????????Text{
????????????text:"mywebview.loadProgress+"
????????????anchors.centerIn:?parent
????????????color:?"white"
????????????font.pixelSize:?dpW(18)
????????}
????????layer.enabled:?true
????????layer.effect:?DropShadow?{
????????????transparentBorder:?true//繪制邊框陰影
????????????color:?"#000000";
????????????radius:?dpH(15)
????????????id:drop;
????????????//cached:?true;
????????????horizontalOffset:?0;
????????????verticalOffset:?0;
????????????samples:?16;
????????????smooth:?true;
????????}
????}
????function??getdpistype()
????{?
????????console.log("SCALA:?"+Screen.pixelDensity*25.4/160)
????????console.log(Screen.pixelDensity)
????????var?curdpi?=?Screen.pixelDensity*25.4;
????????var??mydpi?=?curdpi.toFixed(0);
????????console.log(mydpi);
????????console.log("my?dpi?"+myapp.getdpi())
????????return?"MYDPI+"+mydpi;
????????if(mydpi>=480)
????????{
????????????console.log("XXHDPI");
????????????return?"XXHDPI";
????????}else??if(mydpi>=320)
????????{
????????????console.log("XHDPI");
????????????return?"XHDPI";
????????}else??if(mydpi>=240)
????????{
????????????console.log("HDPI");
????????????return?"HDPI";
????????}else??if(mydpi>=180)
????????{
????????????console.log("MDPI");
????????????return?"MDPI";
????????}else
????????{
????????????console.log("LDPI");
????????????return?"LDPI";
????????}
????}
????Loader{
????????id:mainpage;
????????asynchronous:?true
????????anchors.fill:?parent;
????????sourceComponent:?commapnpage;
????????onProgressChanged:?{
????????????bk.changgepro(1,progress*100);
????????}
????}
????Component{
????????id:commapnpage;
????????SwipeView{
????????????state:?"hide1"
????????????currentIndex:?tabindex
????????????onCurrentIndexChanged:?{
????????????????console.log("onCurrentIndexChanged:"+currentIndex);
????????????????tabindex?=?currentIndex;//導(dǎo)航欄的序號(hào)與這里同步,手動(dòng)滑動(dòng)觸發(fā)這里
????????????????if(currentIndex==1)
????????????????{
????????????????????tongxinlu.item.獲取好友列表();
????????????????}
????????????}
????????????//首頁
????????????Page{
????????????????id:gouzhen;
????????????????Loader{
????????????????????id:shouye
?????????????????????asynchronous:?true//異步加載組件
?????????????????????anchors.fill:?parent
????????????????????sourceComponent:?Page_gouzhen{}
????????????????????onProgressChanged:?{
????????????????????????bk.changgepro(2,shouye.progress*100);
????????????????????}
????????????????}
????????????}
????????//通訊錄
????????????Page{
????????????????id:tongxun;
????????????????Loader{
??????????????????asynchronous:?true
????????????????????id:tongxinlu;
?????????????????????anchors.fill:?parent;
????????????????????sourceComponent:?PageTongxunlv{}
????????????????}
????????????}
????????}
????}
????//當(dāng)前選中的導(dǎo)航序號(hào)
???property?var??benavShow??:false;//是否顯示底部導(dǎo)航
????property?int?tabindex:?mainpage.item.currentIndex
????footer:?Row{
????????visible:?benavShow;
????????id:foot;
????????width:benavShow??parent.width:0;
????????height:benavShow??width/5*0.75:0;
????????Repeater{
????????????id:rep
????????????delegate:?NavNewDelegate{
????????????????width:?benavShow?parent.width/5:0;
????????????????height:benavShow?width*0.75:0;
????????????????rotation:benavShow?0:Math.random()*360
????????????????id:navitem;
???????????????Behavior?on?width?{NumberAnimation?{duration:?2000;?easing.type:?Easing.InOutQuad}}
???????????????Behavior?on?height?{NumberAnimation?{duration:?2000;?easing.type:?Easing.InOutQuad}}
???????????????Behavior?on?rotation?{NumberAnimation?{duration:?2000;?easing.type:?Easing.InOutQuad}}
???????????????onClick:?{
????????????????????dbg("Delegate傳遞過來的下標(biāo):"+index+tabindex);
????????????????????console.log("Delegate傳遞過來的下標(biāo):"+index+tabindex);
????????????????}
????????????}
????????????model:NavNewModel{id:model1}
????????}
????}
????Keys.enabled:?true;
????Keys.onReleased:??{
????????console.log("key"+event.key);
????????if(event.key==Qt.Key_Back)
????????{
????????????console.log("back");
????????}
????}
}
****關(guān)鍵代碼??QML和C++以及webview中的js庫交互的部分
import?QtQuick?2.7
import?QtQuick.Controls?1.4?as?Old
import?QtQuick.Controls?2.0
import?QtQuick.Layouts?1.0
import?QtQuick.Layouts?1.1
import?QtQuick.Window?2.0
import?QtQuick.Dialogs?1.2
import?QtQuick.Controls.Styles?1.4
import?QtQuick.Controls.Material?2.0
import?QtQuick.Controls.Universal?2.0
import?QtGraphicalEffects?1.0
import?QtQuick.Particles?2.0
import?QtWebSockets?1.0
import?"../"
Item?{
????property?alias?mview:?view
????objectName:?"FList";
????????anchors.fill:?parent;
????????id:gen;
????????ListView{
????????????id:view;
?????????????anchors.fill:?parent;
????????????delegate:?ListTongxunlvDelegate{
????????????}
????????????model:?ListTongxunlvModel{
????????????????id:model;
????????????}
????????????Component.onCompleted:?{
????????????????????myapp.setTXL(gen);
?????????????????????console.log("cout:"+model.count);
?????????????????????model.append({name:"代碼統(tǒng)計(jì)行數(shù):"+model.count,tip:"代碼生成的行"});
????????????????}
????????}
????????//專門被C++調(diào)用的函數(shù)
????????function?刷新好友列表(x)
????????{
????????????console.log("get?c++?*****"+x)
????????????root.messaggeBox("刷新好友列表:",x);
????????}
????function?獲取好友列表()
????{
????????mywebview.runJavaScript("獲取好友列表()",function(result){
????????????for(var?x?in??result)
????????????{
????????????????root.dbg(result[x]);
????????????}
?????????????root.dbg(result);
????????});
????}
}
**********HTML代碼?使用裸頁面只為加載JS功能
h
*****js代碼
var?msocket;
mui.init({})
mui.ready(lod);
function???登錄成功(arg1)
{
//??$.post("http://localhost:8080/loging?p1="+arg1,?function(data){
//??????alert("Data?Loaded:?"?+?data.name);
//??},"json");
?$.post("http://localhost:8080/loging?p1="+arg1);
}
function???發(fā)送到C加加(path,arg1)
{
//??$.post("http://localhost:8080/loging?p1="+arg1,?function(data){
//??????alert("Data?Loaded:?"?+?data.name);
//??},"json");
??$.post("http://localhost:8080/"+path+"?p1="+arg1);
}
var??isloging?=?false;
function??lod(){
????????alert("ok");
????????console.log("模擬器連接");
//??????????msocket?=?io.connect('ws://10.0.2.2:8081',?{?'reconnect':?true?});//模擬器訪問局域網(wǎng)
????????msocket?=?io.connect('ws://192.168.0.101:8081',?{?'reconnect':?true?});//模擬器訪問局域網(wǎng)
????????msocket.on('connect',function(){
????????????alert("連接成功;");
????????????isloging?=?true;
????????????msocket.emit("denglu","admin","123",function(callbackdata){
????????????????alert("登錄結(jié)果:"+callbackdata);
????????????})
????????????登錄成功(1);
????????});
????//正在連接
????msocket.on('connecting',function(){
????????alert("正在連接");
????});
????//連接超時(shí)
????msocket.on('connect_timeout',function(){
????????console.log("connect_timeout");
????});
????//連接失敗
????msocket.on('connect_failed',function(){
????????alert("連接失敗");
????});
????//錯(cuò)誤發(fā)生?并且無法被其他事件類型所處理
????msocket.on('error',function(data){
????????alert("錯(cuò)誤發(fā)生?并且無法被其他事件類型所處理");
????});
????//重連失敗
????msocket.on('reconnect_failed',function(){
????????alert("reconnect_failed");
????});
????//成功重連
????msocket.on('reconnect',function(TheNumber){
????????alert("reconnectOk"+TheNumber);
????});
????//正在重連
????msocket.on('reconnecting',function(TheNumber){
????????console.log("reconnecting"+TheNumber);
????});
}
function??Testfunc()
{
//??alert("get")
????return?123;
}
//對(duì)應(yīng)QML代碼:
//function?獲取好友列表()
//????{
//
?//???????mywebview.runJavaScript("獲取好友列表()",function(result){
//????????????for(var?x?in??result)
//????????????{
??//??????????????root.dbg(result[x]);
??//??????????}
?//????????????root.dbg(result);
//
//
//????????});
??//??}
var??mdata;
function?獲取好友列表()
{
????if(isloging==false)
????{
????????alert("暫未登錄,無法拉取好友");
????????return?"";
????}?
????????alert("獲取好友中");
?????????????msocket.emit("獲取好友",/*"獲取好友",*/function(data){
????????//傳過來的是字符串?不需要轉(zhuǎn)換了??交給QtQuick轉(zhuǎn)換吧?一樣
????????//不對(duì)?貌似qtquick可以直接解析對(duì)象
????????console.log(data)
????????發(fā)送到C加加("Flist",data);
????????mdata?=?data;
//??????console.log(JSON.parse(data))
????????????});
????//由于是異步的?因此可能先返回了?才接收到服務(wù)器發(fā)過來的數(shù)據(jù)?解決方式很多?這里用同步方式
????alert("最后層返回");
????return?mdata;//此返回值無用?真正的返回值是由服務(wù)器轉(zhuǎn)發(fā)過來經(jīng)過?發(fā)送到C加加("Flist",data)?轉(zhuǎn)發(fā)給QML
}