HarmonyOS 心跳场景demo

长链接socket demo,心跳场景demo

HarmonyOS
2025-01-10 08:48:49
705浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

udp心跳参考示例代码

import connection from '@ohos.net.connection'
import { BusinessError } from '@kit.BasicServicesKit';
import socket from '@ohos.net.socket';

const udp_server_ip = '192.168.52.119';
const udp_server_port = 8081;
const udp_server: socket.NetAddress = {
  address: udp_server_ip,
  port: udp_server_port
}

const TAG = 'xxx'

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  udp: socket.UDPSocket = socket.constructUDPSocketInstance();
  heartBeatIntervalId: number = -1;
  hasBindLocalAddress: boolean = false;

  bindUdpLocalAddress(): Promise<boolean> {
    let hasDefaultNet = connection.hasDefaultNetSync();
    let netHandle = connection.getDefaultNetSync();
    let properties = connection.getConnectionPropertiesSync(netHandle);
    let linkAddress = properties.linkAddresses.pop();
    if (!hasDefaultNet || !netHandle || !properties || !linkAddress) {
      this.hasBindLocalAddress = false;
      return Promise.resolve(false);
    }
    return this.udp.bind({ address: linkAddress.address.address }).then(() => {
      this.hasBindLocalAddress = true;
      return Promise.resolve(true);
    }).catch((error: BusinessError) => {
      console.log(TAG, 'upd socket bind error, ' + JSON.stringify(error));
      return Promise.reject(error);
    })
  }

  sendHeartBeat() {
    let sendOptions: socket.UDPSendOptions = {
      address: udp_server,
      data: 'HeartBeat'
    }
    this.udp.send(sendOptions, (err: BusinessError) => {
      if (err) {
        console.log(TAG, 'send Heartbeat fail' + JSON.stringify(err));
        return;
      }
      console.log(TAG, 'send Heartbeat success');
    })
  }

  aboutToAppear(): void {
    this.udp.on('message', (value: socket.SocketMessageInfo) => {
      console.log(TAG, "on message");
      let buffer: ArrayBuffer = value.message;
      let dataView = new DataView(buffer);
      let str = "";
      for (let i = 0; i < dataView.byteLength; ++i) {
        str += String.fromCharCode(dataView.getUint8(i));
      }
      console.log(TAG, "on connect received:" + str);
    });
    this.udp.on('listening', () => {
      console.log(TAG, "on listening");
    });
    this.udp.on('close', () => {
      console.log(TAG, "on close");
    });
    this.udp.on('error', () => {
      console.log(TAG, "on error");
    });
    this.heartBeatIntervalId = setInterval(() => {
      if (this.hasBindLocalAddress) {
        this.sendHeartBeat();
      } else {
        this.bindUdpLocalAddress().then((hasBindLocalAddress) => {
          if (hasBindLocalAddress) {
            this.sendHeartBeat();
          }
        })
      }
    }, 5000);
  }

  aboutToDisappear(): void {
    setTimeout(() => {
      if (!this.udp) {
        return;
      }
      this.udp.close().then(() => {
        console.log(TAG, 'close success');
      }).catch((err: BusinessError) => {
        console.log(TAG, 'close fail');
      });
      this.udp.off('message');
      this.udp.off('listening');
      this.udp.off('close');
      this.udp.off('error');
      clearInterval(this.heartBeatIntervalId)
    }, 10 * 1000);
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
分享
微博
QQ
微信
回复
2025-01-10 11:20:13


相关问题
HarmonyOS NFC读、写卡场景demo
795浏览 • 1回复 待解决
HarmonyOS 申请使用webview场景demo
1208浏览 • 1回复 待解决
HarmonyOS 商城demo
608浏览 • 1回复 待解决
HarmonyOS 曲线demo
742浏览 • 1回复 待解决
HarmonyOS 引导页demo
884浏览 • 1回复 待解决
HarmonyOS 帧动画demo
1180浏览 • 1回复 待解决
HarmonyOS 全局弹窗demo
613浏览 • 1回复 待解决
HarmonyOS 滑动缩放demo
549浏览 • 1回复 待解决
HarmonyOS 网络框架demo
719浏览 • 1回复 待解决
HarmonyOS 滤镜实现demo
514浏览 • 1回复 待解决
HarmonyOS Ble蓝牙demo
1075浏览 • 1回复 待解决
HarmonyOS Tab导航demo
646浏览 • 1回复 待解决
HarmonyOS 加解密 demo
1309浏览 • 1回复 待解决
HarmonyOS photopicker的demo
829浏览 • 1回复 待解决
HarmonyOS tab切换demo
644浏览 • 1回复 待解决
HarmonyOS 资讯类demo
938浏览 • 1回复 待解决
HarmonyOS应用更新demo
965浏览 • 1回复 待解决
HarmonyOS viewModal demo问题
817浏览 • 1回复 待解决
HarmonyOS socketio使用demo
780浏览 • 1回复 待解决
HarmonyOS 录制相关demo
676浏览 • 1回复 待解决
HarmonyOS 地区选择demo
640浏览 • 1回复 待解决
HarmonyOS TwoDimensionList Demo答疑
891浏览 • 1回复 待解决