自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Harmonyos網(wǎng)絡通信真機Demo演練(一)之TCP聊天室

開發(fā) OpenHarmony
本Demo界面由ArkUI實現(xiàn),網(wǎng)絡邏輯部分由java實現(xiàn),服務器用容易部署演練的Go實現(xiàn)。JAVA和GO初次實戰(zhàn),本Demo還存在并發(fā)數(shù)據(jù)安全未處理,所以本Demo僅能用于學習。

[[439633]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

本Demo界面由ArkUI實現(xiàn),網(wǎng)絡邏輯部分由java實現(xiàn),服務器用容易部署演練的Go實現(xiàn)。JAVA和GO初次實戰(zhàn),本Demo還存在并發(fā)數(shù)據(jù)安全未處理,所以本Demo僅能用于學習??蓪W習之處有以下幾點:

    一、FA與PA采用ACE方式的調(diào)用及相互交互、數(shù)據(jù)流轉(zhuǎn)等。

    二、Harmonyos的事件機制及使用–自定義事件.

    三、異步多線程TCP通信。

一、效果展示

1,服務器端

#星光計劃2.0#Harmonyos網(wǎng)絡通信真機Demo演練(一)之TCP聊天室-鴻蒙HarmonyOS技術社區(qū)

2,客戶端

#星光計劃2.0#Harmonyos網(wǎng)絡通信真機Demo演練(一)之TCP聊天室-鴻蒙HarmonyOS技術社區(qū)

二、設計流程圖

#星光計劃2.0#Harmonyos網(wǎng)絡通信真機Demo演練(一)之TCP聊天室-鴻蒙HarmonyOS技術社區(qū)

三、界面編寫

1,界面效果

#星光計劃2.0#Harmonyos網(wǎng)絡通信真機Demo演練(一)之TCP聊天室-鴻蒙HarmonyOS技術社區(qū)

2,界面代碼

HML代碼:

  1. <div class="container"
  2.     <div class="container1"
  3.         <text class="title"
  4.             tcp-client test 
  5.         </text> 
  6.     </div> 
  7.     <div class="container2"
  8.         <text class="text2">IP:</text> 
  9.         <input class="input2" placeholder="enter ip" onchange="onChange2"
  10.         </input> 
  11.     </div> 
  12.     <div class="container3"
  13.         <text class="text3">Port:</text> 
  14.         <input class="input3" placeholder="enter port" onchange="onChange3"
  15.         </input> 
  16.     </div> 
  17.     <button class="button1" type="capsule" onclick="onConnect">連接服務器</button> 
  18.     <button class="button3" type="capsule" onclick="onSubResvMsg">訂閱消息</button> 
  19.     <textarea class="text4"
  20.         {{outcont}} 
  21.     </textarea> 
  22.     <div class="container4"
  23.         <input class="input5" placeholder="enter msg" onchange="onChange4"
  24.         </input> 
  25.         <button class="button2" type="capsule" onclick="onSend">發(fā)送消息</button> 
  26.     </div> 
  27. </div> 

JS代碼:

  1. import prompt from '@system.prompt'
  2.  
  3. export default { 
  4.     data: { 
  5.         title: 'World'
  6.         outcont: ''
  7.         ip:''
  8.         port:''
  9.         msg:'' 
  10.     }, 
  11.  
  12.     initConnectAction: function (code,ip,port) { 
  13.         var actionData = {}; 
  14.         actionData.ip = ip; 
  15.         actionData.port = port; 
  16.         var action = {}; 
  17.         action.bundleName = "com.gane.tcpclient"
  18.         action.abilityName = "TcpClientAbility"
  19.         action.messageCode = code; 
  20.         action.data = actionData; 
  21.         action.abilityType = 1; 
  22.         action.syncOption = 0; 
  23.         return action
  24.     }, 
  25.  
  26.     initAction: function (code) { 
  27.         var actionData = {}; 
  28.         var action = {}; 
  29.         action.bundleName = "com.gane.tcpclient"
  30.         action.abilityName = "TcpClientAbility"
  31.         action.messageCode = code; 
  32.         action.data = actionData; 
  33.         action.abilityType = 1; 
  34.         action.syncOption = 0; 
  35.         return action
  36.     }, 
  37.  
  38.     initAction2: function (code,msg) { 
  39.         var actionData = {}; 
  40.         actionData.msg = msg; 
  41.         var action = {}; 
  42.         action.bundleName = "com.gane.tcpclient"
  43.         action.abilityName = "TcpClientAbility"
  44.         action.messageCode = code; 
  45.         action.data = actionData; 
  46.         action.abilityType = 1; 
  47.         action.syncOption = 0; 
  48.         return action
  49.     }, 
  50.  
  51.  
  52.     onChange2(e){ 
  53.         this.ip = e.value; 
  54.     }, 
  55.  
  56.     onChange3(e){ 
  57.         this.port = e.value; 
  58.     }, 
  59.  
  60.     onConnect: async function() { 
  61.         try { 
  62.             var action = this.initConnectAction(1001,this.ip,this.port); 
  63.             var result = await FeatureAbility.callAbility(action); 
  64.             console.info(" result = " + result); 
  65.             this.showToast(result); 
  66.         } catch (pluginError) { 
  67.             console.error("getBatteryLevel : Plugin Error = " + pluginError); 
  68.         } 
  69.     }, 
  70.  
  71.     onSubResvMsg:async function(){ 
  72.         try { 
  73.             var action = this.initAction(1003); 
  74.             var that = this; 
  75.             var result = await FeatureAbility.subscribeAbilityEvent(action,function (msgdata) { 
  76.                 console.info(" batteryLevel info is: " + msgdata); 
  77.                 var msgRet = JSON.parse(msgdata).data; 
  78.                 that.printData(msgRet.msg); 
  79.                 that.showToast(" batteryState change: " + msgRet.msg); 
  80.             }); 
  81.             this.showToast(" subscribe result " + result); 
  82.             console.info(" subscribeCommonEvent result = " + result); 
  83.         } catch (pluginError) { 
  84.             console.error("subscribeCommonEvent error : result= " + result + JSON.stringify(pluginError)); 
  85.         } 
  86.     }, 
  87.  
  88.     onSend: async function() { 
  89.         try { 
  90.             var action = this.initAction2(1002,this.msg); 
  91.             var result = await FeatureAbility.callAbility(action); 
  92.             console.info("onSend result = " + result); 
  93.             this.showToast(result); 
  94.         } catch (pluginError) { 
  95.             console.error("getBatteryLevel : Plugin Error = " + pluginError); 
  96.         } 
  97.     }, 
  98.  
  99.     onChange4(e){ 
  100.         this.msg = e.value; 
  101.     }, 
  102.  
  103.     printData(msg){ 
  104.         if(this.outcont != null || this.outcont != ""){ 
  105.             this.outcont = msg + "\n" + this.outcont; 
  106.         } else { 
  107.             this.outcont = msg; 
  108.         } 
  109.     }, 
  110.  
  111.     showToast: function (msg) { 
  112.         prompt.showToast({ 
  113.             message: msg 
  114.         }); 
  115.     } 

注意:

  1,這里的交互方法都是用的異步方法,因為這樣不會因業(yè)務側(cè)而阻塞UI線程,從而阻塞主線程。

  2,仔細看清楚每個initAction(),弄明白action的構造和帶參傳遞的寫法。

四、PA編寫與交互

java類實現(xiàn)方式如下:

  1. public class TcpClientAbility extends AceInternalAbility { 
  2.     private static final String TAG = TcpClientAbility.class.getSimpleName(); 
  3.     private static final String BUNDLE_NAME = "com.gane.tcpclient"
  4.     private static final String ABILITY_NAME = "TcpClientAbility"
  5.     public static final String SELF_SOCKET_MSG = "TCP.CLIENT.MSG"
  6.     private static TcpClientAbility instance; 
  7.     private TcpClientAbility() { 
  8.         super(BUNDLE_NAME, ABILITY_NAME); 
  9.     } 
  10.  
  11.     /** 
  12.      * ACE注冊 
  13.      */ 
  14.     public void register() { 
  15.         this.setInternalAbilityHandler(this::onRemoteRequest); 
  16.         HiLog.error(LABEL_LOG,"socket_register"); 
  17.     } 
  18.  
  19.     /** 
  20.      * ACE取消注冊 
  21.      */ 
  22.     public void deregister() { 
  23.         this.setInternalAbilityHandler(null); 
  24.         HiLog.error(LABEL_LOG,"socket_unregister"); 
  25.     } 
  26.     /** 
  27.      * ACE事件回調(diào)接口 
  28.      */ 
  29.     public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) { 
  30.         switch (code) { 
  31.         } 
  32.         return true
  33.     } 

注意:

  1,類必須繼承AceInternalAbility,必須實現(xiàn)注冊、取消注冊、事件回調(diào)接口。

  2,register()、deregister()需在合適的位置調(diào)用,我是在mainAblity的onstart和onstop中調(diào)用的

五、TCP客戶端網(wǎng)絡

網(wǎng)絡實現(xiàn),考慮到要能隨時隨地的自由發(fā)送和接收消息,就將消息的收、發(fā)分離,全采用異步進行。根據(jù)業(yè)務需求選型了AsynchronousSocketChannel作為本次實現(xiàn)的網(wǎng)絡基礎類型,主要用到了AsynchronousSocketChannel.open()、AsynchronousSocketChannel.setOption()、AsynchronousSocketChannel.connect()、AsynchronousSocketChannel.write()、AsynchronousSocketChannel.read()等接口。

示例代碼如下:

  1. socketChannel = AsynchronousSocketChannel.open(); 
  2. socketChannel.setOption(StandardSocketOptions.TCP_NODELAY,true); 
  3. socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); 
  4. socketChannel.connect(new InetSocketAddress(param.getIp(), param.getPort()), null
  5.     new CompletionHandler<Void, Object>() { 
  6.         @Override 
  7.         public void completed(Void result, Object attachment) { 
  8.         ByteBuffer byteBuffer = ByteBuffer.allocate(1024); 
  9.         String intput = "我是:"
  10.         try { 
  11.             intput = intput + socketChannel.getLocalAddress().toString(); 
  12.         } catch (IOException e) { 
  13.             e.printStackTrace(); 
  14.         } 
  15.         try { 
  16.             byteBuffer.put(intput.getBytes("UTF-8")); 
  17.         } catch (UnsupportedEncodingException e) { 
  18.             e.printStackTrace(); 
  19.         } 
  20.         socketChannel.write(byteBuffer, 1, TimeUnit.SECONDS, null
  21.             new CompletionHandler<Integer, Object>() { 
  22.             @Override 
  23.             public void completed(Integer result, Object attachment) { 
  24.             } 
  25.             public void failed(Throwable exc, Object attachment) { 
  26.             } 
  27.         }); 
  28.         } 
  29.  
  30.         @Override 
  31.         public void failed(Throwable exc, Object attachment) { 
  32.         } 
  33.     }); 
  1. if (TcpClientAbility.socketChannel != null && TcpClientAbility.socketChannel.isOpen()) { 
  2.     ByteBuffer byteBuffer = ByteBuffer.allocate(1024); 
  3.     CompletionHandler<Integer, Object> comphandler = new CompletionHandler<Integer, Object>() { 
  4.         @Override 
  5.         public void completed(Integer result, Object attachment) { 
  6.             byteBuffer.flip(); 
  7.             byte[] byten = new byte[byteBuffer.limit()]; // 可用的字節(jié)數(shù)量 
  8.             byteBuffer.get(byten, byteBuffer.position(), byteBuffer.limit()); 
  9.             String ret = new String(byten); 
  10.             TcpClientAbility.socketChannel.read(byteBuffer, -1, TimeUnit.SECONDS, null, this); 
  11.         } 
  12.         @Override 
  13.         public void failed(Throwable exc, Object attachment) { 
  14.             TcpClientAbility.socketChannel.read(byteBuffer, -1, TimeUnit.SECONDS, null, this); 
  15.             } 
  16.         }; 
  17.     TcpClientAbility.socketChannel.read(byteBuffer, -1, TimeUnit.SECONDS, null, comphandler); 

六、自定義事件

由官方提供的CommonEventManager通用事件啟發(fā)而來,官方提供了harmonyos系統(tǒng)提供了藍牙、電池、時間、日期等等相關的通用事件,還提供了電池相關的Demo,具體介紹看官方文檔。我這里拿CommonEventManager的CommonEventManager.subscribeCommonEvent()訂閱事件、CommonEventManager.publishCommonEvent()發(fā)布事件給大家看下:

  1. MatchingSkills matchingSkills = new MatchingSkills(); 
  2. matchingSkills.addEvent(SELF_SOCKET_MSG); 
  3. IRemoteObject notifier = data.readRemoteObject(); 
  4. CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills); 
  5. subscriber = new CommonEventSubscriber(subscribeInfo) { 
  6.     @Override 
  7.     public void onReceiveEvent(CommonEventData commonEventData) { 
  8.         //HiLog.info(LABEL_LOG,"socket===shijian" + commonEventData.getData() + ret); 
  9.             } 
  10.         }; 
  11.         try { 
  12.             CommonEventManager.subscribeCommonEvent(subscriber); 
  13.         } catch (RemoteException e) { 
  14.         } 
  1. Intent intent = new Intent(); 
  2. Operation operation = new Intent.OperationBuilder().withAction(TcpClientAbility.SELF_SOCKET_MSG).build(); 
  3. intent.setOperation(operation); 
  4. intent.setParam("msg",ret); 
  5. CommonEventData eventData = new CommonEventData(intent); 
  6. eventData.setData(ret); 
  7. try { 
  8.     CommonEventManager.publishCommonEvent(eventData); 
  9. } catch (RemoteException e) { 
  10.     e.printStackTrace(); 

七、總結

大概思路和所用到的重點知識點在上面以分別列出來了,做完了覺得很簡單,但實際上用一門或多門不怎么熟悉而且相關開發(fā)思路借鑒比較少的開發(fā)框架寫東西時,確實會在動手前很迷茫。覺得迷茫不要退縮,還是那句話,沒有程序解決不了的問題,只有沒思路的程序員,只要想做,就要將整體拆解,化整為零,個個擊破。

本文雖然實現(xiàn)了簡單的多客戶端自由聊天,但還有很多不足,如聊天記錄保存,跳轉(zhuǎn)頁面后回來怎么恢復頁面,websocket、UDP、HTTP、藍牙等通信模式的探索實踐等,不足之處后續(xù)有空繼續(xù)探索不上。有啥不足之處歡迎大家留言,助我改進提升。

文章相關附件可以點擊下面的原文鏈接前往下載

https://harmonyos.51cto.com/resource/1567

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區(qū)
相關推薦

2019-04-29 10:26:49

TCP網(wǎng)絡協(xié)議網(wǎng)絡通信

2022-07-26 14:53:10

WebSocket網(wǎng)絡通信協(xié)議

2021-11-16 09:38:10

鴻蒙HarmonyOS應用

2022-12-01 08:25:23

eTsTCP聊天室

2009-08-24 17:20:13

C#網(wǎng)絡通信TCP連接

2011-12-15 11:11:51

JavaNIO

2023-02-10 08:16:48

WebSocket簡易聊天室

2022-11-14 08:01:48

2019-09-02 10:20:27

TCPIP協(xié)議

2024-02-20 19:53:57

網(wǎng)絡通信協(xié)議

2019-09-25 08:25:49

RPC網(wǎng)絡通信

2015-07-06 10:42:18

PHP聊天室應用

2014-09-16 17:00:02

UDP

2020-07-06 07:52:10

Kubernetes網(wǎng)絡通信

2023-01-13 00:02:41

2023-01-05 09:17:58

2021-02-06 23:26:25

聊天室開發(fā)WebSocket

2022-05-13 10:59:14

容器網(wǎng)絡通信

2024-10-07 10:45:12

2011-06-09 15:44:29

Spring
點贊
收藏

51CTO技術棧公眾號