| 1652 | const BadConnectionError = scErrors.BadConnectionError; |
| 1653 | |
| 1654 | function AGTransport(authEngine, codecEngine, options, wsOptions, handlers) { |
| 1655 | this.state = this.CLOSED; |
| 1656 | this.auth = authEngine; |
| 1657 | this.codec = codecEngine; |
| 1658 | this.options = options; |
| 1659 | this.wsOptions = wsOptions; |
| 1660 | this.protocolVersion = options.protocolVersion; |
| 1661 | this.connectTimeout = options.connectTimeout; |
| 1662 | this.pingTimeout = options.pingTimeout; |
| 1663 | this.pingTimeoutDisabled = !!options.pingTimeoutDisabled; |
| 1664 | this.callIdGenerator = options.callIdGenerator; |
| 1665 | this.authTokenName = options.authTokenName; |
| 1666 | this.isBufferingBatch = false; |
| 1667 | |
| 1668 | this._pingTimeoutTicker = null; |
| 1669 | this._callbackMap = {}; |
| 1670 | this._batchBuffer = []; |
| 1671 | |
| 1672 | if (!handlers) { |
| 1673 | handlers = {}; |
| 1674 | } |
| 1675 | |
| 1676 | this._onOpenHandler = handlers.onOpen || function () {}; |
| 1677 | this._onOpenAbortHandler = handlers.onOpenAbort || function () {}; |
| 1678 | this._onCloseHandler = handlers.onClose || function () {}; |
| 1679 | this._onEventHandler = handlers.onEvent || function () {}; |
| 1680 | this._onErrorHandler = handlers.onError || function () {}; |
| 1681 | this._onInboundInvokeHandler = handlers.onInboundInvoke || function () {}; |
| 1682 | this._onInboundTransmitHandler = handlers.onInboundTransmit || function () {}; |
| 1683 | |
| 1684 | // Open the connection. |
| 1685 | |
| 1686 | this.state = this.CONNECTING; |
| 1687 | let uri = this.uri(); |
| 1688 | |
| 1689 | let wsSocket = createWebSocket(uri, wsOptions); |
| 1690 | wsSocket.binaryType = this.options.binaryType; |
| 1691 | |
| 1692 | this.socket = wsSocket; |
| 1693 | |
| 1694 | wsSocket.onopen = () => { |
| 1695 | this._onOpen(); |
| 1696 | }; |
| 1697 | |
| 1698 | wsSocket.onclose = async (event) => { |
| 1699 | let code; |
| 1700 | if (event.code == null) { |
| 1701 | // This is to handle an edge case in React Native whereby |
| 1702 | // event.code is undefined when the mobile device is locked. |
| 1703 | // TODO: This is not ideal since this condition could also apply to |
| 1704 | // an abnormal close (no close control frame) which would be a 1006. |
| 1705 | code = 1005; |
| 1706 | } else { |
| 1707 | code = event.code; |
| 1708 | } |
| 1709 | this._destroy(code, event.reason); |
| 1710 | }; |
| 1711 | |