forked from Asvarox/allkaraoke
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
14 changed files
with
272 additions
and
28 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
65 changes: 65 additions & 0 deletions
65
src/modules/RemoteMic/Network/Client/Transport/PartyKitClient.ts
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,65 @@ | ||
import { transportCloseReason, transportErrorReason } from 'modules/RemoteMic/Network/Client/NetworkClient'; | ||
import { ClientTransport } from 'modules/RemoteMic/Network/Client/Transport/interface'; | ||
import { NetworkMessages } from 'modules/RemoteMic/Network/messages'; | ||
import { ForwardedMessage, PARTYKIT_SERVER } from 'modules/RemoteMic/Network/Server/Transport/PartyKitServer'; | ||
import { pack, unpack } from 'modules/RemoteMic/Network/utils'; | ||
import Listener from 'modules/utils/Listener'; | ||
|
||
export class PartyKitClientTransport extends Listener<[NetworkMessages]> implements ClientTransport { | ||
private connection: WebSocket | null = null; | ||
private roomId: string | null = null; | ||
|
||
public connect( | ||
clientId: string, | ||
roomId: string, | ||
onConnect: () => void, | ||
onClose: (reason: transportCloseReason, originalEvent: any) => void, | ||
onError: (error: transportErrorReason, originalEvent: any) => void, | ||
): void { | ||
this.roomId = roomId; | ||
// this.connection = new PartySocket({ host: PARTYKIT_SERVER, room: roomId }); | ||
|
||
this.connection = new WebSocket(`${PARTYKIT_SERVER}/party/${roomId}`); | ||
this.connection.binaryType = 'arraybuffer'; | ||
|
||
this.connection.onopen = () => { | ||
this.connection?.send(pack({ t: 'register-player', id: clientId, roomId: roomId })); | ||
}; | ||
|
||
this.connection.onmessage = (message) => { | ||
const data = unpack<ForwardedMessage>(message.data); | ||
if (data.t === 'forward') { | ||
this.onUpdate(data.payload); | ||
} else if (data.t === 'connected') { | ||
onConnect(); | ||
} | ||
}; | ||
|
||
this.connection.onclose = (event) => { | ||
let reason = 'unknown'; | ||
try { | ||
reason = JSON.parse(event.reason)?.error; | ||
} catch (e) { | ||
console.info('could not parse close reason', event); | ||
} | ||
this.clearAllListeners(); | ||
onClose(reason, event); | ||
}; | ||
|
||
this.connection.onerror = (event) => { | ||
this.clearAllListeners(); | ||
onError('error', event); | ||
}; | ||
} | ||
|
||
public sendEvent(event: NetworkMessages) { | ||
this.connection?.send(pack({ t: 'forward', recipients: [this.roomId], payload: event })); | ||
} | ||
|
||
// readyState >=2 means that the connection is closing or closed | ||
public isConnected = () => (this.connection?.readyState ?? Infinity) < 2; | ||
|
||
public close = () => { | ||
this.connection?.close(); | ||
}; | ||
} |
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
139 changes: 139 additions & 0 deletions
139
src/modules/RemoteMic/Network/Server/Transport/PartyKitServer.ts
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,139 @@ | ||
import { | ||
SenderInterface, | ||
ServerTransport, | ||
transportCloseReason, | ||
} from 'modules/RemoteMic/Network/Server/Transport/interface'; | ||
import { NetworkMessages } from 'modules/RemoteMic/Network/messages'; | ||
import { getPingTime, pack, unpack } from 'modules/RemoteMic/Network/utils'; | ||
import Listener from 'modules/utils/Listener'; | ||
|
||
export interface ForwardedMessage { | ||
t: 'forward'; | ||
sender: string; | ||
payload: NetworkMessages; | ||
} | ||
|
||
interface WebsocketConnectedMessage { | ||
t: 'connected'; | ||
} | ||
|
||
interface WebsocketPongMessage { | ||
t: 'pong'; | ||
} | ||
|
||
export type WebsocketMessage = ForwardedMessage | WebsocketConnectedMessage | WebsocketPongMessage; | ||
|
||
export const PARTYKIT_SERVER = import.meta.env.VITE_APP_PARTYKIT_URL; | ||
|
||
export class PartyKitServerTransport extends Listener<[NetworkMessages, SenderInterface]> implements ServerTransport { | ||
public readonly name = 'PartyKit'; | ||
private connection: WebSocket | null = null; | ||
|
||
public connect( | ||
roomId: string, | ||
onConnect: () => void, | ||
onClose: (reason: transportCloseReason, originalEvent: any) => void, | ||
) { | ||
this.connection = new WebSocket(`${PARTYKIT_SERVER}/party/${roomId}`); | ||
this.connection.binaryType = 'arraybuffer'; | ||
this.connection.onopen = () => { | ||
this.connection?.send(pack({ t: 'register-room', id: roomId })); | ||
onConnect(); | ||
this.ping(); | ||
|
||
this.connection?.addEventListener('message', (message) => { | ||
const payload: WebsocketMessage = unpack(message.data); | ||
|
||
if (payload.t === 'forward') { | ||
if (!['ping', 'pong', 'freq'].includes(payload?.payload?.t)) console.log('received', payload); | ||
const { sender, payload: data } = payload; | ||
const conn = new SenderWrapper(sender, this.connection!); | ||
|
||
this.onUpdate(data, conn); | ||
} else if (payload.t === 'pong') { | ||
this.onPong(); | ||
} else { | ||
console.warn('Unknown message type', payload); | ||
} | ||
}); | ||
}; | ||
|
||
this.connection.onclose = (event) => { | ||
onClose(event.reason, event); | ||
}; | ||
} | ||
|
||
public disconnect = () => { | ||
this.connection?.close(); | ||
}; | ||
|
||
// todo create a a util to share with Network Client | ||
private latency = 0; | ||
private pingStart = getPingTime(); | ||
public pinging = false; | ||
private pingTimeout: ReturnType<typeof setTimeout> | null = null; | ||
|
||
private ping = () => { | ||
this.pinging = true; | ||
this.pingStart = getPingTime(); | ||
|
||
this.connection?.send(pack({ t: 'ping' })); | ||
}; | ||
private onPong = () => { | ||
if (!this.pinging) return; | ||
this.latency = getPingTime() - this.pingStart; | ||
this.pinging = false; | ||
|
||
if (this.pingTimeout) clearTimeout(this.pingTimeout); | ||
this.pingTimeout = setTimeout(this.ping, 5_000); | ||
}; | ||
|
||
public getCurrentPing = () => { | ||
return this.pinging ? Math.max(this.latency, getPingTime() - this.pingStart) : this.latency; | ||
}; | ||
} | ||
|
||
type callback = (data: any) => void; | ||
|
||
class SenderWrapper implements SenderInterface { | ||
private currentPing = 0; | ||
constructor( | ||
public peer: string, | ||
private socket: WebSocket, | ||
) {} | ||
|
||
public send = (payload: NetworkMessages) => { | ||
const data = { t: 'forward', recipients: [this.peer], payload }; | ||
if (!['ping', 'pong', 'freq'].includes(payload?.t)) console.log('sending', this.peer, payload); | ||
this.socket.send(pack(data)); | ||
}; | ||
|
||
private callbacksMap: Map<callback, callback> = new Map(); | ||
|
||
public on = (event: string, callback: (data: any) => void) => { | ||
if (event === 'data') { | ||
this.callbacksMap.set(callback, (message) => { | ||
const data: WebsocketMessage = unpack(message.data); | ||
if (data.t === 'forward') { | ||
const { sender, payload } = data; | ||
if (sender === this.peer) { | ||
callback(payload); | ||
} | ||
} | ||
}); | ||
this.socket.addEventListener('message', this.callbacksMap.get(callback)!); | ||
} | ||
}; | ||
|
||
public off = (event: string, callback: (data: any) => void) => { | ||
if (event === 'data') { | ||
const actualCallback = this.callbacksMap.get(callback); | ||
this.socket.removeEventListener('message', actualCallback!); | ||
this.callbacksMap.delete(callback); | ||
} | ||
}; | ||
|
||
public close = () => { | ||
this.socket.close(); | ||
}; | ||
} |
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
Oops, something went wrong.