_enableWebSocketInterception(): void { if (WebSocketInterceptor.isInterceptorEnabled()) { return; } // Show the WebSocket request item in listView when 'connect' is called. WebSocketInterceptor.setConnectCallback( (url, protocols, options, socketId) => { const socketIndex = this.state.requests.length; this._socketIdMap[socketId] = socketIndex; const _webSocket: NetworkRequestInfo = { id: socketIndex, type: 'WebSocket', url: url, protocols: protocols, }; this.setState( { requests: this.state.requests.concat(_webSocket), }, this._scrollRequestsToBottom, ); }, ); WebSocketInterceptor.setCloseCallback( (statusCode, closeReason, socketId) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } if (statusCode !== null && closeReason !== null) { this.setState(({requests}) => { const networkRequestInfo = requests[socketIndex]; networkRequestInfo.status = statusCode; networkRequestInfo.closeReason = closeReason; return {requests}; }); } }, ); WebSocketInterceptor.setSendCallback((data, socketId) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } this.setState(({requests}) => { const networkRequestInfo = requests[socketIndex]; if (!networkRequestInfo.messages) { networkRequestInfo.messages = ''; } networkRequestInfo.messages += 'Sent: ' + JSON.stringify(data) + '\n'; return {requests}; }); }); WebSocketInterceptor.setOnMessageCallback((socketId, message) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } this.setState(({requests}) => { const networkRequestInfo = requests[socketIndex]; if (!networkRequestInfo.messages) { networkRequestInfo.messages = ''; } networkRequestInfo.messages += 'Received: ' + JSON.stringify(message) + '\n'; return {requests}; }); }); WebSocketInterceptor.setOnCloseCallback((socketId, message) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } this.setState(({requests}) => { const networkRequestInfo = requests[socketIndex]; networkRequestInfo.serverClose = message; return {requests}; }); }); WebSocketInterceptor.setOnErrorCallback((socketId, message) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } this.setState(({requests}) => { const networkRequestInfo = requests[socketIndex]; networkRequestInfo.serverError = message; return {requests}; }); }); // Fire above callbacks. WebSocketInterceptor.enableInterception(); }
_enableWebSocketInterception(): void { if (WebSocketInterceptor.isInterceptorEnabled()) { return; } // Show the WebSocket request item in listView when 'connect' is called. WebSocketInterceptor.setConnectCallback( (url, protocols, options, socketId) => { const socketIndex = this._requests.length; this._socketIdMap[socketId] = socketIndex; const _webSocket: NetworkRequestInfo = { 'type': 'WebSocket', 'url': url, 'protocols': protocols, }; this._requests.push(_webSocket); this._detailViewItems.push([]); this._genDetailViewItem(socketIndex); this.setState( {dataSource: this._listViewDataSource.cloneWithRows(this._requests)}, this._scrollToBottom(), ); } ); WebSocketInterceptor.setCloseCallback( (statusCode, closeReason, socketId) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } if (statusCode !== null && closeReason !== null) { this._requests[socketIndex].status = statusCode; this._requests[socketIndex].closeReason = closeReason; } this._genDetailViewItem(socketIndex); } ); WebSocketInterceptor.setSendCallback((data, socketId) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } if (!this._requests[socketIndex].messages) { this._requests[socketIndex].messages = ''; } this._requests[socketIndex].messages += 'Sent: ' + JSON.stringify(data) + '\n'; this._genDetailViewItem(socketIndex); }); WebSocketInterceptor.setOnMessageCallback((socketId, message) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } if (!this._requests[socketIndex].messages) { this._requests[socketIndex].messages = ''; } this._requests[socketIndex].messages += 'Received: ' + JSON.stringify(message) + '\n'; this._genDetailViewItem(socketIndex); }); WebSocketInterceptor.setOnCloseCallback((socketId, message) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } this._requests[socketIndex].serverClose = message; this._genDetailViewItem(socketIndex); }); WebSocketInterceptor.setOnErrorCallback((socketId, message) => { const socketIndex = this._socketIdMap[socketId]; if (socketIndex === undefined) { return; } this._requests[socketIndex].serverError = message; this._genDetailViewItem(socketIndex); }); // Fire above callbacks. WebSocketInterceptor.enableInterception(); }