@laplace.live/event-bridge-sdk 0.1.7 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/index.ts +7 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var B;((w)=>{w.DISCONNECTED="disconnected";w.CONNECTING="connecting";w.CONNECTED="connected";w.RECONNECTING="reconnecting"})(B||={});class D{ws=null;eventHandlers=new Map;anyEventHandlers=[];connectionStateHandlers=[];reconnectTimer=null;reconnectAttempts=0;clientId=null;connectionState="disconnected";options={url:"ws://localhost:9696",token:"",reconnect:!0,reconnectInterval:3000,maxReconnectAttempts:10};constructor(k={}){this.options={...this.options,...k}}connect(){return new Promise((k,m)=>{try{if(this.ws)this.ws.close();this.setConnectionState("connecting");let q=this.options.url,A=[];if(this.options.token){A.push("laplace-event-bridge-role-client",this.options.token);let w=new URL(q);w.searchParams.set("token",this.options.token),q=w.toString()}this.ws=new WebSocket(q,A),this.ws.onopen=()=>{this.setConnectionState("connected"),this.reconnectAttempts=0,console.log("Connected to LAPLACE Event Bridge"),k()},this.ws.onmessage=(w)=>{try{let z=JSON.parse(w.data);if(z.type==="established"&&z.clientId)this.clientId=z.clientId,console.log(`Connection established with client ID: ${this.clientId}`);this.processEvent(z)}catch(z){console.error("Failed to parse event data:",z)}},this.ws.onerror=(w)=>{console.error("WebSocket error:",w),m(w)},this.ws.onclose=()=>{if(console.log("Disconnected from LAPLACE Event Bridge"),this.options.reconnect&&this.reconnectAttempts<this.options.maxReconnectAttempts)this.reconnectAttempts++,this.setConnectionState("reconnecting"),console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.options.maxReconnectAttempts})...`),this.reconnectTimer=setTimeout(()=>{this.connect().catch((w)=>{console.error("Reconnection failed:",w)})},this.options.reconnectInterval);else this.setConnectionState("disconnected")}}catch(q){this.setConnectionState("disconnected"),m(q)}})}disconnect(){if(this.reconnectTimer)clearTimeout(this.reconnectTimer),this.reconnectTimer=null;if(this.ws)this.ws.close(),this.ws=null;this.setConnectionState("disconnected"),this.clientId=null}on(k,m){if(!this.eventHandlers.has(k))this.eventHandlers.set(k,[]);this.eventHandlers.get(k).push(m)}onAny(k){this.anyEventHandlers.push(k)}onConnectionStateChange(k){this.connectionStateHandlers.push(k),k(this.connectionState)}off(k,m){if(!this.eventHandlers.has(k))return;let q=this.eventHandlers.get(k),A=q.indexOf(m);if(A!==-1)q.splice(A,1);if(q.length===0)this.eventHandlers.delete(k)}offAny(k){let m=this.anyEventHandlers.indexOf(k);if(m!==-1)this.anyEventHandlers.splice(m,1)}offConnectionStateChange(k){let m=this.connectionStateHandlers.indexOf(k);if(m!==-1)this.connectionStateHandlers.splice(m,1)}isConnectedToBridge(){return this.connectionState==="connected"}getConnectionState(){return this.connectionState}getClientId(){return this.clientId}send(k){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)throw new Error("Not connected to LAPLACE Event Bridge");this.ws.send(JSON.stringify(k))}setConnectionState(k){if(this.connectionState!==k){this.connectionState=k;for(let m of this.connectionStateHandlers)try{m(k)}catch(q){console.error("Error in connection state change handler:",q)}}}processEvent(k){if(this.eventHandlers.has(k.type))for(let m of this.eventHandlers.get(k.type))try{m(k)}catch(q){console.error(`Error in event handler for type ${k.type}:`,q)}for(let m of this.anyEventHandlers)try{m(k)}catch(q){console.error("Error in any event handler:",q)}}}export{D as LaplaceEventBridgeClient,B as ConnectionState};
|
package/index.ts
CHANGED
|
@@ -82,13 +82,19 @@ export class LaplaceEventBridgeClient {
|
|
|
82
82
|
|
|
83
83
|
this.setConnectionState(ConnectionState.CONNECTING)
|
|
84
84
|
|
|
85
|
+
let url = this.options.url
|
|
85
86
|
const protocols: string[] = []
|
|
86
87
|
if (this.options.token) {
|
|
87
88
|
// Add the token as the second protocol parameter
|
|
88
89
|
protocols.push('laplace-event-bridge-role-client', this.options.token)
|
|
90
|
+
|
|
91
|
+
// Also add token as a query parameter
|
|
92
|
+
const urlObj = new URL(url)
|
|
93
|
+
urlObj.searchParams.set('token', this.options.token)
|
|
94
|
+
url = urlObj.toString()
|
|
89
95
|
}
|
|
90
96
|
|
|
91
|
-
this.ws = new WebSocket(
|
|
97
|
+
this.ws = new WebSocket(url, protocols)
|
|
92
98
|
|
|
93
99
|
this.ws.onopen = () => {
|
|
94
100
|
this.setConnectionState(ConnectionState.CONNECTED)
|