Class RoomProtocolCodec

java.lang.Object
org.atmosphere.room.protocol.RoomProtocolCodec

public final class RoomProtocolCodec extends Object
JSON encoder/decoder for the room protocol, using org.json (already a dependency of atmosphere-runtime via SimpleRestInterceptor).

Wire format (client → server)


 { "type": "join",      "room": "lobby", "memberId": "alice", "metadata": { "avatar": "..." } }
 { "type": "leave",     "room": "lobby" }
 { "type": "broadcast", "room": "lobby", "data": "hello" }
 { "type": "direct",    "room": "lobby", "targetId": "bob", "data": "hi bob" }
 

Wire format (server → client)


 { "type": "join_ack",  "room": "lobby", "members": [...] }
 { "type": "presence",  "room": "lobby", "action": "join|leave", "memberId": "alice", "metadata": {...} }
 { "type": "message",   "room": "lobby", "from": "alice", "data": "hello" }
 { "type": "error",     "room": "lobby", "data": "Unauthorized" }
 
Since:
4.0
  • Method Details

    • decode

      public static RoomProtocolMessage decode(String json)
      Decode a JSON string into a protocol message.
      Parameters:
      json - the raw JSON string
      Returns:
      the decoded message
      Throws:
      IllegalArgumentException - if the JSON is malformed or has an unknown type
    • encodeJoinAck

      public static String encodeJoinAck(String room, Collection<RoomMember> members)
      Encode a join acknowledgement with the current member list.
    • encodePresence

      public static String encodePresence(String room, String action, RoomMember member)
      Encode a presence event (join or leave) for broadcast to other members.
    • encodeMessage

      public static String encodeMessage(String room, String fromMemberId, Object data)
      Encode a room message for delivery to clients.
    • encodeError

      public static String encodeError(String room, String message)
      Encode an error response.
    • encodeTyping

      public static String encodeTyping(String room, String memberId, boolean typing)
      Encode a typing indicator for broadcast to other room members.