中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
socket的话,在鸿蒙next中其实只需要几句代码就好了,如下直接复制就能使用: ```javascript import { webSocket } from '@kit.NetworkKit'; import { BusinessError } from '@kit.BasicServicesKit'; let defaultIpAddress = "ws://"; let ws = webSocket.createWebSocket(); ws.on('open', (err: BusinessError, value: Object) => { console.log("11111:" + JSON.stringify(value)); ws.send("3", (err: BusinessError, value: boolean) => { if (!err) { console.log("success"); } else { console.log(JSON.stringify(err)); } }); }); ws.on('message', (err: BusinessError, value: string | ArrayBuffer) => { console.log("on message, message:" + value); if (value === 'bye') { ws.close((err: BusinessError, value: boolean) => { if (!err) { console.log("success"); } else { console.log(JSON.stringify(err)); } }); } }); ws.on('close', (err: BusinessError, value: webSocket.CloseResult) => { console.log("value.code + ", reason is " + value.reason); }); ws.on('error', (err: BusinessError) => { console.log(JSON.stringify(err)); }); ws.connect(defaultIpAddress, (err: BusinessError, value: boolean) => { if (!err) { console.log("success"); } else { console.log(JSON.stringify(err)); } });