我刚刚使用 feathersjs 在 NodeJs 中完成了我的应用程序。现在我在 android 上编写了一个应用程序,通过 Node 端与我的服务进行通信。如何通过 android 套接字与查找/获取/创建/更新等 Node 服务进行通信?
下面是我的代码 fragment :
try {
mSocket = IO.socket("http://10.0.130.32:3030");
} catch (URISyntaxException e) {
e.printStackTrace();
}
mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
// socket.emit("foo", "hi");
// socket.disconnect();
}
}).on("config", new Emitter.Listener() {
/*
* Android joins to a room after receiving socket's id from another user
*/
@Override
public void call(Object... args) {
try {
JSONObject object = new JSONObject(args[0].toString());
mSocket.emit("join", object.get("socket"));
id = object.getString("socket");
} catch (JSONException e) {
e.printStackTrace();
}
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {}
});
mSocket.connect();
我知道你可以使用:mSocket.emit("join", "some data");但我想调用 REST Api 例如:http://10.0.130.32:3030/getID在 Android 中通过带参数的套接字。
我怎样才能做到这一点?
最佳答案
使用 Feathers,您可以从 REST 或套接字访问所有服务方法(查找/获取/创建等)。
来自文档:https://docs.feathersjs.com/clients/vanilla-socket-io.html
Calling service methods
Service methods can be called by emitting a
<servicepath>::<methodname>
event with the method parameters. servicepath is the name the service has been registered with (in app.use) without leading or trailing slashes. An optional callback following the function(error, data) Node convention will be called with the result of the method call or any errors that might have occurred.params will be set as params.query in the service method call. Other service parameters can be set through a Socket.io middleware.
您可以在该页面上找到很多示例,但作为一个想法:
socket.emit('messages::find', { status: 'read', user: 10 }, (error, data) => {
console.log('Found all messages', data);
});
请注意,您需要 socket
指向你的羽毛应用程序的根:10.0.130.32:3030
关于javascript - 在Android中如何通过socket.io发送带有参数的POST/GET或通过socket.io调用feathersjs服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813349/