mirror of
https://github.com/ragestudio/comty.js.git
synced 2025-06-08 18:14:18 +00:00
use linebridge-client
This commit is contained in:
parent
d767d3c842
commit
3a75a8bb11
11
package.json
11
package.json
@ -1,10 +1,9 @@
|
||||
{
|
||||
"name": "comty.js",
|
||||
"version": "0.61.0",
|
||||
"version": "0.61.1",
|
||||
"main": "./dist/index.js",
|
||||
"author": "RageStudio <support@ragestudio.net>",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
16
src/index.js
16
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
|
||||
|
131
src/rtclient.js
131
src/rtclient.js
@ -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
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user