Update: 更新WebSocket模式

This commit is contained in:
王立帮
2024-12-02 08:00:20 +08:00
parent 9bbd0e6720
commit 4008e1aab5
8 changed files with 208 additions and 226 deletions

View File

@@ -1,6 +1,36 @@
goog.loadJs('web', () => {
goog.require('io');
goog.require('Mixly');
goog.provide('Mixly.WebSocket');
class WebSocket {
#socket_ = null;
constructor(path, option) {
this.#socket_ = io(path, option);
}
emit(eventName, ...args) {
if (this.isConnected()) {
return this.#socket_.emit(eventName, ...args);
} else {
const callback = args.pop();
callback({
error: new Error('socket is not connected')
});
}
}
getSocket() {
return this.#socket_;
}
isConnected() {
return this.#socket_?.connected;
}
}
Mixly.WebSocket = WebSocket;
});