File tree Expand file tree Collapse file tree 3 files changed +48
-3
lines changed
packages/plugins/lesy-plugin-pilot/src Expand file tree Collapse file tree 3 files changed +48
-3
lines changed Original file line number Diff line number Diff line change 1+ // tslint:disable-next-line: import-name
2+ import WebSocket from "ws" ;
3+ import { WebSocketBus } from "../pilot.socket" ;
4+
5+ describe ( "web socket" , ( ) => {
6+ let wsServer : WebSocketBus ;
7+ let wsClient : WebSocket ;
8+
9+ beforeAll ( ( done ) => {
10+ wsServer = new WebSocketBus ( ) . startServer ( "localhost" , 1234 ) . init ( {
11+ requestRunCommand : ( payload ) => {
12+ return {
13+ success : true ,
14+ data : payload ,
15+ } ;
16+ } ,
17+ } ) ;
18+
19+ wsServer [ "ws" ] . on ( "connection" , ( ) => wsClient . on ( "open" , done ) ) ;
20+ wsClient = new WebSocket ( `ws://localhost:1234` ) ;
21+ } ) ;
22+
23+ afterAll ( ( done ) => {
24+ wsClient . close ( ) ;
25+ wsServer . close ( done ) ;
26+ } ) ;
27+
28+ it ( "should exchange messages" , ( done ) => {
29+ wsClient . on ( "message" , ( m ) => {
30+ expect ( m ) . toEqual (
31+ JSON . stringify ( {
32+ success : true ,
33+ data : "echo 123" ,
34+ } ) ,
35+ ) ;
36+ done ( ) ;
37+ } ) ;
38+
39+ wsClient . send (
40+ JSON . stringify ( { REQUEST : "requestRunCommand" , PAYLOAD : "echo 123" } ) ,
41+ ) ;
42+ } ) ;
43+ } ) ;
Original file line number Diff line number Diff line change @@ -65,7 +65,7 @@ export default class PilotCommand {
6565 [ currCtx . feature . pkg . name ] : currCtx ,
6666 } ;
6767 global . lesySelectedProject = currCtx . feature . pkg . name ;
68- const projectsPaths = await this . fetchProjectPaths ( ) ;
68+ const projectsPaths = await this . fetchProjectPaths ( ) ; // todo: remove same project dup
6969 for ( let i = 0 ; i < projectsPaths . length ; i = i + 1 ) {
7070 const p = await require ( projectsPaths [ i ] ) . default ;
7171 global . lesyWorkspace [ p . feature . pkg . name ] = p . localState ;
Original file line number Diff line number Diff line change @@ -37,8 +37,10 @@ export class WebSocketBus {
3737 return this . receiver . asObservable ( ) ;
3838 }
3939
40- close ( ) {
41- this . ws . close ( ) ;
40+ close ( cb ?: ( param : any ) => void ) {
41+ this . sender . unsubscribe ( ) ;
42+ this . receiver . unsubscribe ( ) ;
43+ this . ws . close ( cb ) ;
4244 }
4345
4446 private onConnectionOpen ( ) {
You can’t perform that action at this time.
0 commit comments