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

【開發(fā)實(shí)錄】在鴻蒙開發(fā)板上使用Websocket(移植自librws庫)

系統(tǒng)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com/#zz

[[353869]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.51cto.com/#zz

 librws: Tiny, cross platform websocket client C library

相關(guān)代碼可在下面下載,也可進(jìn)入librws: 將librws移植到鴻蒙Hi_3861開發(fā)板上 (gitee.com)查看

技術(shù)有限,如有錯(cuò)誤還望不吝賜教。

基礎(chǔ):完成官方的快速入門教程

雖然在web雙向通信中,除了使用ajax(單向輪詢)外,一般都推薦websocket,但網(wǎng)上關(guān)于websocket的實(shí)現(xiàn)代碼很少,而且大部分為js和java,很少有C語言。因?yàn)檎n程項(xiàng)目開發(fā)需要使用到websocket,就在網(wǎng)上尋找了挺久的,終于找到了這個(gè)庫。相對(duì)于其他庫來說,這個(gè)庫需要跨平臺(tái)的代碼都用了一層適配層,而且沒有用到其他例如libevent等其他庫,用他的話說就是"No additional dependecies",所以移植挺方便的。然后因?yàn)檫@個(gè)庫除了windows平臺(tái)外,其他平臺(tái)都是使用POSIX接口,而Hi_3861上只有POSIX的聲明,但并沒有實(shí)現(xiàn)(詳細(xì)可看【開發(fā)實(shí)錄】在Hi3861開發(fā)板上創(chuàng)建線程(三種方式)-鴻蒙HarmonyOS技術(shù)社區(qū)-官方戰(zhàn)略合作伙伴-51CTO.COM里面有所說明)。所以適配相關(guān)的函數(shù)以及處理一些小細(xì)節(jié)。

這篇文章主要介紹如何使用,下一篇可能會(huì)介紹如何移植,大家有需求的話,可先學(xué)習(xí)連志安老師的如何在鴻蒙系統(tǒng)中移植 Paho-MQTT 實(shí)現(xiàn)MQTT協(xié)議-鴻蒙HarmonyOS技術(shù)社區(qū)-官方戰(zhàn)略合作伙伴-51CTO.COM文章,這篇文章有挺大的參考價(jià)值的。

1、向項(xiàng)目中導(dǎo)入此庫

下載下面的librws資源放至//third_party目錄下,然后在//vendor/hisi/hi3861/hi3861/BUILD.gn文件中的lite_component("sdk")-->deps下添加 "//third_party/librws:librws_static" 這樣就可以在下次編譯時(shí)將相關(guān)代碼編譯成庫放進(jìn)項(xiàng)目內(nèi)了。


2、編寫應(yīng)用代碼

先使用板子連接上wifi,具體操作可查看Hi3861 WiFi操作,熱點(diǎn)連接-鴻蒙HarmonyOS技術(shù)社區(qū)-官方戰(zhàn)略合作伙伴-51CTO.COM,需要注意,連接WiFi后要sleep一段時(shí)間。下面資源有相關(guān)代碼。

然后配置相關(guān)信息:

  1. rws_socket _socket = NULL
  2.    _socket = rws_socket_create(); 
  3.     
  4.    rws_socket_set_scheme(_socket, "ws"); 
  5.    rws_socket_set_host(_socket, "192.168.1.103"); 
  6.    rws_socket_set_port(_socket, 7777); 
  7.    rws_socket_set_path(_socket, "/ws"); 

 注意配置信息一定要對(duì)應(yīng)真實(shí)信息,下面我會(huì)分享我的websocket服務(wù)端測試代碼。

然后配置回調(diào)函數(shù):

  1. // 因主動(dòng)或者出現(xiàn)錯(cuò)誤時(shí),斷開連接的回調(diào)函數(shù) 
  2. static void on_socket_disconnected(rws_socket socket) { 
  3.     // process error 
  4.     rws_error error = rws_socket_get_error(socket); 
  5.     if (error) {  
  6.         printf("Socket disconnect with code, error: %i, %s\n", rws_error_get_code(error), rws_error_get_description(error));  
  7.     } 
  8.     // forget about this socket object, due to next disconnection sequence 
  9.     socket = NULL
  10.  
  11. // 建立鏈接完成后的回調(diào)函數(shù) 
  12. static void on_socket_connected(rws_socket socket) { 
  13.     (void) socket; 
  14.     printf("Socket connected\n"); 
  15.  
  16. // 接受到信息的回調(diào)函數(shù) 
  17. static void on_socket_received_text(rws_socket socket, const char * text, const unsigned int length) { 
  18.     (void) socket; 
  19.     char *buff[128] = {0}; 
  20.     memcpy_s(buff, 128, text, length); 
  21.     printf("Socket text: %s\n", buff); 

  1. // 設(shè)置回調(diào)函數(shù) 
  2.    rws_socket_set_on_disconnected(_socket, &on_socket_disconnected); // required 
  3.    rws_socket_set_on_connected(_socket, &on_socket_connected); 
  4.    rws_socket_set_on_received_text(_socket, &on_socket_received_text); 

 需要注意一個(gè)地方,在接受信息回調(diào)函數(shù)中,原倉庫的測試代碼是直接打印text,但測試發(fā)現(xiàn),text變量除了接受到的信息還會(huì)包括一部分亂碼,所以使用了memcpy_s函數(shù)復(fù)制有效的信息在打印。大家編寫接受回調(diào)函數(shù)時(shí)需要注意這個(gè)地方。

接下來就是連接,發(fā)送消息以及關(guān)閉了。

  1. printf("[RWS]ready to connect\n"); 
  2.  rws_bool res = rws_socket_connect(_socket); 
  3.  if(res == rws_false) { 
  4.      printf("[RWS]error connect\n"); 
  5.      exit(1); 
  6.  } 
  7.  sleep(2); 
  8.  const char * example_text = "hello world"
  9.  printf("[RWS]ready to send msg\n"); 
  10.  rws_socket_send_text(_socket, example_text); 
  11.  sleep(2); 
  12.  printf("[RWS]ready to disconnect\n"); 
  13.  rws_socket_disconnect_and_release(_socket); 
  14.  _socket = NULL
  15.  return

 3、測試代碼

服務(wù)器代碼使用的是golang,詳細(xì)代碼在下方下載。在代碼文件當(dāng)前目錄運(yùn)行命令:

  1. go run server.go 

4、其他文章

【開發(fā)實(shí)錄】在Hi3861開發(fā)板上發(fā)送一個(gè)get請(qǐng)求

【開發(fā)實(shí)錄】在Hi3861開發(fā)板上創(chuàng)建線程(三種方式)

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.51cto.com/#zz

 

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

2020-10-30 17:12:05

Hi3861

2020-10-30 09:48:03

Hi3861開發(fā)板get請(qǐng)求

2022-01-07 21:11:27

鴻蒙HarmonyOS應(yīng)用

2020-12-29 09:59:01

鴻蒙HarmonyOS智能家居

2022-06-06 10:44:10

C++語言鴻蒙

2020-12-16 10:05:48

鴻蒙開發(fā)板Onenet平臺(tái)

2021-12-16 15:14:54

鴻蒙HarmonyOS應(yīng)用

2020-12-07 12:34:33

開發(fā)板鴻蒙hello world

2011-06-15 14:19:11

QT 移植

2020-11-17 12:15:36

MQTT開發(fā)

2017-09-17 12:21:52

移植Linux系統(tǒng)sqlite3

2020-10-27 10:00:26

鴻蒙開發(fā)板物聯(lián)網(wǎng)

2020-11-19 10:25:24

MQTT

2020-12-08 12:36:41

Hi3861 ADC驅(qū)動(dòng)開發(fā)

2021-12-29 16:11:11

鴻蒙HarmonyOS應(yīng)用

2020-11-18 11:56:06

鴻蒙Hi3861

2020-10-20 09:32:43

HiSparkWi-FiIoT

2022-04-01 15:54:01

DHCP網(wǎng)絡(luò)協(xié)議開發(fā)板

2021-12-30 16:12:07

鴻蒙HarmonyOS應(yīng)用

2020-10-27 10:08:19

Hi3861ADC驅(qū)動(dòng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)