【发布时间】:2019-12-09 00:56:46
【问题描述】:
我使用以下代码设置了一个非常基本的 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 地址,您的笔记本电脑和您的设备是否在同一个网络中?
-
是的,他们是。我在家里。两台设备(笔记本电脑和安卓设备)都连接到同一个 WiFi。
-
当您使用 locahost 时,您可以将 node.js IP 更改为 127.0.0.1
-
经过测试。不工作。
-
抱歉是我的错,IP 0.0.0.0 应该是你的 node.js IP。您需要从设备连接到本地主机,而不是在路由器中看到笔记本电脑的 IP
标签: android node.js connection ip