但是,当我从商店运行发布版本时,应用程序不会连接到套接字。
我正在使用 LaravelEcho 和 SocketIo 客户端。
搜索问题后,我发现了一些有关权限的结果,但我已经有了。
<uses-permission android:name="android.permission.INTERNET" />
发布模式下可以调试应用吗?
这就是我导入 NgxLaravelEchoModule 的方式
NgxLaravelEchoModule.forRoot({
userModel: 'App.Models.User',
notificationNamespace: 'App\\Notifications',
options: {
broadcaster: 'socket.io',
host: environment.socketsURL
}
}),
我设法指出套接字模块抛出的错误消息
xhr poll error
我花了几个小时寻找这个问题的答案(我知道这篇文章很旧,但也许它可以帮助某人)。没有人能够回答这个问题。不过,我相信真正的答案是,默认情况下,Android 不喜欢 http。相反,解决方案是在您的 socket.io 客户端上使用 https。但设置起来很烦人。
更好的解决方案是在 Android 应用程序上允许 http。在 AndroidManifest.xml 中,找到“application”。默认情况下它应该看起来像这样:
<application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true">
将
android:usesCleartextTraffic="true"
附加到此以获得以下内容:
<application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:usesCleartextTraffic="true">
编译您的 apk 并在 Android 设备上运行它。瞧!您现在可以连接到发布版本上的套接字。
服务器:确保您已设置原点:
const express = require("express");
const app = express();
const http = require("http").Server(app);
const cors = require("cors");
const io = require("socket.io")(http, {
cors: {
origin: "*",
},
});
app.use(cors());