离子骨架+ cordova。 WebSocket在Android发行版中不起作用,但调试版本可以正常工作

时间:2017-02-02 15:37:34

标签: android cordova ionic-framework websocket release

使用此日志:

WebSocket连接到' wss://xxxxxx.net/'失败:WebSocket开放握手被取消"

" TransportWebSocket.errorHandler - 错误{}"

" TransportWebSocket.closeHandler - 错误{"代码":1006,"文字":""}"

请帮助解决此问题。

sp.external.TransportWebSocket = function () {
        var self = this;
        var socket = null;
        var isOpen = false;
        var listSendMsg = [];

        this.openHandler = null;
        this.closeHandler = null;
        this.messageHandler = null;
        this.errorHandler = null;
        this.serverURL = '';

        this.send = function (_data, _highPrioritetFlag) {
            utils.logger.debug('TransportWebSocket', 'send', 'Open status:' + isOpen);

            _highPrioritetFlag = _highPrioritetFlag || false;

            if (isOpen) {
                if (_data && typeof _data === 'object') {
                    try {
                        socket.send(JSON.stringify(_data));
                    }
                    catch (error) {
                        throw new Error('Invalid JSON stringify');
                    }
                }
                else {
                    throw new Error('The data sent must be an object');
                }
            }
            else {
                if (_highPrioritetFlag) {
                    listSendMsg.unshift(_data);
                }
                else {
                    listSendMsg.push(_data);
                }

                openSocket();
            }
        };

        this.reopen = function () {
            utils.logger.debug('TransportWebSocket', 'reopen');
            openSocket();
        };

        this.close = function (_code) {
            utils.logger.debug('TransportWebSocket', 'close');
            socket.close(_code);
        };

        function procListMsg() {
            utils.logger.debug('TransportWebSocket', 'procListMsg', 'Processing buffer messages, items:' + listSendMsg.length);
            if (listSendMsg.length > 0) {
                self.send(listSendMsg.shift());
                setTimeout(procListMsg, 20);
            }
        }

        function openHandler() {
            utils.logger.debug('TransportWebSocket', 'openHandler', 'WebSocket.readyState:' + socket.readyState);
            if (socket.readyState === WebSocket.OPEN) {
                isOpen = true;

                procListMsg();

                if (self.openHandler) {
                    self.openHandler();
                }
            }
        }

        function closeHandler(error) {
            utils.logger.debug('TransportWebSocket', 'closeHandler', 'Error', {code: error.code, text: error.reason});
            isOpen = false;

            if (socket.readyState === WebSocket.CLOSED) {

                if (self.closeHandler) {
                    self.closeHandler(error);
                }
            }
        }

        function errorHandler(error) {
            utils.logger.debug('TransportWebSocket', 'errorHandler', 'Error', {code: error.code, text: error.reason});
            isOpen = false;

            if (self.errorHandler) {
                self.errorHandler(error);
            }
        }

        function messageHandler(e) {
            var msg = null;

            if (self.messageHandler) {
                try {
                    msg = JSON.parse(e.data);
                }
                catch (error) {
                    throw new Error('Invalid JSON format in response: ' + e.data);
                }

                self.messageHandler(msg);
            }
        }

        function openSocket() {
            utils.logger.debug('TransportWebSocket', 'openSocket', 'Opening');
            socket = new WebSocket(self.serverURL);
            socket.onopen = openHandler;
            socket.onclose = closeHandler;
            socket.onmessage = messageHandler;
            socket.onerror = errorHandler;
        }
    };

1 个答案:

答案 0 :(得分:0)

我不知道你是否已经解决了这个问题,但对于遇到同样问题并且找到这个帖子的人,你可以查看cordova "release" behaves differently to "debug" regarding SSL

简而言之,修改使调试和发布之间的行为不同的文件: platforms/android/CordovaLib/src/org/apache/cordova/engine/SystemWebViewClient.java

注释方法onReceivedSslError的内容并将其替换为

handler.proceed();
return;