This commit is contained in:
Eugene Pankov
2021-06-19 23:01:22 +02:00
parent f8f58a66ec
commit e74b44a9bc
5 changed files with 81 additions and 210 deletions

View File

@@ -1,11 +1,53 @@
import { Buffer } from 'buffer'
import { Subject } from 'rxjs'
import { debounceTime } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
export class SocketProxy {
connect$ = new Subject<void>()
data$ = new Subject<Buffer>()
webSocket: WebSocket
initialBuffer: Buffer
constructor (...args) {
console.log('ws ctr', args)
this.initialBuffer = Buffer.from('')
}
connect (...args) {
console.log('ws connect', args)
this.webSocket = new WebSocket(`ws://${location.host}/api/1/gateway/tcp`)
this.webSocket.onopen = event => {
this.connect$.next()
this.connect$.complete()
this.webSocket.send(this.initialBuffer)
this.initialBuffer = Buffer.from('')
}
this.webSocket.onmessage = async event => {
this.data$.next(Buffer.from(await event.data.arrayBuffer()))
}
}
write (chunk: Buffer): void {
if (!this.webSocket.readyState) {
this.initialBuffer = Buffer.concat([this.initialBuffer, chunk])
} else {
this.webSocket.send(chunk)
}
}
close (error: Error): void {
console.warn('socket destroy', error)
}
}
@Injectable({ providedIn: 'root' })
export class AppConnectorService {
config: any
version: any
Socket = SocketProxy
private configUpdate = new Subject<string>()
constructor (private http: HttpClient) {
@@ -23,4 +65,8 @@ export class AppConnectorService {
this.configUpdate.next(content)
this.config.content = content
}
getAppVersion (): string {
return this.version.version
}
}