简体   繁体   English

如何通过socket.io发送带有参数的POST/GET或在Android中通过socket.io调用feathersjs服务?

[英]How can to send POST/GET with params via socket.io or call a feathersjs service via socket.io in Android?

I just finished my application in NodeJs using feathersjs .我刚刚使用完在我的NodeJS应用feathersjs Now I write an app on android to communicate with my services by node side.现在我在 android 上编写了一个应用程序,通过节点端与我的服务进行通信。 How can I communicate with node service like find/get/create/update via android socket?如何通过 android socket 与节点服务(如 find/get/create/update)通信?

Below is my snippet code:下面是我的代码片段:

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();

I know that you can use: mSocket.emit("join", "some data");我知道你可以使用: mSocket.emit("join", "some data"); but I would like call a REST Api for example: http://10.0.130.32:3030/getID in Android via socket with params.但我想通过带有参数的套接字调用 REST Api 例如:Android 中的http://10.0.130.32:3030/getID

How can I do that?我怎样才能做到这一点?

With Feathers, you are able to access all service methods (find/get/create/etc) from either REST or Sockets.使用 Feathers,您可以从 REST 或 Sockets 访问所有服务方法(find/get/create/etc)。

From the docs: https://docs.feathersjs.com/clients/vanilla-socket-io.html来自文档: 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>::<methodname>事件来调用服务方法。 servicepath is the name the service has been registered with (in app.use) without leading or trailing slashes. servicepath 是服务注册的名称(在 app.use 中),不带前导或尾随斜杠。 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.将使用方法调用的结果或可能发生的任何错误调用遵循 function(error, data) 节点约定的可选回调。

params will be set as params.query in the service method call. params 将在服务方法调用中设置为 params.query。 Other service parameters can be set through a Socket.io middleware.其他服务参数可以通过 Socket.io 中间件设置。

You can find a lot of examples on that page, but as an idea:您可以在该页面上找到很多示例,但作为一个想法:

socket.emit('messages::find', { status: 'read', user: 10 }, (error, data) => {
  console.log('Found all messages', data);
});

Note that you'll need the socket pointed to the root of your feathers app: 10.0.130.32:3030请注意,您需要将socket指向10.0.130.32:3030应用程序的根目录: 10.0.130.32:3030 : 10.0.130.32:3030

In Android for REST Api calls we use HttpURLConnection.在 Android for REST Api 调用中,我们使用 HttpURLConnection。 For example for GET:例如对于 GET:

String url = "http://10.0.130.32:3030/getID";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM