android - 无法从真正的 Android 设备访问 Node.js 的 IP 地址
问题描述
我使用以下代码设置了一个非常基本的 node.js 服务器:
const express = require("express");
const app = express();
const port = 8000;
const ip = '192.16x.xxx.yyy'; // the x's and y's are placeholders. I didnt want to write my real IP here
const http = require("http").createServer();
const io = require("socket.io")(http);
// using the socket, we can send/receive data to/from the client
// the server listens for the "connection" event
io.on("connection", (socket) => {
// emit an event called "welcome" with a string as data to the client using its socket
// so, whenever the client connects to the server, it receives this welcome message
socket.emit("welcome", "Hello and Welcome to the Socket.io server.");
console.log("a new client is connected.")
});
// the server listens on the specified port for incoming events
http.listen(port, ip, () => {
console.log("Server is listening on " + ip + ":" + port);
});
因此,服务器正在侦听“连接”事件,并在客户端连接到服务器时发送欢迎消息。
作为 Android 应用程序的客户端如下所示:
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import com.github.nkzawa.emitter.Emitter;
import java.net.URISyntaxException;
public class MainActivity extends AppCompatActivity {
private Socket mSocket;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.textView);
try {
/*
* IO.socket() returns a socket for the specified URL and port with the
* default options. Notice that the method caches the result, so you can
* always get a same Socket instance for an url from any Activity or
* Fragment.
* */
mSocket = IO.socket("http://192.16x.xxx.yyy:8000");
} catch (URISyntaxException e) {
e.getMessage();
}
/*
* We then can make the socket listen an event on onCreate lifecycle callback.
* */
mSocket.on("welcome", onNewMessage);
/*
* And we explicitly call "connect()" to establish the connection here.
* In this app, we use onCreate lifecycle callback for that, but it
* actually depends on your application.
* */
mSocket.connect();
}
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
String message = (String) args[0];
mTextView.setText("Received from Server: " + message);
}
});
}
};
/*
* Since an Android Activity has its own lifecycle, we should carefully manage the
* state of the socket also to avoid problems like memory leaks. In this app, we’ll
* close the socket connection and remove all listeners on onDestroy callback of
* Activity.
* */
@Override
protected void onDestroy() {
// socket gets disconnected
mSocket.disconnect();
// off() removes the listener of the "welcome" event
mSocket.off("welcome", onNewMessage);
super.onDestroy();
}
}
客户端代码也很简单:它连接到服务器并显示它从服务器获取的消息。我得到了这个前任。来自官方 Socket.IO 网站https://socket.io/blog/native-socket-io-and-android/。
当我node server.js
在笔记本电脑的终端上启动服务器并使用 Android 模拟器时,一切正常。
但是,当我使用 USB 将我真正的 Android 设备连接到我的笔记本电脑时,连接不会发生。
为什么 Android 应用程序(在真实设备中运行)无法连接到服务器?
解决方案
这对我有用:我创建了一个 IP 为 0.0.0.0 的套接字,您应该将 node.js 上的 IP 更改为此值。
比我打开 cmd 并输入 ipconfig 来查看我的 IP 地址是什么,这是你的本地 IP 地址。在代码中,我连接到这个 IP 地址。
例如,如果您在 ipconfig 中看到 IP 192.168.178.50,则将您的 Android 代码更改为
mSocket = IO.socket("http://192.168.178.50:8000");
你的 Node.js 代码应该看起来像
const ip = '0.0.0.0';
更改 node.js 代码时不要忘记重新启动服务器
推荐阅读
- apache-spark - Spark 流无法使用来自 Kafka 特定偏移量的数据
- reactjs - 如何在 ReactJS 中选择时使项目唯一
- regex - 用外部参照替换链接:lalala.html[lalala] 的 Powershell 脚本:lalala.adoc[lalala] 捕获模式并递归替换
- azure - 在 Azure Kubernetes 服务上部署时出现 Filebeat Demonset 错误
- c++ - 我想在 windows 环境中使用 cpprest 进行 https 传输
- angular - 使用 FormGroup、FormBuilder 和 Validators 时更改 angular11 表单验证状态
- slider - 如何在android中使用范围滑块
- angular - 更新数据库中的前端字段
- javascript - Android webview 不调用 API 级别 <24 的 javascipts 函数
- r - R - 带有自由刻度的双 y 轴 - GGPLOT