Archived
2
0

automatic login

This commit is contained in:
Craig
2020-02-11 19:36:06 +00:00
parent b6a5c2cc95
commit 6db53d8c48
5 changed files with 106 additions and 45 deletions

View File

@ -73,6 +73,28 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
}
}
protected disconnect() {
if (this._timeout) {
clearTimeout(this._timeout)
}
if (this.socketOpen) {
try {
this._ws!.close()
} catch (err) {}
this._ws = undefined
}
if (this.peerConnected) {
try {
this._peer!.close()
} catch (err) {}
this._peer = undefined
}
this._state = 'disconnected'
}
public sendData(event: 'wheel' | 'mousemove', data: { x: number; y: number }): void
public sendData(event: 'mousedown' | 'mouseup' | 'keydown' | 'keyup', data: { key: number }): void
public sendData(event: string, data: any) {
@ -165,14 +187,6 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
}
}
this._peer.onsignalingstatechange = event => {
this.emit('debug', `peer signal state chagned: ${this._peer!.signalingState}`)
}
this._peer.onconnectionstatechange = event => {
this.emit('debug', `peer connection state chagned: ${this._peer!.connectionState}`)
}
this._peer.oniceconnectionstatechange = event => {
this._state = this._peer!.iceConnectionState
@ -213,7 +227,7 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
private setRemoteDescription(payload: SignalPayload) {
if (this.peerConnected) {
this.emit('warn', `received ${event} with no peer!`)
this.emit('warn', `attempting to set remote description while peer connected`, payload)
return
}
this._peer!.setRemoteDescription({ type: 'answer', sdp: payload.sdp })
@ -292,24 +306,7 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
}
protected onDisconnected(reason?: Error) {
if (this._timeout) {
clearTimeout(this._timeout)
}
if (this.socketOpen) {
try {
this._ws!.close()
} catch (err) {}
this._ws = undefined
}
if (this.peerConnected) {
try {
this._peer!.close()
} catch (err) {}
this._peer = undefined
}
this.disconnect()
this.emit('debug', `disconnected:`, reason)
this[EVENT.DISCONNECTED](reason)
}

View File

@ -28,22 +28,40 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
private $vue!: Vue
private $accessor!: typeof accessor
private get id() {
return this.$accessor.user.id
}
init(vue: Vue) {
this.$vue = vue
this.$accessor = vue.$accessor
}
connect(password: string, username: string) {
private cleanup() {
this.$accessor.setConnected(false)
this.$accessor.remote.reset()
this.$accessor.user.reset()
this.$accessor.video.reset()
this.$accessor.chat.reset()
}
login(password: string, username: string) {
const url =
process.env.NODE_ENV === 'development'
? `ws://${location.host.split(':')[0]}:${process.env.VUE_APP_SERVER_PORT}/`
: `${/https/gi.test(location.protocol) ? 'wss' : 'ws'}://${location.host}/`
super.connect(url, password, username)
this.connect(url, password, username)
}
private get id() {
return this.$accessor.user.id
logout() {
this.disconnect()
this.cleanup()
this.$vue.$swal({
title: 'You have logged out!',
icon: 'info',
confirmButtonText: 'ok',
})
}
/////////////////////////////
@ -67,13 +85,7 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
}
protected [EVENT.DISCONNECTED](reason?: Error) {
this.$accessor.setConnected(false)
this.$accessor.remote.reset()
this.$accessor.user.reset()
this.$accessor.video.reset()
this.$accessor.chat.reset()
this.cleanup()
this.$vue.$notify({
group: 'neko',
type: 'error',