Skip to content

Commit

Permalink
account for null errors in socket handler, tidy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Feb 12, 2024
1 parent 878ee37 commit 9189c75
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
16 changes: 7 additions & 9 deletions src/connection/socket-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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<void>{
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()
}
Expand Down Expand Up @@ -108,7 +107,6 @@ export class SocketConnection {
status: jsonRpcResponse?.result?.reply?.status?.code || 0,
});
}

this.send(jsonRpcResponse);
}

Expand Down
5 changes: 3 additions & 2 deletions src/json-rpc-socket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import log from 'loglevel';
import { v4 as uuidv4 } from 'uuid';
import WebSocket from 'ws';

Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/subscription-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
close: (target: string, id: string) => Promise<void>;
Expand Down
2 changes: 0 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,8 @@ export async function subscriptionRequest(
messageHandler(record);
return;
}

if (subscription) {
resolved = true;

resolve({
status,
subscription: {
Expand Down

0 comments on commit 9189c75

Please sign in to comment.