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CAAC,OAAO,EAAE;wCACd,IAAI,KAAK,CAAC,QAAQ,KAAK,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,EAAE;4CACjD,KAAI,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,wCAAsC,KAAK,CAAC,IAAI,UAAK,KAAK,CAAC,MAAM,MAAG,CAAC,CAAC,CAAC;yCACjG;6CAAM;4CACH,KAAI,CAAC,OAAO,EAAE,CAAC;yCAClB;qCACJ;gCACL,CAAC,CAAC;4BACN,CAAC,CAAC,EAAC;;;;KACN;IAEM,iCAAI,GAAX,UAAY,IAAS;QACjB,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,UAAU,KAAK,SAAS,CAAC,IAAI,EAAE;YAChE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,0CAAwC,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,iBAAiB,CAAC,MAAG,CAAC,CAAC;YACxH,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC1B,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;SAC5B;QAED,OAAO,OAAO,CAAC,MAAM,CAAC,oCAAoC,CAAC,CAAC;IAChE,CAAC;IAEM,iCAAI,GAAX;QACI,IAAI,IAAI,CAAC,SAAS,EAAE;YAChB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;YACvB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;SACzB;QACD,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC7B,CAAC;IAIL,yBAAC;AAAD,CAAC,AAvFD,IAuFC","sourcesContent":["// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { ITransport, TransferFormat } from \"./ITransport\";\r\nimport { Arg, getDataDetail } from \"./Utils\";\r\n\r\n/** @private */\r\nexport class WebSocketTransport implements ITransport {\r\n private readonly logger: ILogger;\r\n private readonly accessTokenFactory: () => string | Promise;\r\n private readonly logMessageContent: boolean;\r\n private webSocket: WebSocket;\r\n\r\n constructor(accessTokenFactory: () => string | Promise, logger: ILogger, logMessageContent: boolean) {\r\n this.logger = logger;\r\n this.accessTokenFactory = accessTokenFactory || (() => null);\r\n this.logMessageContent = logMessageContent;\r\n }\r\n\r\n public async connect(url: string, transferFormat: TransferFormat): Promise {\r\n Arg.isRequired(url, \"url\");\r\n Arg.isRequired(transferFormat, \"transferFormat\");\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n if (typeof (WebSocket) === \"undefined\") {\r\n throw new Error(\"'WebSocket' is not supported in your environment.\");\r\n }\r\n\r\n this.logger.log(LogLevel.Trace, \"(WebSockets transport) Connecting\");\r\n\r\n const token = await this.accessTokenFactory();\r\n if (token) {\r\n url += (url.indexOf(\"?\") < 0 ? \"?\" : \"&\") + `access_token=${encodeURIComponent(token)}`;\r\n }\r\n\r\n return new Promise((resolve, reject) => {\r\n url = url.replace(/^http/, \"ws\");\r\n const webSocket = new WebSocket(url);\r\n if (transferFormat === TransferFormat.Binary) {\r\n webSocket.binaryType = \"arraybuffer\";\r\n }\r\n\r\n // tslint:disable-next-line:variable-name\r\n webSocket.onopen = (_event: Event) => {\r\n this.logger.log(LogLevel.Information, `WebSocket connected to ${url}`);\r\n this.webSocket = webSocket;\r\n resolve();\r\n };\r\n\r\n webSocket.onerror = (event: ErrorEvent) => {\r\n reject(event.error);\r\n };\r\n\r\n webSocket.onmessage = (message: MessageEvent) => {\r\n this.logger.log(LogLevel.Trace, `(WebSockets transport) data received. ${getDataDetail(message.data, this.logMessageContent)}.`);\r\n if (this.onreceive) {\r\n this.onreceive(message.data);\r\n }\r\n };\r\n\r\n webSocket.onclose = (event: CloseEvent) => {\r\n // webSocket will be null if the transport did not start successfully\r\n this.logger.log(LogLevel.Trace, \"(WebSockets transport) socket closed.\");\r\n if (this.onclose) {\r\n if (event.wasClean === false || event.code !== 1000) {\r\n this.onclose(new Error(`Websocket closed with status code: ${event.code} (${event.reason})`));\r\n } else {\r\n this.onclose();\r\n }\r\n }\r\n };\r\n });\r\n }\r\n\r\n public send(data: any): Promise {\r\n if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {\r\n this.logger.log(LogLevel.Trace, `(WebSockets transport) sending data. ${getDataDetail(data, this.logMessageContent)}.`);\r\n this.webSocket.send(data);\r\n return Promise.resolve();\r\n }\r\n\r\n return Promise.reject(\"WebSocket is not in the OPEN state\");\r\n }\r\n\r\n public stop(): Promise {\r\n if (this.webSocket) {\r\n this.webSocket.close();\r\n this.webSocket = null;\r\n }\r\n return Promise.resolve();\r\n }\r\n\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (error?: Error) => void;\r\n}\r\n"]}