improve websocket params and auth logics

This commit is contained in:
SrGooglo 2023-05-18 19:48:54 +00:00
parent db89cce639
commit 7357e3b5c6

View File

@ -5,7 +5,7 @@ import { io } from "socket.io-client"
import remotes from "./remotes" import remotes from "./remotes"
import request from "./handlers/request" //import request from "./handlers/request"
import Storage from "./helpers/withStorage" import Storage from "./helpers/withStorage"
import SessionModel from "./models/session" import SessionModel from "./models/session"
@ -25,10 +25,11 @@ if (globalThis.isServerMode) {
} }
export default function createClient({ export default function createClient({
wsEvents = Object(),
useWs = false,
accessKey = null, accessKey = null,
privateKey = null, privateKey = null,
enableWs = false,
wsEvents = Object(),
wsParams = Object(),
} = {}) { } = {}) {
const sharedState = globalThis.__comty_shared_state = { const sharedState = globalThis.__comty_shared_state = {
onExpiredExceptionEvent: false, onExpiredExceptionEvent: false,
@ -54,12 +55,25 @@ export default function createClient({
baseURL: remote.origin, baseURL: remote.origin,
}) })
if (useWs && remote.hasWebsocket) { if (enableWs && remote.hasWebsocket) {
sharedState.wsInstances[key] = io(remote.wsOrigin ?? remote.origin, { let opts = {
transports: ["websocket"], transports: ["websocket"],
autoConnect: true, autoConnect: remote.autoConnect ?? true,
...remote.wsParams ?? {}, ...remote.wsParams ?? {},
}) }
if (wsParams[key]) {
if (typeof wsParams[key] === "function") {
opts = wsParams[key](opts)
} else {
opts = {
...opts,
...wsParams[key],
}
}
}
sharedState.wsInstances[key] = io(remote.wsOrigin ?? remote.origin, opts)
} }
} }
@ -68,9 +82,9 @@ export default function createClient({
const ws = sharedState.wsInstances[key] const ws = sharedState.wsInstances[key]
ws.on("connect", () => { ws.on("connect", () => {
console.log(`[WS-API][${key}] Connected`) console.debug(`[WS-API][${key}] Connected`)
if (remotes[key].needsAuth) { if (remotes[key].useClassicAuth) {
// try to auth // try to auth
ws.emit("authenticate", { ws.emit("authenticate", {
token: SessionModel.token, token: SessionModel.token,
@ -87,7 +101,7 @@ export default function createClient({
}) })
ws.onAny((event, ...args) => { ws.onAny((event, ...args) => {
console.log(`[WS-API][${key}] Event recived`, event, ...args) console.debug(`[WS-API][${key}] Event recived`, event, ...args)
}) })
const customEvents = wsEvents[key] const customEvents = wsEvents[key]