Skip to content

Commit

Permalink
Ping connection for net-sync client
Browse files Browse the repository at this point in the history
  • Loading branch information
pvarentsov committed Jul 22, 2022
1 parent 07a2cb1 commit 3421cdd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test:cov": "jest --config jest.json --runInBand --coverage",
"lint": "eslint 'src/**/*.ts'",
"lint:fix": "eslint --fix 'src/**/*.ts'",
"link": "npm link",
"link": "npm run build:release && npm link",
"unlink": "npm unlink iola -g",
"demo:iola": "terminalizer render ./demo/iola-demo.yml -o ./demo/iola-demo.gif",
"ws": "ts-node ./test/server/main/ws.ts",
Expand Down
32 changes: 15 additions & 17 deletions src/api/cli/cli/cli.interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ export class CliInteractive implements ICliInteractive {
) {}

async listen(server: IHttpServer, client: ISocketClient): Promise<void> {
if (!this.config.netSync) {
const spinner = ora('Connecting').start()

const stopSpinner = (delay: number): Promise<ora.Ora> => new Promise(
resolve => setTimeout(() => resolve(spinner.stop()), delay)
)

try {
await client.connect()
await stopSpinner(500)
}
catch (err) {
spinner.color = 'red'
await stopSpinner(500)

throw err
}
const spinner = ora('Connecting').start()

const stopSpinner = (delay: number): Promise<ora.Ora> => new Promise(
resolve => setTimeout(() => resolve(spinner.stop()), delay)
)

try {
await client.connect()
await stopSpinner(500)
}
catch (err) {
spinner.color = 'red'
await stopSpinner(500)

throw err
}

const address = await server
Expand Down
14 changes: 13 additions & 1 deletion src/core/socket/client/net-socket.sync.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ export class NetSocketSyncClient implements ISocketClient {
}

async connect(): Promise<void> {
return undefined
const socket = createConnection(this.netOptions())

const connect$ = fromEvent(socket, 'connect').pipe(
RxJSUtil.timeout(1000, `connection to ${this._options.address} is timed out`),
)

try {
await firstValueFrom(connect$)
} catch (e) {
throw e
} finally {
socket.destroy()
}
}

async sendData<TData>(data: TData): Promise<SocketSendReply> {
Expand Down

0 comments on commit 3421cdd

Please sign in to comment.