96 lines
No EOL
4.6 KiB
JavaScript
96 lines
No EOL
4.6 KiB
JavaScript
"use strict";
|
|
/**
|
|
* @module botframework-streaming
|
|
*/
|
|
/**
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License.
|
|
*/
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.WebSocketClient = void 0;
|
|
const protocolAdapter_1 = require("../protocolAdapter");
|
|
const payloads_1 = require("../payloads");
|
|
const payloadTransport_1 = require("../payloadTransport");
|
|
const nodeWebSocket_1 = require("./nodeWebSocket");
|
|
const webSocketTransport_1 = require("./webSocketTransport");
|
|
/**
|
|
* Web socket based client to be used as streaming transport.
|
|
*/
|
|
class WebSocketClient {
|
|
/**
|
|
* Creates a new instance of the [WebSocketClient](xref:botframework-streaming.WebSocketClient) class.
|
|
*
|
|
* @param config For configuring a [WebSocketClient](xref:botframework-streaming.WebSocketClient) instance to communicate with a WebSocket server.
|
|
* @param config.url The URL of the remote server to connect to.
|
|
* @param config.requestHandler The [RequestHandler](xref:botframework-streaming.RequestHandler) used to process incoming messages received by this client.
|
|
* @param config.disconnectionHandler Optional function to handle the disconnection message.
|
|
*/
|
|
constructor({ url, requestHandler, disconnectionHandler = null, }) {
|
|
this._url = url;
|
|
this._requestHandler = requestHandler;
|
|
this._disconnectionHandler = disconnectionHandler;
|
|
this._requestManager = new payloads_1.RequestManager();
|
|
this._sender = new payloadTransport_1.PayloadSender();
|
|
this._sender.disconnected = this.onConnectionDisconnected.bind(this);
|
|
this._receiver = new payloadTransport_1.PayloadReceiver();
|
|
this._receiver.disconnected = this.onConnectionDisconnected.bind(this);
|
|
this._protocolAdapter = new protocolAdapter_1.ProtocolAdapter(this._requestHandler, this._requestManager, this._sender, this._receiver);
|
|
}
|
|
/**
|
|
* Establish a connection with no custom headers.
|
|
*
|
|
* @returns A promise that will not resolve until the client stops listening for incoming messages.
|
|
*/
|
|
connect() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const ws = new nodeWebSocket_1.NodeWebSocket();
|
|
try {
|
|
yield ws.connect(this._url);
|
|
const transport = new webSocketTransport_1.WebSocketTransport(ws);
|
|
this._sender.connect(transport);
|
|
this._receiver.connect(transport);
|
|
}
|
|
catch (_error) {
|
|
throw new Error('Unable to connect client to Node transport.');
|
|
}
|
|
});
|
|
}
|
|
/**
|
|
* Stop this client from listening.
|
|
*/
|
|
disconnect() {
|
|
this._sender.disconnect(new payloadTransport_1.TransportDisconnectedEvent('Disconnect was called.'));
|
|
this._receiver.disconnect(new payloadTransport_1.TransportDisconnectedEvent('Disconnect was called.'));
|
|
}
|
|
/**
|
|
* Task used to send data over this client connection.
|
|
*
|
|
* @param request The [StreamingRequest](xref:botframework-streaming.StreamingRequest) instance to send.
|
|
* @returns A promise that will produce an instance of receive response on completion of the send operation.
|
|
*/
|
|
send(request) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return this._protocolAdapter.sendRequest(request);
|
|
});
|
|
}
|
|
onConnectionDisconnected(sender, args) {
|
|
// Rejects all pending requests on disconnect.
|
|
this._requestManager.rejectAllResponses(new Error('Disconnect was called.'));
|
|
if (this._disconnectionHandler != null) {
|
|
this._disconnectionHandler('Disconnected');
|
|
return;
|
|
}
|
|
throw new Error(`Unable to re-connect client to Node transport for url ${this._url}. Sender: '${JSON.stringify(sender)}'. Args:' ${JSON.stringify(args)}`);
|
|
}
|
|
}
|
|
exports.WebSocketClient = WebSocketClient;
|
|
//# sourceMappingURL=nodeWebSocketClient.js.map
|