import webSocket from '@ohos.net.webSocket';
class SocketHarmony {
onopen: () => void;
onmessage: () => void;
onclose: () => void;
onerror: () => void;
ws: webSocket.WebSocket;
constructor(serverURL) {
this.onopen = () => {};
this.onmessage = () => {};
this.onclose = () => {};
this.onerror = () => {};
this.ws = webSocket.createWebSocket();
this.ws.on('open', (err, value) => {
this.onopen();
})
this.ws.on('message', (err, msg) => {
this.onmessage(msg);
})
this.ws.on('close', (err, event) => {
this.onclose(event);
});
this.ws.on('error', (error) => {
this.onerror(error);
});
this.ws.connect(serverURL);
}
send(data) {
this.ws.send(data)
}
close() {
this.ws.close();
}
}
export default SocketHarmony;