add new api internal class.

This commit is contained in:
Miroslav Šedivý 2020-11-29 15:34:52 +01:00
parent 84e4f22524
commit ac9ac7ac69
3 changed files with 39 additions and 3 deletions

View File

@ -0,0 +1,30 @@
import * as Api from '../api'
export class NekoApi {
api_configuration = new Api.Configuration()
public connect(url: string, id: string, secret: string) {
this.api_configuration = new Api.Configuration({
basePath: url,
headers: {
Authorization: 'Basic ' + btoa(id + ':' + secret),
},
})
}
public disconnect() {
this.api_configuration = new Api.Configuration()
}
get admin(): Api.AdminsApi {
return new Api.AdminsApi(this.api_configuration)
}
get user(): Api.UsersApi {
return new Api.UsersApi(this.api_configuration)
}
get host(): Api.HostsApi {
return new Api.HostsApi(this.api_configuration)
}
}

View File

@ -32,8 +32,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
this.emit('connecting')
const ws_url = url.replace(/^http/, 'ws').replace(/\/$|\/ws$/, '')
this._ws = new WebSocket(`${ws_url}/ws?id=${encodeURIComponent(id)}&secret=${encodeURIComponent(secret)}`)
this._ws = new WebSocket(`${url}/ws?id=${encodeURIComponent(id)}&secret=${encodeURIComponent(secret)}`)
this._log.debug(`connecting to ${this._ws.url}`)
this._ws.onopen = this.onConnected.bind(this)

View File

@ -44,6 +44,7 @@
import ResizeObserver from 'resize-observer-polyfill'
import EventEmitter from 'eventemitter3'
import { NekoApi } from './internal/api'
import { NekoWebSocket } from './internal/websocket'
import { NekoWebRTC } from './internal/webrtc'
import { NekoMessages } from './internal/messages'
@ -63,6 +64,7 @@
@Ref('container') readonly _container!: HTMLElement
@Ref('video') readonly _video!: HTMLVideoElement
api = new NekoApi()
websocket = new NekoWebSocket()
webrtc = new NekoWebRTC()
observer = new ResizeObserver(this.onResize.bind(this))
@ -138,7 +140,11 @@
throw new Error('client already connected')
}
this.websocket.connect(url, id, secret)
const wsURL = url.replace(/^http/, 'ws').replace(/\/$|\/ws\/?$/, '')
this.websocket.connect(wsURL, id, secret)
const httpURL = url.replace(/^ws/, 'http').replace(/\/$|\/ws\/?$/, '')
this.api.connect(httpURL, id, secret)
}
public disconnect() {
@ -147,6 +153,7 @@
}
this.websocket.disconnect()
this.api.disconnect()
}
public play() {