• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microsoft / botbuilder-js / 12431963409

20 Dec 2024 01:05PM CUT coverage: 84.624% (-0.001%) from 84.625%
12431963409

Pull #4823

github

web-flow
Merge 04876b140 into 3b8fcab21
Pull Request #4823: fix: [#4684] ESLint issues in botbuilder-testing

8185 of 10821 branches covered (75.64%)

Branch coverage included in aggregate %.

15 of 15 new or added lines in 1 file covered. (100.0%)

20512 of 23090 relevant lines covered (88.83%)

7391.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.76
/libraries/botframework-streaming/src/webSocket/browserWebSocketClient.ts
1
/**
8✔
2
 * @module botframework-streaming
3
 */
4
/**
5
 * Copyright (c) Microsoft Corporation. All rights reserved.
6
 * Licensed under the MIT License.
7
 */
8

9
import { BrowserWebSocket } from './browserWebSocket';
10
import { IReceiveResponse, IStreamingTransportClient } from '../interfaces';
11
import { PayloadReceiver, PayloadSender, TransportDisconnectedEvent } from '../payloadTransport';
12
import { ProtocolAdapter } from '../protocolAdapter';
13
import { RequestHandler } from '../requestHandler';
14
import { RequestManager } from '../payloads';
15
import { StreamingRequest } from '../streamingRequest';
16
import { WebSocketTransport } from './webSocketTransport';
17

18
/**
19
 * Web socket based client to be used as streaming transport.
20
 */
21
export class WebSocketClient implements IStreamingTransportClient {
2✔
22
    private readonly _url: string;
23
    private readonly _requestHandler: RequestHandler;
24
    private readonly _sender: PayloadSender;
25
    private readonly _receiver: PayloadReceiver;
26
    private readonly _requestManager: RequestManager;
27
    private readonly _protocolAdapter: ProtocolAdapter;
28
    private readonly _disconnectionHandler: (message: string) => void;
29

30
    /**
31
     * Creates a new instance of the [WebSocketClient](xref:botframework-streaming.WebSocketClient) class.
32
     *
33
     * @param config For configuring a [WebSocketClient](xref:botframework-streaming.WebSocketClient) instance to communicate with a WebSocket server.
34
     * @param config.url The URL of the remote server to connect to.
35
     * @param config.requestHandler The [RequestHandler](xref:botframework-streaming.RequestHandler) used to process incoming messages received by this client.
36
     * @param config.disconnectionHandler Optional function to handle the disconnection message.
37
     */
38
    constructor({
39
        url,
40
        requestHandler,
41
        disconnectionHandler = null,
×
42
    }: {
43
        url: string;
44
        requestHandler: RequestHandler;
45
        disconnectionHandler: (message: string) => void;
46
    }) {
47
        this._url = url;
×
48
        this._requestHandler = requestHandler;
49
        this._disconnectionHandler = disconnectionHandler;
50

51
        this._requestManager = new RequestManager();
52

53
        this._sender = new PayloadSender();
54
        this._sender.disconnected = this.onConnectionDisconnected.bind(this);
55
        this._receiver = new PayloadReceiver();
56
        this._receiver.disconnected = this.onConnectionDisconnected.bind(this);
57

58
        this._protocolAdapter = new ProtocolAdapter(
59
            this._requestHandler,
60
            this._requestManager,
61
            this._sender,
62
            this._receiver
63
        );
64
    }
65

66
    /**
67
     * Establish a connection with no custom headers.
68
     *
69
     * @returns A promise that will not resolve until the client stops listening for incoming messages.
70
     */
71
    async connect(): Promise<void> {
×
72
        const ws = new BrowserWebSocket();
×
73
        await ws.connect(this._url);
×
74
        const transport = new WebSocketTransport(ws);
×
75
        this._sender.connect(transport);
×
76
        this._receiver.connect(transport);
77
    }
78

79
    /**
80
     * Stop this client from listening.
81
     */
82
    disconnect(): void {
83
        this._sender.disconnect(new TransportDisconnectedEvent('Disconnect was called.'));
×
84
        this._receiver.disconnect(new TransportDisconnectedEvent('Disconnect was called.'));
85
    }
86

87
    /**
88
     * Task used to send data over this client connection.
89
     *
90
     * @param request The streaming request to send.
91
     * @returns A promise that will produce an instance of receive response on completion of the send operation.
92
     */
93
    async send(request: StreamingRequest): Promise<IReceiveResponse> {
×
94
        return this._protocolAdapter.sendRequest(request);
×
95
    }
96

97
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
    private onConnectionDisconnected(sender: Record<string, unknown>, args: any): void {
99
        // Rejects all pending requests on disconnect.
100
        this._requestManager.rejectAllResponses(new Error('Disconnect was called.'));
101

102
        if (this._disconnectionHandler != null) {
×
103
            this._disconnectionHandler('Disconnected');
×
104
            return;
×
105
        }
106

107
        throw new Error(
×
108
            `Unable to re-connect client to transport for url ${this._url}. Sender: '${JSON.stringify(
109
                sender
110
            )}'. Args:' ${JSON.stringify(args)}`
111
        );
112
    }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc