简体   繁体   English

在Socket.io Android客户端v1.4中处理回调

[英]Handle Callbacks in Socket.io android client v1.4

I am not able to find any doc on how to properly handle Ack's and Events in the latest Socket.io ( v1.4.3 ). 我无法在最新的Socket.iov1.4.3 )中找到有关如何正确处理Ack和事件的任何文档。 All existing articles/question refer to older versions, especially the IOCallback class. 现有的所有文章/问题都引用了较旧的版本,尤其是IOCallback类。 But that class is not present on the latest version. 但是该类在最新版本中不存在。

All I managed to find out till now is this : 我直到现在才发现的是:

To get Callbacks for Socket Events : 要获取Socket事件的回调

mSocket.connect();

mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        //What to do here
    }
})
  1. How do I handle the (Object... args) . 我该如何处理(Object... args) A little code example would be great. 一个小的代码示例将是很棒的。
  2. There seems to be more than a dozen events, do I have to handle all that separately? 似乎有十几个事件,我是否必须分别处理所有这些事件? Or what is a good minimum set of events that I can implement to be informed about the connection? 或者,我可以实施哪些最小的事件集来告知有关连接的信息?

To get callbacks for individual emit events : 要获取各个emit事件的回调

mSocket.emit("payload", jsObj.toString(), new Ack() {
    @Override
    public void call(Object... args) {
        //TODO process ACK
    }
});
  1. Again, how am I supposed to process the (Object... args) ? 同样,我应该如何处理(Object... args)

Well. 好。 I finally figured that out on my own. 我终于自己弄明白了。

How do I handle the (Object... args) on the EVENT_CONNECT listener's call method? 如何处理EVENT_CONNECT侦听器的call方法上的(Object... args)

I haven't yet figured that out. 我还没有想通。 But I'm lookin. 但是我在找。

What is a good minimum set of events that I can implement to be informed about the connection 我可以实施哪些最小的事件集以告知有关连接的信息

These three methods would be sufficient : 这三种方法就足够了:

connect : Fired upon a successful connection. connect :成功连接后触发。
connect_error : Fired upon a connection error. connect_error :连接错误时触发。
connect_timeout : Fired upon a connection timeout. connect_timeout :在连接超时时触发。

Source : Socket.io Docs 资料来源: Socket.io文件

how am I supposed to process the (Object... args) on an emit acknowledgement? 我应该如何在发出确认时处理(Object... args)

So I was digging through the docs and found this : 所以,我是通过挖文档,发现这个

Server (app.js) 服务器(app.js)

 var io = require('socket.io')(80); io.on('connection', function (socket) { socket.on('ferret', function (name, fn) { fn('woot'); }); }); 

Client 客户

 socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too! socket.emit('ferret', 'tobi', function (data) { console.log(data); // data will be 'woot' }); }); 

So the args will be whatever the server sent as parameter into the callback. 因此,args将是服务器作为参数发送到回调中的任何参数。 So this is how you would write Java client code for the above server code : 因此,这就是为上述服务器代码编写Java客户端代码的方式:

public void call(Object... args) {
  String response = (String)args[0]; //this will be woot
}

The param can also be JSON, or any of the supported datatypes in socket.io : 参数也可以是JSON或socket.io中任何受支持的数据类型

we send a string but you can do JSON data too with the org.json package, and even binary data is supported as well! 我们发送一个字符串,但是您也可以使用org.json包进行JSON数据处理,甚至还支持二进制数据!

No In Android its works like this 否在Android中,其工作方式如下

payload can be of JSONOBJECT/JSONArray 有效负载可以是JSONOBJECT / JSONArray

import com.github.nkzawa.socketio.client.Ack

socket.emit("EVENT_NAME", payload, Ack {
                    val ackData = it[0]
                    Logger.e(TAG, "ackData $ackData")

                })

server side 服务器端

  socket.on('EVENT_NAME', (payload, callback) => {
   callback("success");
});

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

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