Tuesday, 14 March 2017

Socket.io listen to room without join

I'd like to broadcast a message to everyone that listen to this room, not only who join the room.

I do receive another call from server but I didn't get any call for this 'room' listener.

Server: Node.js

io.on('connection', function(socket){

    console.log('connected');

    socket.on('register', function(match) {
        console.log(match);
        var resultJSON = JSON.parse(match);
        socket.join(resultJSON['roomId'], function() {
            socket.broadcast.emit(match['roomId'], match);
        });
    });

});

Client: Java

mSocket.on(roomSelected.getId()), onSelectedRoom);
private Emitter.Listener onSelectedRoom = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {

                    try {
                        JsonObject obj = new JsonParser().parse(args[0].toString()).getAsJsonObject();
                        System.err.println(obj);
                        Match match = new Gson().fromJson(obj, Match.class);
                        updateRoomWith(match);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            });
        }
    };



via Marckaraujo

No comments:

Post a Comment