kitten-cloud-function
Version:
用于编程猫源码云功能(云变量、云列表等)的客户端工具
57 lines (56 loc) • 2.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSocketProxy = void 0;
const signal_1 = require("./signal");
class WebSocketProxy {
constructor(argument) {
var _a, _b, _c, _d;
this.url = argument.url;
this.socket = argument;
this.beforeSend = new signal_1.Signal();
this.sended = new signal_1.Signal();
this.opened = new signal_1.Signal();
this.received = new signal_1.Signal();
this.errored = new signal_1.Signal();
this.closed = new signal_1.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();
}
}
exports.WebSocketProxy = WebSocketProxy;