From c6261cc039a07cf681b1ef3439815b878d33f6ac Mon Sep 17 00:00:00 2001 From: kungfuboy <17kungfuboy@gmail.com> Date: Tue, 13 Sep 2022 23:07:13 +0800 Subject: [PATCH] feat: form reset --- .../src/app/pages/api/api-tab.service.ts | 3 +- .../api/websocket/websocket.component.html | 138 ++++++++++++++---- .../api/websocket/websocket.component.ts | 19 ++- src/workbench/node/server/socketio.js | 15 +- 4 files changed, 135 insertions(+), 40 deletions(-) diff --git a/src/workbench/browser/src/app/pages/api/api-tab.service.ts b/src/workbench/browser/src/app/pages/api/api-tab.service.ts index 3000dd48..9a6a6f8f 100644 --- a/src/workbench/browser/src/app/pages/api/api-tab.service.ts +++ b/src/workbench/browser/src/app/pages/api/api-tab.service.ts @@ -166,7 +166,8 @@ export class ApiTabService { replaceTab.extends.method = model.method; if (currentTab.module === 'test') { if (currentTab.pathname === '/home/api/ws/test') { - replaceTab.extends.method = model.request.protocol.toUpperCase(); + console.log('model', model); + replaceTab.extends.method = model.request.protocol?.toUpperCase(); } else { replaceTab.extends.method = model.request.method; } diff --git a/src/workbench/browser/src/app/pages/api/websocket/websocket.component.html b/src/workbench/browser/src/app/pages/api/websocket/websocket.component.html index 64d08261..cc836b72 100644 --- a/src/workbench/browser/src/app/pages/api/websocket/websocket.component.html +++ b/src/workbench/browser/src/app/pages/api/websocket/websocket.component.html @@ -2,27 +2,53 @@ - + - + - + Connect Connecting - + Disconnect @@ -30,31 +56,58 @@ - + - Headers + Headers + {{ + model.request.requestHeaders | apiParamsNum + }} - + - Query Params + Query Params + {{ + model.request.queryParams | apiParamsNum + }} - + Message - + @@ -62,8 +115,14 @@ - + Send @@ -84,29 +143,37 @@ - + {{ item.msg }} - + {{ item.msg }} - + {{ item.title }} - + @@ -114,10 +181,17 @@ - + }" + [eventList]="['type', 'format', 'copy', 'search']" + (codeChange)="rawDataChange($event)" + > diff --git a/src/workbench/browser/src/app/pages/api/websocket/websocket.component.ts b/src/workbench/browser/src/app/pages/api/websocket/websocket.component.ts index 9d5eb6cb..6d05645a 100644 --- a/src/workbench/browser/src/app/pages/api/websocket/websocket.component.ts +++ b/src/workbench/browser/src/app/pages/api/websocket/websocket.component.ts @@ -12,6 +12,8 @@ import { ElectronService } from 'eo/workbench/browser/src/app/core/services'; import { Subject, takeUntil } from 'rxjs'; import { ModalService } from '../../../shared/services/modal.service'; import { isEmptyObj } from 'eo/workbench/browser/src/app/utils'; +import { ApiParamsNumPipe } from '../../../shared/pipes/api-param-num.pipe'; + import { ApiTestHeaders, ApiTestQuery } from 'eo/workbench/browser/src/app/shared/services/api-test/api-test.model'; interface testViewModel { requestTabIndex: number; @@ -70,7 +72,7 @@ export class WebsocketComponent implements OnInit, OnDestroy { const historyData: unknown = await this.testService.getHistory(Number(id.replace('history_', ''))); this.model = historyData as testViewModel; } - console.log(this.model); + // console.log(this.model); } this.watchBasicForm(); this.eoOnInit.emit(this.model); @@ -182,6 +184,10 @@ export class WebsocketComponent implements OnInit, OnDestroy { this.listen(); } + bindGetApiParamNum(params) { + return new ApiParamsNumPipe().transform(params); + } + handleSendMsg() { // * 通过 SocketIO 通知后端 // send a message to the server @@ -246,7 +252,7 @@ export class WebsocketComponent implements OnInit, OnDestroy { } else { this.model.response.responseBody.unshift({ type: 'end', - msg: `Disconnect by ${this.getLink()}`, + msg: `Error by ${this.getLink()}`, isExpand: false, }); this.wsStatus = 'disconnect'; @@ -265,6 +271,7 @@ export class WebsocketComponent implements OnInit, OnDestroy { } checkTabCanLeave = () => { if (this.wsStatus === 'disconnect') { + this.resetForm(); return true; } return new Promise((resolve) => { @@ -278,6 +285,7 @@ export class WebsocketComponent implements OnInit, OnDestroy { type: 'primary', onClick: () => { modal.destroy(); + this.resetForm(); // * disconnect ws connect this.handleConnect('disconnect'); resolve(true); @@ -299,10 +307,10 @@ export class WebsocketComponent implements OnInit, OnDestroy { const link = /^(wss:\/{2})|(ws:\/{2})\S+$/m.test(uri.trim()) ? uri.trim() : protocol + '://' + uri.trim().replace('//', ''); - console.log('link', link); + // console.log('link', link); return link; } - private resetModel() { + private resetModel(): testViewModel { return { requestTabIndex: 2, protocol: 'websocket', @@ -320,6 +328,9 @@ export class WebsocketComponent implements OnInit, OnDestroy { }, }; } + resetForm() { + this.validateForm.reset(); + } private checkForm(): boolean { for (const i in this.validateForm.controls) { if (this.validateForm.controls.hasOwnProperty(i)) { diff --git a/src/workbench/node/server/socketio.js b/src/workbench/node/server/socketio.js index 108b4eee..62f85dd3 100644 --- a/src/workbench/node/server/socketio.js +++ b/src/workbench/node/server/socketio.js @@ -11,6 +11,13 @@ const socket = (port = 4301) => { // send a message to the client socket.emit('ws-client', 'link success'); let ws = null; + + const unlisten = () => { + ws.on('close', null); + ws.on('upgrade', null); + ws.on('message', null); + ws = null; + }; // receive a message from the client socket.on('ws-server', ({ type, content }) => { if (type === 'connect') { @@ -39,10 +46,11 @@ const socket = (port = 4301) => { }); ws.on('error', (err) => { socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: err }); + unlisten(); }); } catch (error) { socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: error }); - ws = null; + unlisten(); return; } @@ -67,10 +75,11 @@ const socket = (port = 4301) => { ws.on('close', () => { socket.emit('ws-client', { - type: 'ws-message-back', - status: 1, + type: 'ws-connect-back', + status: -1, content: 'Server disconnected', }); + unlisten(); }); } if (type === 'ws-message') {