Ionic (CORDOVA)应用程序未在发布模式下连接到套接字API

vyswwuz2  于 2023-01-15  发布在  Ionic
关注(0)|答案(2)|浏览(124)
  • (注意:这只发生在标记--release上)*

问题

我最近构建了一个基于套接字连接的ionic应用程序,当我处于调试模式时一切正常。然而,当我移动到release(--release属性)时,应用程序不再连接到api端点。套接字正在显示以下消息xhr poll error

预期会发生什么?

连接到API端点。

到底发生了什么?

不连接到API端点。

信息

我目前正在使用以下软件包

"dependencies": {
    "@angular/animations": "^8.2.14",
    "@angular/common": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/forms": "~8.1.2",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@ionic-native/call-number": "^5.20.0",
    "@ionic-native/core": "^5.19.1",
    "@ionic-native/diagnostic": "^5.19.1",
    "@ionic-native/file": "^5.19.1",
    "@ionic-native/file-transfer": "^5.19.1",
    "@ionic-native/geolocation": "^5.19.1",
    "@ionic-native/google-maps": "^5.5.0",
    "@ionic-native/http": "^5.19.1",
    "@ionic-native/location-accuracy": "^5.19.1",
    "@ionic-native/splash-screen": "^5.19.1",
    "@ionic-native/status-bar": "^5.19.1",
    "@ionic/angular": "^4.7.1",
    "@ionic/storage": "^2.2.0",
    "@mapbox/polyline": "^1.1.0",
    "@types/socket.io": "^2.1.4",
    "@types/socket.io-client": "^1.4.32",
    "angular-laravel-echo": "git+https://github.com/chancezeus/angular-laravel-echo.git",
    "call-number": "^1.0.1",
    "cordova-android": "^8.1.0",
    "cordova-plugin-advanced-http": "^2.3.1",
    "cordova-plugin-file": "^6.0.2",
    "cordova-plugin-geolocation": "^4.0.2",
    "cordova-plugin-googlemaps": "^2.6.2",
    "cordova-plugin-request-location-accuracy": "git+https://github.com/razvang10/cordova-plugin-request-location-accuracy.git",
    "cordova-sqlite-storage": "^4.0.0",
    "cordova.plugins.diagnostic": "^5.0.1",
    "core-js": "^2.5.4",
    "laravel-echo": "^1.6.1",
    "ngx-laravel-echo": "^1.0.26",
    "rxjs": "^6.5.4",
    "socket.io-client": "^2.3.0",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/architect": "~0.801.2",
    "@angular-devkit/build-angular": "^0.803.23",
    "@angular-devkit/core": "~8.1.2",
    "@angular-devkit/schematics": "~8.1.2",
    "@angular/cli": "8.1.3",
    "@angular/compiler": "~8.1.2",
    "@angular/compiler-cli": "~8.1.2",
    "@angular/language-service": "~8.1.2",
    "@ionic/angular-toolkit": "^2.1.1",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.2.1",
    "cordova-plugin-device": "^2.0.3",
    "cordova-plugin-ionic-keyboard": "^2.2.0",
    "cordova-plugin-ionic-webview": "^4.1.3",
    "cordova-plugin-splashscreen": "^5.0.3",
    "cordova-plugin-statusbar": "^2.4.3",
    "cordova-plugin-whitelist": "^1.3.4",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.5.1",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },

命令或代码

ionic cordova构建版android--发布--产品

环境、平台、设备

平台:安卓
器械:任何

版本信息

cordova :9.0.0( cordova -库@9. 0. 1)
离子:6.1.0
操作系统: windows
Android Studio :3.5
平台版本:API 29
软件开发工具包:26.1.1

hm2xizp9

hm2xizp91#

看起来在Android API级别28之后,你不能使用443以外的任何其他端口进行安全连接...因为它只会在Relase模式下限制你的访问,所以我把我的套接字端口从我原来的改为443,它工作正常。

c9qzyr3d

c9qzyr3d2#

我也遇到了同样的问题,无法在生产应用上运行(只能在模拟器的开发模式下运行)。无论是端口443还是其他端口。我使用的是自签名证书,但在服务器端和客户端都是rejectUnauthorized: false
服务器:

import { createSecureServer } from "http2";
import { Server } from "socket.io";
import fs from 'fs';

console.log("Start Server..");

var options = {
    key: fs.readFileSync('./zefau-key.pem', 'utf-8'),
    cert: fs.readFileSync('./zefau-crt.pem', 'utf-8'),
    'allowHTTP1': true,
    requestCert: false,
    rejectUnauthorized: false
}

const httpsServer = createSecureServer(options);
const io = new Server({
    // options
        'cors': {
            'origin': (origin, fn) => {
                return fn(null, origin);
            }
        }
});

// attach server to socket
io.attach(httpsServer);

// error
io.on('error', error => {
    console.error(error);
});

// connection
io.on("connection", (socket) => {
    console.log(socket.id);
    
    socket.on("message", (message) => {
        console.log('message received: ' + message);
        socket.emit('message', 'hi back');
    });
    
});

httpsServer.listen(443, '0.0.0.0');

客户Cordova应用程序(应用程序版本仅用于测试)

<template>
  <router-view />
</template>

<script>
import { defineComponent } from 'vue'
import { io } from "socket.io-client";

export default defineComponent({
  name: 'App',
  
    created() {
        alert("Start Client..");
        
        const URL = "192.168.178.50";
        const socket = io('https://' + URL + ':443', { rejectUnauthorized: false });
        
        socket.on("connect", () => {
            alert("CONNECTED socketio");
            socket.emit('message', 'hi');
        });
        
        socket.on("connect_error", error => {
            alert("CONNECT ERROR (" + URL + "): " + error.message);
        });

        socket.on("message", message => {
            alert('socket message received: ' + message);
        });
    }
})
</script>

我必须配置什么才能让它在Android上的生产应用程序上运行?

相关问题