【发布时间】:2021-01-13 04:04:30
【问题描述】:
您好,我尝试将 android 我的 socket.io 连接到我使用 ngnix 托管的 nodejs 服务器
这是我在我的安卓应用中使用的安卓代码
private var mSocket: Socket? = null
val opts = IO.Options()
opts.forceNew = true
opts.reconnection = true
val aara = arrayOf("websocket")
opts.transports = aara
try {
mSocket = IO.socket(socket_url, opts)
mSocket?.connect()
mSocket?.emit("connection", "Nickname");
mSocket?.emit("user_connect", "Nickname");
Log.e("mSocket: ", mSocket?.connected().toString())
mSocket?.on("connection") {
Log.e("onCreate4w: ", "${it::class.simpleName} ${it.size}")
Log.e("mSocket: ", mSocket?.connected().toString())
}
} catch (e: URISyntaxException) {
Log.e("onCreate25: ", mSocket?.connected().toString())
}
服务器代码是用 Nodejs 编写的
const io = soketio(server, {
cors: {
origin: [
process.env.TYPEFORM_ORIGIN,
process.env.ADMINPANEL_ORIGIN,
"http://localhost:4200",
"*"
,
],
methods: ["GET", "POST"],
allowedHeaders: ["my-custom-header"],
credentials: true,
},
multiplex: false,
});
io.on("connection", (soket) => {
soket.on("user_connect", (userId) => {
// console.log("USER1", user);
user[userId] = soket.id;
// console.log("USER2", user);
console.log("User Connected", soket.id);
});
这是在服务器中启用的 ngnix 配置
location \ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:5555;
}
请指导我们哪里做错了 此设置适用于 web 面板,而不是在 android 中
【问题讨论】:
标签: android nginx websocket socket.io