mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
webrtc & websocket simple implementation.
This commit is contained in:
@ -15,7 +15,7 @@ export interface NekoWebRTCEvents {
|
||||
track: (event: RTCTrackEvent) => void
|
||||
}
|
||||
|
||||
export abstract class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
|
||||
export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
|
||||
private _peer?: RTCPeerConnection
|
||||
private _channel?: RTCDataChannel
|
||||
private _state: RTCIceConnectionState = 'disconnected'
|
||||
@ -23,7 +23,7 @@ export abstract class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
|
||||
this._log = new Logger('webrtc')
|
||||
}
|
||||
|
||||
@ -37,11 +37,11 @@ export abstract class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
|
||||
|
||||
public async connect(sdp: string, lite: boolean, servers: string[]): Promise<string> {
|
||||
this._log.debug(`creating peer`)
|
||||
|
||||
|
||||
if (!this.supported) {
|
||||
throw new Error('browser does not support webrtc')
|
||||
}
|
||||
|
||||
|
||||
if (this.connected) {
|
||||
throw new Error('attempting to create peer while connected')
|
||||
}
|
||||
@ -87,7 +87,7 @@ export abstract class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
|
||||
this._peer.addTransceiver('video', { direction: 'recvonly' })
|
||||
|
||||
this._channel = this._peer.createDataChannel('data')
|
||||
this._channel.onerror = this.onError.bind(this)
|
||||
this._channel.onerror = this.onDisconnected.bind(this, new Error('peer data channel error'))
|
||||
this._channel.onmessage = this.onData.bind(this)
|
||||
this._channel.onclose = this.onDisconnected.bind(this, new Error('peer data channel closed'))
|
||||
|
||||
@ -175,17 +175,13 @@ export abstract class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
|
||||
private onTrack(event: RTCTrackEvent) {
|
||||
this._log.debug(`received ${event.track.kind} track from peer: ${event.track.id}`, event)
|
||||
const stream = event.streams[0]
|
||||
|
||||
|
||||
if (!stream) {
|
||||
this._log.warn(`no stream provided for track ${event.track.id}(${event.track.label})`)
|
||||
return
|
||||
}
|
||||
|
||||
this.emit('track', event)
|
||||
}
|
||||
|
||||
private onError(event: Event) {
|
||||
this._log.error((event as ErrorEvent).error)
|
||||
this.emit('track', event)
|
||||
}
|
||||
|
||||
private onConnected() {
|
||||
@ -201,7 +197,7 @@ export abstract class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
|
||||
private onDisconnected(reason?: Error) {
|
||||
this.disconnect()
|
||||
|
||||
this._log.debug(`disconnected:`, reason)
|
||||
this._log.debug(`disconnected:`, reason?.message)
|
||||
this.emit('disconnected', reason)
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ export interface NekoWebSocketEvents {
|
||||
connecting: () => void
|
||||
connected: () => void
|
||||
disconnected: (error?: Error) => void
|
||||
message: (event: string, payload: any) => void
|
||||
}
|
||||
|
||||
export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
||||
@ -36,7 +37,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
||||
|
||||
this._ws.onopen = this.onConnected.bind(this)
|
||||
this._ws.onclose = this.onDisconnected.bind(this, new Error('websocket closed'))
|
||||
this._ws.onerror = this.onError.bind(this)
|
||||
this._ws.onerror = this.onDisconnected.bind(this, new Error('websocket error'))
|
||||
this._ws.onmessage = this.onMessage.bind(this)
|
||||
|
||||
this._timeout = setTimeout(this.onTimeout.bind(this), timeout)
|
||||
@ -68,20 +69,9 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
||||
|
||||
private onMessage(e: MessageEvent) {
|
||||
const { event, ...payload } = JSON.parse(e.data)
|
||||
|
||||
this._log.debug(`received websocket event ${event} ${payload ? `with payload: ` : ''}`, payload)
|
||||
|
||||
// @ts-ignore
|
||||
if (typeof this[event] === 'function') {
|
||||
// @ts-ignore
|
||||
// TODO: REFACTOR
|
||||
this[event](payload)
|
||||
} else {
|
||||
this._log.warn(`unhandled websocket event '${event}':`, payload)
|
||||
}
|
||||
}
|
||||
|
||||
private onError(event: Event) {
|
||||
this._log.error((event as ErrorEvent).error)
|
||||
this.emit('message', event, payload)
|
||||
}
|
||||
|
||||
private onConnected() {
|
||||
@ -104,7 +94,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
||||
}
|
||||
|
||||
private onDisconnected(reason?: Error) {
|
||||
this._log.debug(`disconnected:`, reason)
|
||||
this._log.debug(`disconnected:`, reason?.message)
|
||||
|
||||
this.disconnect()
|
||||
this.emit('disconnected', reason)
|
||||
|
Reference in New Issue
Block a user