Files
mixly3/common/modules/mixly-modules/web-socket/web-socket.js
2024-12-02 08:00:20 +08:00

36 lines
682 B
JavaScript

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;
});