jjenkov/java-nio-server

can this server push message to client ?

PudgeMa opened this issue · 3 comments

I want to make a Tcp Server that can push message to client, and I'm worry about how to save and mark every SocketChannel of client.
does your code have this function ? thanks.

@jjenkov yeah. I have the same idea. I think it may be difficult to remove socket from the map when the socket disconnects with server, so the heartbeat may be need to detect that.

Actually, this server already assigns an ID to the sockets and puts them into a Map. It also removes them again when the sockets are closed. Look at this code from the SocketProcessor:

    if(socket.endOfStreamReached){
        System.out.println("Socket closed: " + socket.socketId);
        this.socketMap.remove(socket.socketId);
        key.attach(null);
        key.cancel();
        key.channel().close();
    }