mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-13 13:17:45 +02:00
Initial standalone server implementation (#15)
Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
@@ -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'))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user