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:
// create the Subscription; field names will be extracted from the grid
var chatSubscription = new Subscription("DISTINCT", [COLOR=#ff0000][B]chat_room[/B][/COLOR], chatGrid.extractFieldList());
[JS.2] In the submit form:
var mex = "CHAT|" + text [B][COLOR=#ff0000]+ "|" + chat_room[/COLOR] [/B];
ChatMetaDataAdapter
The expected message contains a 3rd more part: the name of the room, so we address it.
private void handleChatMessage(String[] pieces, String message, String session) throws NotificationException {
//extract session infos
if (pieces.length != [COLOR=#ff0000][B]3[/B][/COLOR]) {
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][B][COLOR=#ff0000], [/COLOR][COLOR=#ff0000]pieces[2][/COLOR][/B])) {
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
[COLOR=#931A68]private [/COLOR][COLOR=#931A68]volatile[/COLOR] Object subscribed;
was replaced with the map
[COLOR=#931A68]private [/COLOR][COLOR=#931A68]volatile[/COLOR]HashMap<String, Object> rooms = [COLOR=#931A68]null[/COLOR];
that is initialized in the
init method with
rooms = [COLOR=#931A68]new[/COLOR]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:
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;
[B][COLOR=#ff0000]rooms.put(item, handle);[/COLOR][/B]
...
In the
sendMessage method we get the right Item from the map.
public boolean sendMessage(String IP, String nick, String message, String room) {
final Object currSubscribed = [B][COLOR=#ff0000]rooms.get(room)[/COLOR][/B];
if (currSubscribed == null) {
return false;
}
...
Finally in the adapter_log_conf.xml i commented out the following block
<!-- 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