This commit is contained in:
Eugene Pankov
2021-06-21 09:22:13 +02:00
parent 8bae6e1769
commit 663615fe06
11 changed files with 145 additions and 22 deletions

View File

@@ -3,30 +3,67 @@ import { Subject } from 'rxjs'
import { debounceTime } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { LoginService } from '../services/login.service'
export class SocketProxy {
connect$ = new Subject<void>()
data$ = new Subject<Buffer>()
error$ = new Subject<Buffer>()
webSocket: WebSocket
initialBuffer: Buffer
options: {
host: string
port: number
}
constructor () {
constructor (private appConnector: AppConnectorService) {
this.initialBuffer = Buffer.from('')
}
connect (options) {
console.log('ws connect', options)
this.webSocket = new WebSocket(`ws://${location.host}/api/1/gateway/tcp/${options.host}:${options.port}`)
this.webSocket.onopen = event => {
this.options = options
this.webSocket = new WebSocket(
this.appConnector.loginService.user.custom_connection_gateway ||
`ws://${location.host}/api/1/gateway/tcp`
)
this.webSocket.onmessage = async event => {
if (typeof(event.data) === 'string') {
this.handleServiceMessage(JSON.parse(event.data))
} else {
this.data$.next(Buffer.from(await event.data.arrayBuffer()))
}
}
}
handleServiceMessage (msg) {
if (msg._ === 'hello') {
this.sendServiceMessage({
_: 'hello',
version: 1,
auth_token: this.appConnector.loginService.user.custom_connection_gateway_token,
})
} else if (msg._ === 'ready') {
this.sendServiceMessage({
_: 'connect',
host: this.options.host,
port: this.options.port,
})
} else if (msg._ === 'connected') {
this.connect$.next()
this.connect$.complete()
this.webSocket.send(this.initialBuffer)
this.initialBuffer = Buffer.from('')
} else if (msg._ === 'error') {
console.error('Connection gateway error', msg)
this.close(new Error(msg.details))
} else {
console.warn('Unknown service message', msg)
}
this.webSocket.onmessage = async event => {
this.data$.next(Buffer.from(await event.data.arrayBuffer()))
}
}
sendServiceMessage (msg) {
this.webSocket.send(JSON.stringify(msg))
}
write (chunk: Buffer): void {
@@ -38,7 +75,12 @@ export class SocketProxy {
}
close (error: Error): void {
console.warn('socket destroy', error)
if (error) {
this.error$.next(error)
}
this.connect$.complete()
this.data$.complete()
this.error$.complete()
}
}
@@ -46,10 +88,13 @@ export class SocketProxy {
export class AppConnectorService {
config: any
version: any
Socket = SocketProxy
user: any
private configUpdate = new Subject<string>()
constructor (private http: HttpClient) {
constructor (
private http: HttpClient,
public loginService: LoginService,
) {
this.configUpdate.pipe(debounceTime(1000)).subscribe(async content => {
const result = await this.http.patch(`/api/1/configs/${this.config.id}`, { content }).toPromise()
Object.assign(this.config, result)
@@ -68,4 +113,8 @@ export class AppConnectorService {
getAppVersion (): string {
return this.version.version
}
createSocket () {
return new SocketProxy(this)
}
}

View File

@@ -0,0 +1,21 @@
import { AsyncSubject } from 'rxjs'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
@Injectable({ providedIn: 'root' })
export class LoginService {
user: any
ready$ = new AsyncSubject<void>()
constructor (private http: HttpClient) {
this.init()
}
private async init () {
const user = await this.http.get('/api/1/user').toPromise()
this.user = user
this.ready$.next()
this.ready$.complete()
}
}