uts android 插件中(uni_modules/uts-udp/utssdk/app-android/index.uts)
export class UDPMulticast {
....
listener(receive:UTSCallback) {
...
this.socket = new MulticastSocket(this.port);
const address = InetAddress.getByName(this.group);
this.socket?.joinGroup(address);
const buff = new ByteArray(1024);
const packet = new DatagramPacket(buff, buff.size, address, this.port);
setTimeout(()=>{
console.log(111);
if (this.socket == null) return
try {
this.socket?.receive(packet)
const msg = new String(buff, 0, packet.getLength())
console.log('msg', msg);
receive(msg)
} catch (e) {
console.log('e',e);
}
},100)
}
.....
}
vue页面中(pages/index/index.vue)
<template>
<button @click="initUdp">开启UDP</button>
<button @click="test">测试</button>
</template>
import { UDPMulticast } from '../../uni_modules/uts-udp'
export default {
...
methods:{
initUdp(){
this.server = new UDPMulticast(MULTICAST_IP, MULTICAST_PORT)
this.server.listener((res) => {
console.log("res",res);
})
},
test(){
console.log(‘test');
}
}
}
0 个回复