mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
make websocket connect async.
This commit is contained in:
parent
31cfacd1df
commit
f689bea219
@ -49,7 +49,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|||||||
return typeof this._ws !== 'undefined' && this._ws.readyState === WebSocket.OPEN
|
return typeof this._ws !== 'undefined' && this._ws.readyState === WebSocket.OPEN
|
||||||
}
|
}
|
||||||
|
|
||||||
public connect() {
|
public async connect() {
|
||||||
if (this.connected) {
|
if (this.connected) {
|
||||||
throw new Error('attempting to create websocket while connection open')
|
throw new Error('attempting to create websocket while connection open')
|
||||||
}
|
}
|
||||||
@ -66,15 +66,24 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|||||||
url += '?token=' + encodeURIComponent(this._token)
|
url += '?token=' + encodeURIComponent(this._token)
|
||||||
}
|
}
|
||||||
|
|
||||||
this._ws = new WebSocket(url)
|
await new Promise<void>((res, rej) => {
|
||||||
this._log.info(`connecting`)
|
this._ws = new WebSocket(url)
|
||||||
|
this._log.info(`connecting`)
|
||||||
|
|
||||||
this._ws.onopen = this.onConnected.bind(this)
|
this._ws.onclose = rej.bind(this, new Error('connection close'))
|
||||||
this._ws.onclose = this.onClose.bind(this)
|
this._ws.onerror = rej.bind(this, new Error('connection error'))
|
||||||
this._ws.onerror = this.onError.bind(this)
|
this._ws.onmessage = this.onMessage.bind(this)
|
||||||
this._ws.onmessage = this.onMessage.bind(this)
|
|
||||||
|
|
||||||
this._connTimer = setTimeout(this.onTimeout.bind(this), connTimeout)
|
this._ws.onopen = () => {
|
||||||
|
this._ws!.onclose = this.onClose.bind(this, 'close')
|
||||||
|
this._ws!.onerror = this.onClose.bind(this, 'error')
|
||||||
|
|
||||||
|
this.onConnected()
|
||||||
|
res()
|
||||||
|
}
|
||||||
|
|
||||||
|
this._connTimer = setTimeout(rej.bind(this, new Error('connection timeout')), connTimeout)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnect(reason?: Error) {
|
public disconnect(reason?: Error) {
|
||||||
@ -145,21 +154,9 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|||||||
this._reconnTimeout = reconnTimeout
|
this._reconnTimeout = reconnTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
private onTimeout() {
|
private onClose(reason: string) {
|
||||||
this._log.info(`connection timeout`)
|
this._log.info(`connection ${reason}`)
|
||||||
this.disconnect(new Error('connection timeout'))
|
this.disconnect(new Error(`connection ${reason}`))
|
||||||
this.tryReconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
private onError() {
|
|
||||||
this._log.info(`connection error`)
|
|
||||||
this.disconnect(new Error('connection error'))
|
|
||||||
this.tryReconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
private onClose() {
|
|
||||||
this._log.info(`connection closed`)
|
|
||||||
this.disconnect(new Error('connection closed'))
|
|
||||||
this.tryReconnect()
|
this.tryReconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,7 +207,7 @@
|
|||||||
Vue.set(this.state.connection, 'authenticated', true)
|
Vue.set(this.state.connection, 'authenticated', true)
|
||||||
|
|
||||||
if (this.autoconnect) {
|
if (this.autoconnect) {
|
||||||
this.websocket.connect()
|
await this.websocket.connect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -230,7 +230,7 @@
|
|||||||
Vue.set(this.state.connection, 'authenticated', true)
|
Vue.set(this.state.connection, 'authenticated', true)
|
||||||
|
|
||||||
if (this.autoconnect) {
|
if (this.autoconnect) {
|
||||||
this.websocket.connect()
|
await this.websocket.connect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,7 +257,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public websocketConnect() {
|
public async websocketConnect() {
|
||||||
if (!this.authenticated) {
|
if (!this.authenticated) {
|
||||||
throw new Error('client not authenticated')
|
throw new Error('client not authenticated')
|
||||||
}
|
}
|
||||||
@ -266,7 +266,7 @@
|
|||||||
throw new Error('client already connected to websocket')
|
throw new Error('client already connected to websocket')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.websocket.connect()
|
await this.websocket.connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
public websocketDisconnect() {
|
public websocketDisconnect() {
|
||||||
|
Loading…
Reference in New Issue
Block a user