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

microsoft / botbuilder-js / 13264809812

11 Feb 2025 02:09PM CUT coverage: 84.524%. Remained the same
13264809812

Pull #4856

github

web-flow
Merge 4e615f951 into 7534989ce
Pull Request #4856: fix: [#4786] Clarify createBotFrameworkAuthenticationFromConfiguration usage in yo templates

8205 of 10860 branches covered (75.55%)

Branch coverage included in aggregate %.

20555 of 23166 relevant lines covered (88.73%)

4123.74 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
/**
4✔
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 {
1✔
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
    private onConnectionDisconnected(sender: Record<string, unknown>, args: any): void {
98
        // Rejects all pending requests on disconnect.
99
        this._requestManager.rejectAllResponses(new Error('Disconnect was called.'));
100

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

106
        throw new Error(
×
107
            `Unable to re-connect client to transport for url ${this._url}. Sender: '${JSON.stringify(
108
                sender,
109
            )}'. Args:' ${JSON.stringify(args)}`,
110
        );
111
    }
112
}
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