Results 1 to 10 of 11

Thread: Multi-room chat

Hybrid View

  1. #1
    Power Member
    Join Date
    Feb 2008
    Location
    Siracusa
    Posts
    161
    Hi drmistral,

    What is the purpose of the HashMap in the ChatDataAdapter?
    Is "<"CHAT", text, chat_room>" the message string sent through the sendMessage method?
    Is "CHAT" the item to which your client is subscribing to?
    Could you please provide more info about item and fields model?

    Thanks
    Gianluca

  2. #2
    Hi Gianluca, here is some details about my test to move from single room to multi-room chat. Starting point was the actual "Basic Chat Demo", client and server components downloaded from your demo page, changed parts are red.

    JS CLIENT

    First, in the client JS side I manage the name of the room through a global variable chat_room which can be set, in my case, with a reference to the post (room) id .

    [JS.1] In the Subscription management:
    Code:
    //  create the Subscription; field names will be extracted from the grid
    var chatSubscription = new Subscription("DISTINCT", chat_room, chatGrid.extractFieldList());
    [JS.2] In the submit form:
    Code:
    var mex = "CHAT|" + text + "|" + chat_room;
    ChatMetaDataAdapter

    The expected message contains a 3rd more part: the name of the room, so we address it.

    Code:
    private void handleChatMessage(String[] pieces, String message, String session) throws NotificationException {
            //extract session infos
    
            if (pieces.length != 3) {
                logger.warn("Wrong message received: " + message);
                throw new NotificationException("Wrong message received");
            }
    
            Map<String,String> sessionInfo = sessions.get(session);
            if (sessionInfo == null) {
                 logger.warn("Message received from non-existent session: " + message);
                 throw new NotificationException("Wrong message received");
            }
            //read from infos the IP and the user agent of the user
            String ua = sessionInfo.get("USER_AGENT");
            String ip =  sessionInfo.get("REMOTE_IP");
    
            //Check the message, it must be of the form "CHAT|message"
            if (pieces[0].equals("CHAT")) {
                //and send it to the feed
                if (!this.chatFeed.sendMessage(ip,ua,pieces[1], pieces[2])) {
                     logger.warn("Wrong message received: " + message);
                     throw new NotificationException("Wrong message received");
                }
            } else {
                 logger.warn("Wrong message received: " + message);
                 throw new NotificationException("Wrong message received");
            }
        }
    ChatDataAdapter.java

    Code:
    private volatile Object subscribed; 
    

    was replaced with the map
    Code:
    private volatile HashMap<String, Object> rooms = null;
    that is initialized in the init method with

    Code:
    rooms = new HashMap<String, Object>();
    The purpose of this map is to associate a chat name key to a Item handler, instead of the singleton handler (Object subscribed).

    In the subscribe method the item name is associated with the passed Handler:

    Code:
        public void subscribe(String item, Object handle, boolean arg2) throws SubscriptionException, FailureException {
    
    
    //        if (!item.equals(ITEM_NAME)) {
    //            // only one item for a unique chat room is managed
    //            throw new SubscriptionException("No such item");
    //        }
    //        assert(subscribed == null);
    //        subscribed = handle;
            
            rooms.put(item, handle);
    ...
    In the sendMessage method we get the right Item from the map.

    Code:
        public boolean sendMessage(String IP, String nick, String message, String room) {
            final Object currSubscribed = rooms.get(room);
            if (currSubscribed == null) {
                return false;
            }
        ...
    Finally in the adapter_log_conf.xml i commented out the following block

    Code:
            <!-- Optional, managed by the inherited LiteralBasedProvider. See LiteralBasedProvider javadoc. 
            <param name="item_family_1">chat_room</param>
            <param name="modes_for_item_family_1">DISTINCT</param>
            -->

    That's it. It's just a quick&dirty test, and it's also possible that I've misunderstood the usage philosophy of classes.
    Does it make sense?

    Thanks
    Last edited by drmistral; October 26th, 2016 at 06:55 PM. Reason: code formatting

  3. #3
    Power Member
    Join Date
    Feb 2008
    Location
    Siracusa
    Posts
    161
    Hi drmistral,

    thank you very much for your great effort in showing me complete details of your work. You did a very good job, in compliance of the usage philosophy.

    My only concern is related to the use of an HashMap, you should use instead a ConcurrentHashMap as concurrent threads my access such object.

    Let me know if you need any further clarification.

    Thanks and Regards,
    Gianluca

  4. #4
    Hi Gianluca, thanks a lot for your check, it was very important for me to know if I was using this stuff in the right way.
    Yes, of course ConcurrentHashMap has to be used. That was just a quick test, now I'm going to integrate LS in my project, let's see...

    Thanks for your time.

    Cheers
    Ferdinando

 

 

Similar Threads

  1. Chat Demo in Flash???
    By phamminh05 in forum Client SDKs
    Replies: 7
    Last Post: August 13th, 2019, 05:25 PM
  2. Replies: 1
    Last Post: April 30th, 2015, 08:51 AM
  3. chat and private message
    By ernivan in forum Client SDKs
    Replies: 1
    Last Post: March 11th, 2011, 09:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +1. The time now is 08:55 AM.