-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67fa520
commit d2ed258
Showing
8 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Kient from 'Kient' | ||
import { Events } from '../src/events' | ||
|
||
// const kient = new Kient({ | ||
// connectToWebsocket: false, | ||
// pusher: { | ||
// appKey: '32cbd69e4b950bf97679', | ||
// }, | ||
// }) | ||
|
||
const kient = new Kient() | ||
|
||
kient.on(Events.Core.WebSocketConnected, async () => { | ||
console.log('connected to ws') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const KientEvents = { | ||
WebSocketConnected: 'KIENT_WEBSOCKET_CONNECTED', | ||
WebSocketDisconnected: 'KIENT_WEBSOCKET_DISCONNECTED', | ||
} as const | ||
|
||
type CoreEvents = { | ||
[KientEvents.WebSocketConnected]: () => void | ||
[KientEvents.WebSocketDisconnected]: () => void | ||
} | ||
|
||
export const Events = { | ||
Core: KientEvents, | ||
} | ||
|
||
export type KientEventEmitters = CoreEvents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type Kient from 'Kient' | ||
import Pusher, { type Channel, type Options } from 'pusher-js' | ||
import { Events } from './events' | ||
|
||
interface WSClientOptions { | ||
pusher: { | ||
appKey: string | ||
cluster: string | ||
} | ||
} | ||
|
||
export class WSClient { | ||
private readonly _kientInstance!: Kient | ||
private readonly pusher!: Pusher | ||
|
||
constructor(kientInstance: Kient, options: WSClientOptions) { | ||
this._kientInstance = kientInstance | ||
|
||
const pusherOptions: Options = { | ||
cluster: options.pusher.cluster, | ||
} | ||
this.pusher = new Pusher(options.pusher.appKey, pusherOptions) | ||
this.pusher.connection.bind('connected', () => | ||
this._kientInstance.emit(Events.Core.WebSocketConnected), | ||
) | ||
this.pusher.connection.bind('disconnected', () => | ||
this._kientInstance.emit(Events.Core.WebSocketDisconnected), | ||
) | ||
} | ||
|
||
async subscribe(channel: string) { | ||
return new Promise<Channel>((resolve, _) => { | ||
const subscribedChannel = this.pusher.subscribe(channel) | ||
subscribedChannel.bind( | ||
'pusher:subscription_error', | ||
(error: { type: string; error: string; status: number }) => { | ||
throw new Error(`Unable to subscribe to channel, error: ${error}`) | ||
}, | ||
) | ||
subscribedChannel.bind('pusher:subscription_succeeded', () => { | ||
resolve(subscribedChannel) | ||
}) | ||
}) | ||
} | ||
|
||
async unsubscribe(channel: string) { | ||
await this.pusher.unsubscribe(channel) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,8 @@ | |
"Kient": [ | ||
"./src/index.ts" | ||
] | ||
} | ||
}, | ||
"outDir": "dist" | ||
}, | ||
"reflection": true | ||
} |