UNPKG

kitten-cloud-function

Version:

用于编程猫源码云功能(云变量、云列表等)的客户端工具

53 lines (52 loc) 1.94 kB
import { Signal } from "./signal"; export class WebSocketProxy { constructor(argument) { var _a, _b, _c, _d; this.url = argument.url; this.socket = argument; this.beforeSend = new Signal(); this.sended = new Signal(); this.opened = new Signal(); this.received = new Signal(); this.errored = new Signal(); this.closed = new Signal(); const originalSend = this.socket.send; const originalOnOpen = (_a = this.socket.onopen) !== null && _a !== void 0 ? _a : (() => { }); const originalOnMessage = (_b = this.socket.onmessage) !== null && _b !== void 0 ? _b : (() => { }); const originalOnError = (_c = this.socket.onerror) !== null && _c !== void 0 ? _c : (() => { }); const originalOnClose = (_d = this.socket.onclose) !== null && _d !== void 0 ? _d : (() => { }); this.socket.send = (data) => { const message = { data }; this.beforeSend.emit(message); originalSend.call(this.socket, message.data); this.sended.emit(message.data); }; this.socket.onopen = (event) => { // @ts-ignore originalOnOpen.call(this.socket, event); this.opened.emit(event); }; this.socket.onmessage = (event) => { // @ts-ignore originalOnMessage.call(this.socket, event); this.received.emit(event); }; this.socket.onerror = (event) => { // @ts-ignore originalOnError.call(this.socket, event); this.errored.emit(event); }; this.socket.onclose = (event) => { // @ts-ignore originalOnClose.call(this.socket, event); this.closed.emit(event); }; } send(message) { this.socket.send(message); } close() { // @ts-ignore this.socket.close(); } }