socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.34k stars 977 forks source link

args[1] is received as null instead of socket acknowledgment callback #775

Open z-u-a opened 3 days ago

z-u-a commented 3 days ago

Problem:

Emitting event from server along with the callback and expecting client to trigger callback as the event received to acknowledge the server. Client is getting args[1] as null when it should be a callback.

Steps: setup "socket.io-client": "^2.0.0" in react just to test if callback is emitting from server, in this test code, callback received.

server:

 const sendWithRetry = async (msg, attempts = 0, retries, options = {}) => {

    let timer;
    attempts++;
    const waitDelay = parseInt(options.waitDelay) || parseInt(global.settingss.socket_ack_wait_time) || 5000;
    const retryCount = parseInt(retries) || parseInt(global.settingss.socket_ack_failure_retries) || 3;

    const socket = socketIO.sockets.connected[msg.socketId];
    if(socket) {
        const response = await new Promise((resolve) => {

            timer = setTimeout(() => resolve(null), waitDelay); // Timeout occurred

            socket.emit(msg.eventName, msg.response, function(ack) {
                clearTimeout(timer);
                resolve(ack); // Resolve with client acknowledgment
            });
        });

        if (response && response.success) {
            return response; 
        } else if (!response && attempts < retryCount) {
            console.log(`No acknowledgment, retrying...`, attempts);
            clearTimeout(timer);
            return sendWithRetry(msg, attempts, retryCount, options); // Recursively retry
        } else {
            console.log('Acknowledgment not received after retries. sending push')
            clearTimeout(timer);
            sendPush(msg);
        }
    }}

client

        @Override
        public void call(Object... args) {
            Log.d("hello","helloTesting::"+new Gson().toJson(args));
            String serverResponse = args[0].toString();
            Utils.redLog(ApiTags.BOOKING_CONVERSION_NOTIFICATION, serverResponse);
            if (StringUtils.isNotBlank(serverResponse)) {
                try {
                    BookingConversion response = new Gson().fromJson(serverResponse, BookingConversion.class);
                    mCallback.onBookingConversion(response);
                }catch (Exception ignored){
                }
            }
            if (args.length > 1 && args[1] instanceof io.socket.emitter.Emitter.Listener) {
                Log.d("hello","helloTesting::1::"+new Gson().toJson(args[1]));
                Emitter.Listener ack = (Emitter.Listener) args[1];
                //ack.call("Acknowledgment received!");
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("success",true);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                ack.call(jsonObject);
            }
        }

server socket.io version "socket.io": "^2.0.3", client version io.socket:socket.io-client:1.0.0

cannot upgrade versions as this is legacy code up and running on prod. Any help would be appreciated. Thanks