diff --git a/src/connection/socket-connection.ts b/src/connection/socket-connection.ts index e4de558..fabcab1 100644 --- a/src/connection/socket-connection.ts +++ b/src/connection/socket-connection.ts @@ -38,7 +38,8 @@ export class SocketConnection { if (this.socket[SOCKET_ISALIVE_SYMBOL] === false) { this.close(); } - this.ping(); + this.socket[SOCKET_ISALIVE_SYMBOL] = false; + this.socket.ping(); }, HEARTBEAT_INTERVAL); } @@ -52,24 +53,22 @@ export class SocketConnection { // close all of the associated subscriptions await this.subscriptions.closeAll(); - } - private ping(): void { - this.socket[SOCKET_ISALIVE_SYMBOL] = false; - this.socket.ping(); + // close the socket. + this.socket.close(); } /** * Pong messages are automatically sent in response to ping messages as required by - * the websocket spec. So, no need to send explicit pongs from browser + * the websocket spec. So, no need to send explicit pongs. */ private pong(): void { this.socket[SOCKET_ISALIVE_SYMBOL] = true; } private async error(error?:Error): Promise{ - if (error !== undefined) { - log.error('WebSocket', this.socket.url, error); + if (error) { + log.error(`SocketConnection error, terminating connection`, error); this.socket.terminate(); await this.close() } @@ -108,7 +107,6 @@ export class SocketConnection { status: jsonRpcResponse?.result?.reply?.status?.code || 0, }); } - this.send(jsonRpcResponse); } diff --git a/src/json-rpc-socket.ts b/src/json-rpc-socket.ts index c96f40d..0622b37 100644 --- a/src/json-rpc-socket.ts +++ b/src/json-rpc-socket.ts @@ -1,3 +1,4 @@ +import log from 'loglevel'; import { v4 as uuidv4 } from 'uuid'; import WebSocket from 'ws'; @@ -22,11 +23,11 @@ export class JSONRPCSocket { const { connectTimeout = CONNECT_TIMEOUT, responseTimeout = RESPONSE_TIMEOUT } = options; const onclose = ():void => { - console.log('json rpc close'); + log.info(`JSON RPC Socket close ${url}`); }; const onerror = (event: any):void => { - console.log('json rpc error', event); + log.error(`JSON RPC Socket error ${url}`, event); }; const socket = new WebSocket(url); diff --git a/src/subscription-manager.ts b/src/subscription-manager.ts index 7351954..de87dec 100644 --- a/src/subscription-manager.ts +++ b/src/subscription-manager.ts @@ -2,6 +2,9 @@ import type { MessageSubscription } from "@tbd54566975/dwn-sdk-js"; import { DwnServerError, DwnServerErrorCode } from "./dwn-error.js"; +/** + * SubscriptionManager manages the subscriptions related to a `SocketConnection` + */ export interface SubscriptionManager { subscribe: (target: string, subscription: MessageSubscription) => Promise; close: (target: string, id: string) => Promise; diff --git a/tests/utils.ts b/tests/utils.ts index 7b1b318..c05021d 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -255,10 +255,8 @@ export async function subscriptionRequest( messageHandler(record); return; } - if (subscription) { resolved = true; - resolve({ status, subscription: {