Java Reference
In-Depth Information
String id = msg.split("-")[1];
session.getUserProperties().put("id", id);
//save the ID of the user
usersId.add(id);
//add the ID in the list of connected
users
Object[] obj1 = new
Object[]{"wel","Welcome to the chat room "+id
+"!"};
String jsonString = getJsonObject(obj1);
//json message transformation
//send a welcome message to the new user
session.getBasicRemote().sendText(jsonString);
//send the list of connected users to all
users
Object[] obj2 = new
Object[]{"users",usersId};
jsonString = getJsonObject(obj2);
for (Session s : users) {
s.getBasicRemote().sendText(jsonString);
}
} else { //if it is a message to the chat
room
//get ID of the user who sends message
String id = (String)
session.getUserProperties().get("id");
Object[] obj = new Object[]{"msg",id +
">>" + msg.split("-")[1]};
String jsonString =
getJsonObject(obj);//json transformation
//sends the message to all connected users
for (Session s : users) {
s.getBasicRemote().sendText(jsonString);
}
}
}
Search WWH ::




Custom Search