use linebridge-client

This commit is contained in:
SrGooglo 2025-03-26 10:57:57 +00:00
parent d767d3c842
commit 3a75a8bb11
4 changed files with 24 additions and 143 deletions

View File

@ -1,10 +1,9 @@
{ {
"name": "comty.js", "name": "comty.js",
"version": "0.61.0", "version": "0.61.1",
"main": "./dist/index.js", "main": "./dist/index.js",
"author": "RageStudio <support@ragestudio.net>", "author": "RageStudio <support@ragestudio.net>",
"scripts": { "scripts": {
"test": "ava",
"build": "hermes build" "build": "hermes build"
}, },
"files": [ "files": [
@ -13,15 +12,15 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@foxify/events": "^2.1.0", "@foxify/events": "^2.1.0",
"axios": "^1.4.0", "axios": "^1.8.4",
"js-cookie": "^3.0.5", "js-cookie": "^3.0.5",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"jwt-decode": "^4.0.0", "jwt-decode": "^4.0.0",
"luxon": "^3.3.0", "linebridge-client": "^0.2.0",
"socket.io-client": "^4.6.1" "luxon": "^3.6.0",
"socket.io-client": "^4.8.1"
}, },
"devDependencies": { "devDependencies": {
"ava": "^6.1.2",
"@ragestudio/hermes": "^1.0.0" "@ragestudio/hermes": "^1.0.0"
} }
} }

View File

@ -30,7 +30,10 @@ if (globalThis.isServerMode) {
export function createClient({ export function createClient({
accessKey = null, accessKey = null,
privateKey = null, privateKey = null,
enableWs = false, ws = {
enable: false,
autoConnect: false,
},
origin = Remotes.origin, origin = Remotes.origin,
eventBus = new EventEmitter(), eventBus = new EventEmitter(),
} = {}) { } = {}) {
@ -72,9 +75,14 @@ export function createClient({
return config return config
}) })
if (enableWs == true) { if (typeof ws === "object") {
__comty_shared_state.ws = new WebsocketManager() if (ws.enable === true) {
sharedState.ws.connectAll() __comty_shared_state.ws = new WebsocketManager()
if (ws.autoConnect === true) {
sharedState.ws.connectAll()
}
}
} }
return sharedState return sharedState

View File

@ -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

View File

@ -2,7 +2,8 @@ import Remotes from "./remotes"
import Storage from "./helpers/withStorage" import Storage from "./helpers/withStorage"
import { io } from "socket.io-client" import { io } from "socket.io-client"
import RTClient from "./rtclient" //import { RTEngineClient } from "linebridge-client"
import { RTEngineClient } from "../../linebridge/client/src"
class WebsocketManager { class WebsocketManager {
sockets = new Map() sockets = new Map()
@ -54,7 +55,7 @@ class WebsocketManager {
remote, remote,
) )
const client = new RTClient({ const client = new RTEngineClient({
url: `${Remotes.origin}/${remote.namespace}`, url: `${Remotes.origin}/${remote.namespace}`,
token: Storage.engine.get("token"), token: Storage.engine.get("token"),
}) })
@ -119,6 +120,10 @@ class WebsocketManager {
await this.connect(remote) await this.connect(remote)
} }
} catch (error) { } catch (error) {
console.error(
`Failed to connect to [${remote.namespace}]:`,
error,
)
globalThis.__comty_shared_state.eventBus.emit( globalThis.__comty_shared_state.eventBus.emit(
`wsmanager:${remote.namespace}:error`, `wsmanager:${remote.namespace}:error`,
error, error,