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 975 forks source link

The listener cannot be removed by calling the `off('EVENT')` method #696

Closed ghost closed 2 years ago

ghost commented 2 years ago

Describe the bug

The listener cannot be removed by calling the off('EVENT') method

To Reproduce

Socket.IO server version: 4.4.0

Server

server = require('http').createServer();
io = require("socket.io")();
io.attach(server);

server.listen(port, 'localhost');
server.on("listening", function () {
    startJavaBackend(port);
});

// prototype
app['mainWindowURL'] = "";
app['mainWindow'] = null;

// @ts-ignore
io.on('connection', (socket) => {

});

Socket.IO java client version: x.y.z

Client

// BridgeConnector.java
if (socket == null && HybridSupport.IsElectronActive()) {
    synchronized (BridgeConnector.class) {
        if (socket == null && HybridSupport.IsElectronActive()) {
            IO.Options options = IO.Options.builder().build();
            socket = IO.socket(URI.create("http://localhost:" + BridgeSettings.getSocketPort()), options);
            socket.connect();
        }
    }
}

// IpcMain.java
public void On(String channel, Action1<Object> listener) {
    BridgeConnector.getSocket().emit("registerIpcMainChannel", channel);
    BridgeConnector.getSocket().off(channel);
    BridgeConnector.getSocket().on(channel, (args) -> {
        var argList = Electron.fromJsonString(args[0].toString(), ArrayList.class, Object.class);
        if (argList.size() == 1) {
            listener.accept(argList.get(0));
        } else {
            listener.accept(argList);
        }
    });
}
Call the code below multiple times

Electron.getIpcMain().On("put-in-tray", (args) ->
{
    System.out.print("test")
});

Expected behavior

Print "test" once when I send "put in tray"

darrachequesne commented 2 years ago

Hi! I wasn't able to reproduce with this basic example:

public class MyApplication {
    public static void main(String[] args) throws URISyntaxException {
        IO.Options options = IO.Options.builder()
                .build();

        Socket socket = IO.socket("http://localhost:8080", options);

        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println("connect");
            }
        });

        socket.on("test", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                System.out.println("got test"); // printed just once
                socket.off("test");
            }
        });

        socket.open();
    }
}
darrachequesne commented 2 years ago

Closed due to inactivity, please reopen if needed.