diff --git a/package.json b/package.json index bc49a7a..0c46559 100755 --- a/package.json +++ b/package.json @@ -1,10 +1,9 @@ { "name": "comty.js", - "version": "0.61.0", + "version": "0.61.1", "main": "./dist/index.js", "author": "RageStudio ", "scripts": { - "test": "ava", "build": "hermes build" }, "files": [ @@ -13,15 +12,15 @@ "license": "MIT", "dependencies": { "@foxify/events": "^2.1.0", - "axios": "^1.4.0", + "axios": "^1.8.4", "js-cookie": "^3.0.5", "jsonwebtoken": "^9.0.0", "jwt-decode": "^4.0.0", - "luxon": "^3.3.0", - "socket.io-client": "^4.6.1" + "linebridge-client": "^0.2.0", + "luxon": "^3.6.0", + "socket.io-client": "^4.8.1" }, "devDependencies": { - "ava": "^6.1.2", "@ragestudio/hermes": "^1.0.0" } } diff --git a/src/index.js b/src/index.js index fe7ead8..19acf4b 100755 --- a/src/index.js +++ b/src/index.js @@ -30,7 +30,10 @@ if (globalThis.isServerMode) { export function createClient({ accessKey = null, privateKey = null, - enableWs = false, + ws = { + enable: false, + autoConnect: false, + }, origin = Remotes.origin, eventBus = new EventEmitter(), } = {}) { @@ -72,9 +75,14 @@ export function createClient({ return config }) - if (enableWs == true) { - __comty_shared_state.ws = new WebsocketManager() - sharedState.ws.connectAll() + if (typeof ws === "object") { + if (ws.enable === true) { + __comty_shared_state.ws = new WebsocketManager() + + if (ws.autoConnect === true) { + sharedState.ws.connectAll() + } + } } return sharedState diff --git a/src/rtclient.js b/src/rtclient.js deleted file mode 100644 index 6d06bee..0000000 --- a/src/rtclient.js +++ /dev/null @@ -1,131 +0,0 @@ -export class RTEngineClient { - constructor(params = {}) { - this.params = params - } - - socket = null - - stateSubscribers = [] - - joinedTopics = new Set() - handlers = new Set() - - async connect() { - return new Promise((resolve, reject) => { - if (this.socket) { - this.disconnect() - } - - let url = `${this.params.url}` - - if (this.params.token) { - url += `?token=${this.params.token}` - } - - this.socket = new WebSocket(url) - - this.socket.onopen = () => { - resolve() - this._emit("connect") - } - this.socket.onclose = () => { - this._emit("disconnect") - } - this.socket.onerror = () => { - reject() - this._emit("error") - } - this.socket.onmessage = (event) => this.handleMessage(event) - }) - } - - async disconnect() { - if (!this.socket) { - return false - } - - for await (const topic of this.joinedTopics) { - this.leaveTopic(topic) - } - - this.socket.close() - this.socket = null - } - - _emit(event, data) { - for (const handler of this.handlers) { - if (handler.event === event) { - handler.handler(data) - } - } - } - - on = (event, handler) => { - this.handlers.add({ - event, - handler, - }) - } - - off = (event, handler) => { - this.handlers.delete({ - event, - handler, - }) - } - - emit = (event, data) => { - if (!this.socket) { - throw new Error("Failed to send, socket not connected") - } - - this.socket.send(JSON.stringify({ event, data })) - } - - joinTopic = (topic) => { - this.emit("topic:join", topic) - this.joinedTopics.add(topic) - } - - leaveTopic = (topic) => { - this.emit("topic:leave", topic) - this.joinedTopics.delete(topic) - } - - updateState(state) { - this.stateSubscribers.forEach((callback) => callback(state)) - } - - //* HANDLERS - handleMessage(event) { - try { - const payload = JSON.parse(event.data) - - if (typeof payload.event !== "string") { - return false - } - - if (payload.event === "error") { - console.error(payload.data) - return false - } - - this._emit(payload.event, payload.data) - } catch (error) { - console.error("Error handling message:", error) - } - } - - // UPDATERS - onStateChange(callback) { - this.stateSubscribers.push(callback) - - return () => { - this.stateSubscribers = this.stateSubscribers.filter( - (cb) => cb !== callback, - ) - } - } -} - -export default RTEngineClient diff --git a/src/ws.js b/src/ws.js index 8d0b719..5a59ada 100644 --- a/src/ws.js +++ b/src/ws.js @@ -2,7 +2,8 @@ import Remotes from "./remotes" import Storage from "./helpers/withStorage" import { io } from "socket.io-client" -import RTClient from "./rtclient" +//import { RTEngineClient } from "linebridge-client" +import { RTEngineClient } from "../../linebridge/client/src" class WebsocketManager { sockets = new Map() @@ -54,7 +55,7 @@ class WebsocketManager { remote, ) - const client = new RTClient({ + const client = new RTEngineClient({ url: `${Remotes.origin}/${remote.namespace}`, token: Storage.engine.get("token"), }) @@ -119,6 +120,10 @@ class WebsocketManager { await this.connect(remote) } } catch (error) { + console.error( + `Failed to connect to [${remote.namespace}]:`, + error, + ) globalThis.__comty_shared_state.eventBus.emit( `wsmanager:${remote.namespace}:error`, error,