Skip to content

Commit

Permalink
socket-mode: add more debug logging to low level websocket event hand…
Browse files Browse the repository at this point in the history
…lers (#1757)
  • Loading branch information
filmaj authored Mar 15, 2024
1 parent 5df239e commit 6ab1e68
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/socket-mode/src/SocketModeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,33 +512,38 @@ export class SocketModeClient extends EventEmitter {
};

let websocket: WebSocket;
let socketId: string;
if (this.websocket === undefined) {
this.websocket = new WebSocket(url, options);
socketId = 'Primary';
websocket = this.websocket;
} else {
// Set up secondary websocket
// This is used when creating a new connection because the first is about to disconnect
this.secondaryWebsocket = new WebSocket(url, options);
socketId = 'Secondary';
websocket = this.secondaryWebsocket;
}

// Attach event listeners
websocket.addEventListener('open', (event) => {
this.logger.debug(`${socketId} WebSocket open event received (connection established)`);
this.stateMachine.handle(Event.WebSocketOpen, event);
});
websocket.addEventListener('error', (event) => {
this.logger.error(`A WebSocket error occurred: ${event.message}`);
this.logger.error(`${socketId} WebSocket error occurred: ${event.message}`);
this.emit('error', websocketErrorWithOriginal(event.error));
});
websocket.on('message', this.onWebSocketMessage.bind(this));
websocket.on('close', (code: number, _data: Buffer) => {
this.logger.debug(`${socketId} WebSocket close event received (code: ${code}, reason: ${_data.toString()})`);
this.stateMachine.handle(Event.WebSocketClose, code);
});

// Confirm WebSocket connection is still active
websocket.on('ping', ((data: Buffer) => {
if (this.pingPongLoggingEnabled) {
this.logger.debug(`Received ping from Slack server (data: ${data})`);
this.logger.debug(`${socketId} WebSocket received ping from Slack server (data: ${data})`);
}
this.startMonitoringPingFromSlack();
// Since the `addEventListener` method does not accept listener with data arg in TypeScript,
Expand All @@ -547,7 +552,7 @@ export class SocketModeClient extends EventEmitter {

websocket.on('pong', ((data: Buffer) => {
if (this.pingPongLoggingEnabled) {
this.logger.debug(`Received pong from Slack server (data: ${data})`);
this.logger.debug(`${socketId} WebSocket received pong from Slack server (data: ${data})`);
}
this.lastPongReceivedTimestamp = new Date().getTime();
// Since the `addEventListener` method does not accept listener with data arg in TypeScript,
Expand Down

0 comments on commit 6ab1e68

Please sign in to comment.