From 098fe2867a0f14c03f311e8dfb06077bc093b697 Mon Sep 17 00:00:00 2001 From: Oscar Fawkes Date: Mon, 10 Jun 2024 21:41:18 +1000 Subject: [PATCH] read full lines of json-rpc from clients (#46) Lgtm, thanks for your contribution --- src/models/StratumV1Client.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index be55195..9592614 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -7,6 +7,7 @@ import * as crypto from 'crypto'; import { Socket } from 'net'; import { firstValueFrom, Subscription } from 'rxjs'; import { clearInterval } from 'timers'; +import { createInterface } from 'readline'; import { AddressSettingsService } from '../ORM/address-settings/address-settings.service'; import { BlocksService } from '../ORM/blocks/blocks.service'; @@ -64,19 +65,19 @@ export class StratumV1Client { private readonly addressSettingsService: AddressSettingsService ) { - this.socket.on('data', (data: Buffer) => { - data.toString() - .split('\n') - .filter(m => m.length > 0) - .forEach(async (m) => { - try { - await this.handleMessage(m); - } catch (e) { - await this.socket.end(); - console.error(e); - } - }) - }); + const rl = createInterface({ + input: this.socket + }) + + rl.on("line", async (line) => { + try { + await this.handleMessage(line); + } catch (e) { + await this.socket.end(); + console.error(e); + } + }) + rl.on('error', async (error: Error) => { }); }