Skip to content

Commit

Permalink
Merge pull request #29 from UserJHansen/master
Browse files Browse the repository at this point in the history
Catch a failed message
  • Loading branch information
ryo-ma authored Sep 6, 2021
2 parents 87017ed + 0cb7384 commit 12f8fca
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface WebSocketClient extends EventEmitter {
}

export class WebSocketAcceptedClient extends EventEmitter
implements WebSocketClient {
implements WebSocketClient {
state: WebSocketState = WebSocketState.CONNECTING;
webSocket: DenoWebSocketType;
constructor(sock: DenoWebSocketType) {
Expand Down Expand Up @@ -128,17 +128,22 @@ export class WebSocketAcceptedClient extends EventEmitter
if (this.state === WebSocketState.CONNECTING) {
throw new WebSocketError(
"WebSocket is not open: state 0 (CONNECTING)",
);
);
}
return this.webSocket!.ping(message);
}
async send(message: string | Uint8Array) {
if (this.state === WebSocketState.CONNECTING) {
throw new WebSocketError(
"WebSocket is not open: state 0 (CONNECTING)",
);
try {
if (this.state === WebSocketState.CONNECTING) {
throw new WebSocketError(
"WebSocket is not open: state 0 (CONNECTING)",
);
}
return this.webSocket!.send(message);
} catch (error) {
this.state = WebSocketState.CLOSED;
this.emit("close", error.message);
}
return this.webSocket!.send(message);
}
async close(code = 1000, reason?: string): Promise<void> {
if (
Expand Down Expand Up @@ -182,15 +187,15 @@ export class StandardWebSocketClient extends EventEmitter
if (this.webSocket?.readyState === WebSocketState.CONNECTING) {
throw new WebSocketError(
"WebSocket is not open: state 0 (CONNECTING)",
);
);
}
return this.webSocket!.send("ping");
}
async send(message: string | Uint8Array) {
if (this.webSocket?.readyState === WebSocketState.CONNECTING) {
throw new WebSocketError(
"WebSocket is not open: state 0 (CONNECTING)",
);
);
}
return this.webSocket!.send(message);
}
Expand All @@ -207,7 +212,7 @@ export class StandardWebSocketClient extends EventEmitter
throw new Error("Method not implemented.");
}
get isClosed(): boolean | undefined {
return this.webSocket!.readyState === WebSocketState.CLOSING ||
return this.webSocket!.readyState === WebSocketState.CLOSING ||
this.webSocket!.readyState === WebSocketState.CLOSED
}
}

0 comments on commit 12f8fca

Please sign in to comment.