@laplace.live/event-bridge-sdk 0.0.2 → 0.0.5
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/README.md +31 -36
- package/dist/index.js +1 -1
- package/index.ts +22 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -6,13 +6,13 @@ A TypeScript SDK for connecting to the LAPLACE Event Bridge server. This SDK all
|
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
# Using npm
|
|
9
|
-
npm install @laplace.live/event-bridge
|
|
9
|
+
npm install @laplace.live/event-bridge-sdk
|
|
10
10
|
|
|
11
11
|
# Using Yarn
|
|
12
|
-
yarn add @laplace.live/event-bridge
|
|
12
|
+
yarn add @laplace.live/event-bridge-sdk
|
|
13
13
|
|
|
14
14
|
# Using Bun
|
|
15
|
-
bun add @laplace.live/event-bridge
|
|
15
|
+
bun add @laplace.live/event-bridge-sdk
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Usage
|
|
@@ -20,49 +20,44 @@ bun add @laplace.live/event-bridge
|
|
|
20
20
|
### Basic Usage
|
|
21
21
|
|
|
22
22
|
```typescript
|
|
23
|
-
import {
|
|
24
|
-
import type { Message, System } from '@laplace.live/event-types';
|
|
23
|
+
import { LaplaceEventBridgeClient } from '@laplace.live/event-bridge-sdk'
|
|
25
24
|
|
|
26
25
|
// Create a new client
|
|
27
|
-
const client = new
|
|
26
|
+
const client = new LaplaceEventBridgeClient({
|
|
28
27
|
url: 'ws://localhost:9696',
|
|
29
28
|
token: 'your-auth-token', // Optional
|
|
30
29
|
reconnect: true,
|
|
31
|
-
})
|
|
30
|
+
})
|
|
32
31
|
|
|
33
32
|
// Connect to the bridge
|
|
34
|
-
await client.connect()
|
|
33
|
+
await client.connect()
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
console.log(`${event.username}: ${event.message}`);
|
|
40
|
-
});
|
|
35
|
+
client.on('message', event => {
|
|
36
|
+
console.log(`${event.username}: ${event.message}`)
|
|
37
|
+
})
|
|
41
38
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
console.log(`System: ${event.message}`);
|
|
46
|
-
});
|
|
39
|
+
client.on('system', event => {
|
|
40
|
+
console.log(`System: ${event.message}`)
|
|
41
|
+
})
|
|
47
42
|
|
|
48
43
|
// Listen for any event
|
|
49
|
-
client.onAny(
|
|
50
|
-
console.log(`Received event of type: ${event.type}`)
|
|
51
|
-
})
|
|
44
|
+
client.onAny(event => {
|
|
45
|
+
console.log(`Received event of type: ${event.type}`)
|
|
46
|
+
})
|
|
52
47
|
|
|
53
48
|
// Disconnect when done
|
|
54
|
-
client.disconnect()
|
|
49
|
+
client.disconnect()
|
|
55
50
|
```
|
|
56
51
|
|
|
57
52
|
### Connection Options
|
|
58
53
|
|
|
59
54
|
```typescript
|
|
60
55
|
interface ConnectionOptions {
|
|
61
|
-
url?: string
|
|
62
|
-
token?: string
|
|
63
|
-
reconnect?: boolean
|
|
64
|
-
reconnectInterval?: number
|
|
65
|
-
maxReconnectAttempts?: number
|
|
56
|
+
url?: string // WebSocket URL, default: 'ws://localhost:9696'
|
|
57
|
+
token?: string // Authentication token, default: ''
|
|
58
|
+
reconnect?: boolean // Auto reconnect on disconnect, default: true
|
|
59
|
+
reconnectInterval?: number // Milliseconds between reconnect attempts, default: 3000
|
|
60
|
+
maxReconnectAttempts?: number // Maximum reconnect attempts, default: 10
|
|
66
61
|
}
|
|
67
62
|
```
|
|
68
63
|
|
|
@@ -88,23 +83,23 @@ For example:
|
|
|
88
83
|
|
|
89
84
|
```typescript
|
|
90
85
|
// This will infer that 'event' is of type 'Message'
|
|
91
|
-
client.on('message',
|
|
86
|
+
client.on('message', event => {
|
|
92
87
|
// TypeScript knows these properties exist
|
|
93
|
-
console.log(event.username)
|
|
94
|
-
console.log(event.message)
|
|
95
|
-
})
|
|
88
|
+
console.log(event.username)
|
|
89
|
+
console.log(event.message)
|
|
90
|
+
})
|
|
96
91
|
|
|
97
92
|
// This will infer that 'event' is of type 'Gift'
|
|
98
|
-
client.on('gift',
|
|
93
|
+
client.on('gift', event => {
|
|
99
94
|
// TypeScript knows these properties exist
|
|
100
|
-
console.log(event.giftName)
|
|
101
|
-
console.log(event.giftCount)
|
|
102
|
-
})
|
|
95
|
+
console.log(event.giftName)
|
|
96
|
+
console.log(event.giftCount)
|
|
97
|
+
})
|
|
103
98
|
```
|
|
104
99
|
|
|
105
100
|
## Event Types
|
|
106
101
|
|
|
107
|
-
Event types are imported from `@laplace.live/event-types` package. See the [documentation](https://chat.laplace.live/event-types
|
|
102
|
+
Event types are imported from `@laplace.live/event-types` package. See the [documentation](https://chat.laplace.live/event-types) for available event types.
|
|
108
103
|
|
|
109
104
|
## License
|
|
110
105
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class A{ws=null;eventHandlers=new Map;anyEventHandlers=[];reconnectTimer=null;reconnectAttempts=0;clientId=null;isConnected=!1;options={url:"ws://localhost:9696",token:"",reconnect:!0,reconnectInterval:3000,maxReconnectAttempts:10};constructor(k={}){this.options={...this.options,...k}}connect(){return new Promise((k,q)=>{try{if(this.ws)this.ws.close();let u=[];if(this.options.token)u.push("laplace-event-bridge-role-client",this.options.token);this.ws=new WebSocket(this.options.url,u),this.ws.onopen=()=>{this.isConnected=!0,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),q(w)},this.ws.onclose=()=>{if(this.isConnected=!1,console.log("Disconnected from LAPLACE Event Bridge"),this.options.reconnect&&this.reconnectAttempts<this.options.maxReconnectAttempts)this.reconnectAttempts++,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)}}catch(u){q(u)}})}disconnect(){if(this.reconnectTimer)clearTimeout(this.reconnectTimer),this.reconnectTimer=null;if(this.ws)this.ws.close(),this.ws=null;this.isConnected=!1,this.clientId=null}on(k,q){if(!this.eventHandlers.has(k))this.eventHandlers.set(k,[]);this.eventHandlers.get(k).push(q)}onAny(k){this.anyEventHandlers.push(k)}off(k,q){if(!this.eventHandlers.has(k))return;let u=this.eventHandlers.get(k),w=u.indexOf(q);if(w!==-1)u.splice(w,1);if(u.length===0)this.eventHandlers.delete(k)}offAny(k){let q=this.anyEventHandlers.indexOf(k);if(q!==-1)this.anyEventHandlers.splice(q,1)}isConnectedToBridge(){return this.isConnected}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))}processEvent(k){if(this.eventHandlers.has(k.type))for(let q of this.eventHandlers.get(k.type))try{q(k)}catch(u){console.error(`Error in event handler for type ${k.type}:`,u)}for(let q of this.anyEventHandlers)try{q(k)}catch(u){console.error("Error in any event handler:",u)}}}export{A as
|
|
1
|
+
class A{ws=null;eventHandlers=new Map;anyEventHandlers=[];reconnectTimer=null;reconnectAttempts=0;clientId=null;isConnected=!1;options={url:"ws://localhost:9696",token:"",reconnect:!0,reconnectInterval:3000,maxReconnectAttempts:10};constructor(k={}){this.options={...this.options,...k}}connect(){return new Promise((k,q)=>{try{if(this.ws)this.ws.close();let u=[];if(this.options.token)u.push("laplace-event-bridge-role-client",this.options.token);this.ws=new WebSocket(this.options.url,u),this.ws.onopen=()=>{this.isConnected=!0,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),q(w)},this.ws.onclose=()=>{if(this.isConnected=!1,console.log("Disconnected from LAPLACE Event Bridge"),this.options.reconnect&&this.reconnectAttempts<this.options.maxReconnectAttempts)this.reconnectAttempts++,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)}}catch(u){q(u)}})}disconnect(){if(this.reconnectTimer)clearTimeout(this.reconnectTimer),this.reconnectTimer=null;if(this.ws)this.ws.close(),this.ws=null;this.isConnected=!1,this.clientId=null}on(k,q){if(!this.eventHandlers.has(k))this.eventHandlers.set(k,[]);this.eventHandlers.get(k).push(q)}onAny(k){this.anyEventHandlers.push(k)}off(k,q){if(!this.eventHandlers.has(k))return;let u=this.eventHandlers.get(k),w=u.indexOf(q);if(w!==-1)u.splice(w,1);if(u.length===0)this.eventHandlers.delete(k)}offAny(k){let q=this.anyEventHandlers.indexOf(k);if(q!==-1)this.anyEventHandlers.splice(q,1)}isConnectedToBridge(){return this.isConnected}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))}processEvent(k){if(this.eventHandlers.has(k.type))for(let q of this.eventHandlers.get(k.type))try{q(k)}catch(u){console.error(`Error in event handler for type ${k.type}:`,u)}for(let q of this.anyEventHandlers)try{q(k)}catch(u){console.error("Error in any event handler:",u)}}}export{A as LaplaceEventBridgeClient};
|
package/index.ts
CHANGED
|
@@ -10,14 +10,35 @@ export type EventTypeHandler = (event: LaplaceEvent) => void
|
|
|
10
10
|
export type AnyEventHandler = (event: LaplaceEvent) => void
|
|
11
11
|
|
|
12
12
|
export interface ConnectionOptions {
|
|
13
|
+
/**
|
|
14
|
+
* The URL of the LAPLACE Event Bridge server
|
|
15
|
+
*
|
|
16
|
+
* @example 'ws://localhost:9696'
|
|
17
|
+
*/
|
|
13
18
|
url?: string
|
|
19
|
+
/**
|
|
20
|
+
* The authentication token for the LAPLACE Event Bridge server
|
|
21
|
+
*/
|
|
14
22
|
token?: string
|
|
23
|
+
/**
|
|
24
|
+
* Whether to automatically reconnect to the LAPLACE Event Bridge server
|
|
25
|
+
*/
|
|
15
26
|
reconnect?: boolean
|
|
27
|
+
/**
|
|
28
|
+
* The interval between reconnect attempts in milliseconds
|
|
29
|
+
*
|
|
30
|
+
* @default 3000
|
|
31
|
+
*/
|
|
16
32
|
reconnectInterval?: number
|
|
33
|
+
/**
|
|
34
|
+
* The maximum number of reconnect attempts
|
|
35
|
+
*
|
|
36
|
+
* @default 10
|
|
37
|
+
*/
|
|
17
38
|
maxReconnectAttempts?: number
|
|
18
39
|
}
|
|
19
40
|
|
|
20
|
-
export class
|
|
41
|
+
export class LaplaceEventBridgeClient {
|
|
21
42
|
private ws: WebSocket | null = null
|
|
22
43
|
private eventHandlers = new Map<string, EventHandler<any>[]>()
|
|
23
44
|
private anyEventHandlers: AnyEventHandler[] = []
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laplace.live/event-bridge-sdk",
|
|
3
3
|
"description": "LAPLACE Event Bridge SDK",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.5",
|
|
5
5
|
"module": "index.ts",
|
|
6
|
+
"license": "MIT",
|
|
6
7
|
"type": "module",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"build": "bun build index.ts --outdir dist --target browser --minify",
|