diff --git a/libraries/botbuilder/src/botFrameworkAdapter.ts b/libraries/botbuilder/src/botFrameworkAdapter.ts index 4fe7132cf9..c9ed62ed79 100644 --- a/libraries/botbuilder/src/botFrameworkAdapter.ts +++ b/libraries/botbuilder/src/botFrameworkAdapter.ts @@ -136,12 +136,7 @@ export interface BotFrameworkAdapterSettings { channelService?: string; /** - * Optional. The option to determine if this adapter accepts WebSocket connections - */ - enableWebSockets?: boolean; - - /** - * Optional. Used to pass in a NodeWebSocketFactoryBase instance. Allows bot to accept WebSocket connections. + * Optional. Used to pass in a NodeWebSocketFactoryBase instance. */ webSocketFactory?: NodeWebSocketFactoryBase; } @@ -269,12 +264,7 @@ export class BotFrameworkAdapter extends BotAdapter implements IUserTokenProvide this.credentials.oAuthScope = GovernmentConstants.ToChannelFromBotOAuthScope; } - // If the developer wants to use WebSockets, but didn't provide a WebSocketFactory, - // create a NodeWebSocketFactory. - if (this.settings.enableWebSockets && !this.settings.webSocketFactory) { - this.webSocketFactory = new NodeWebSocketFactory(); - } - + // If a NodeWebSocketFactoryBase was passed in, set it on the BotFrameworkAdapter. if (this.settings.webSocketFactory) { this.webSocketFactory = this.settings.webSocketFactory; } @@ -1146,13 +1136,14 @@ export class BotFrameworkAdapter extends BotAdapter implements IUserTokenProvide /** * Process the initial request to establish a long lived connection via a streaming server. * @param req The connection request. - * @param res The response sent on error or connection termination. - * @param logic The logic that will handle incoming requests. + * @param socket The raw socket connection between the bot (server) and channel/caller (client). + * @param head The first packet of the upgraded stream. + * @param logic The logic that handles incoming streaming requests for the lifetime of the WebSocket connection. */ - public async useWebSocket(req: IncomingMessage, socket: Socket, head: Buffer, logic: (context: TurnContext) => Promise): Promise { - if (!this.webSocketFactory || !this.webSocketFactory.createWebSocket) { - throw new Error('BotFrameworkAdapter must have a WebSocketFactory in order to support streaming.'); - } + public async useWebSocket(req: IncomingMessage, socket: Socket, head: Buffer, logic: (context: TurnContext) => Promise): Promise { + // Use the provided NodeWebSocketFactoryBase on BotFrameworkAdapter construction, + // otherwise create a new NodeWebSocketFactory. + const webSocketFactory = this.webSocketFactory || new NodeWebSocketFactory(); if (!logic) { throw new Error('Streaming logic needs to be provided to `useWebSocket`'); @@ -1177,7 +1168,7 @@ export class BotFrameworkAdapter extends BotAdapter implements IUserTokenProvide throw err; } - const nodeWebSocket = await this.webSocketFactory.createWebSocket(req, socket, head); + const nodeWebSocket = await webSocketFactory.createWebSocket(req, socket, head); await this.startWebSocket(nodeWebSocket); } diff --git a/libraries/botbuilder/tests/streaming/botFrameworkAdapterStreaming.test.js b/libraries/botbuilder/tests/streaming/botFrameworkAdapterStreaming.test.js index 0d119eac56..c4a1a92e35 100644 --- a/libraries/botbuilder/tests/streaming/botFrameworkAdapterStreaming.test.js +++ b/libraries/botbuilder/tests/streaming/botFrameworkAdapterStreaming.test.js @@ -16,10 +16,9 @@ const createNetSocket = (readable = true, writable = true) => { }; class TestAdapterSettings { - constructor(appId = undefined, appPassword = undefined, channelAuthTenant, oAuthEndpoint, openIdMetadata, channelServce) { + constructor(appId, appPassword) { this.appId = appId; this.appPassword = appPassword; - this.enableWebSockets = true; } }