mirror of
https://gitee.com/xiongmao1988/rax-medical.git
synced 2025-08-25 13:34:58 +08:00
46 lines
801 B
TypeScript
46 lines
801 B
TypeScript
![]() |
let ws: WebSocket;
|
||
|
|
||
|
function init(url: string | URL, protocols?: string | string[]) {
|
||
|
return new Promise(resolve => {
|
||
|
try {
|
||
|
ws = new WebSocket(url);
|
||
|
resolve(true)
|
||
|
} catch (e) {
|
||
|
console.log(e)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
function close(code?: number, reason?: string) {
|
||
|
ws.close(code, reason)
|
||
|
}
|
||
|
|
||
|
function send(data: string | ArrayBufferLike | Blob | ArrayBufferView) {
|
||
|
ws.send(data)
|
||
|
}
|
||
|
|
||
|
function closeListener(cb: any) {
|
||
|
ws.onclose = cb
|
||
|
}
|
||
|
|
||
|
function openListener(cb: any) {
|
||
|
ws.onopen = cb
|
||
|
}
|
||
|
|
||
|
function errorListener(cb: any) {
|
||
|
ws.onerror = cb
|
||
|
}
|
||
|
|
||
|
function messageListener(cb: any) {
|
||
|
ws.onmessage = cb
|
||
|
}
|
||
|
|
||
|
export const wsApi= {
|
||
|
init,
|
||
|
close,
|
||
|
send,
|
||
|
closeListener,
|
||
|
openListener,
|
||
|
errorListener,
|
||
|
messageListener
|
||
|
}
|