Initial standalone server implementation (#15)

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov
2021-08-08 23:04:39 +02:00
committed by GitHub
parent 67940e0d35
commit a8693be822
41 changed files with 349 additions and 174 deletions
+9 -4
View File
@@ -31,12 +31,10 @@ class DeferredPromise {
}
class Connection implements Storage {
private readonly webSocket: WebSocket
private readonly requests = new Map<ReqId, DeferredPromise>()
private lastId = 0
constructor (url: string, private readonly handler: TxHander) {
this.webSocket = new WebSocket(url)
constructor (private readonly webSocket: WebSocket, private readonly handler: TxHander) {
this.webSocket.onmessage = (event: MessageEvent) => {
const resp = readResponse(event.data)
if (resp.id !== undefined) {
@@ -49,6 +47,7 @@ class Connection implements Storage {
promise.resolve(resp.result)
}
} else {
console.log('handle', resp)
this.handler(resp.result as Tx)
}
}
@@ -76,5 +75,11 @@ class Connection implements Storage {
}
export async function connect (url: string, handler: TxHander): Promise<Storage> {
return new Connection(url, handler)
return await new Promise((resolve, reject) => {
const webSocket = new WebSocket(url)
webSocket.onopen = () => {
resolve(new Connection(webSocket, handler))
}
webSocket.onerror = () => reject(new Error('Could not connect'))
})
}
+3 -7
View File
@@ -14,8 +14,6 @@
//
import { createClient, Client, TxHander } from '@anticrm/core'
import { getMetadata } from '@anticrm/platform'
import clientPlugin from '@anticrm/client'
import { connect } from './connection'
@@ -30,13 +28,11 @@ export default async () => {
return {
function: {
GetClient: async (): Promise<Client> => {
GetClient: async (token: string, endpoint: string): Promise<Client> => {
if (client === undefined) {
const url = getMetadata(clientPlugin.metadata.ClientUrl)
if (url === undefined) {
throw new Error('no app server url provided.')
}
return await createClient((handler: TxHander) => {
const url = `ws://${endpoint}:3333/${token}`
console.log('connecting to', url)
return connect(url, handler)
})
}