Answer the question
In order to leave comments, you need to log in
How to transfer the model from the server to the client if they are different for the client and server?
Good day to all!
I am writing a messenger as a thesis in Java EE.
The server has a ChatRoom model
public class ChatRoom {
private long id;
private String name;
private Map<String, Session> clients;
private List<User> members;
MessageEncoder encoder;
OnlineUsersStorage onlineUsersStorage = OnlineUsersStorage.getInstance();
public ChatRoom(long id, String name, List<User> members) {
clients = new HashMap<>();
encoder = new MessageEncoder();
this.id = id;
this.name = name;
this.members = members;
}
//Setters
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setMembers(List<User> members) {
this.members = members;
}
public void setClients(Map<String, Session> clients) {
this.clients = clients;
}
//Getters
public long getId() {
return id;
}
public String getName() {
return name;
}
public List<User> getMembers() {
return members;
}
public Map<String, Session> getClients() {
return clients;
}
//Methods
public void sendMessage(Message message){
try {
String encodedMessage = encoder.encode(message);
List<Session> sessions = onlineUsersStorage.getUserSession(members);
for (Session s : sessions) s.getBasicRemote().sendText(encodedMessage);
}catch (IOException | EncodeException e){
e.printStackTrace();
}
}
}
public class ChatRoom {
private long id;
private String name;
private List<User> members;
private List<Message> messages;
public ChatRoom(long id, String name, List<User> members, List<Message> messages) {
this.id = id;
this.name = name;
this.members = members;
this.messages = messages;
}
//Getters
public long getId() {
return id;
}
public String getName() {
return name;
}
public List<User> getMembers() {
return members;
}
public List<Message> getMessages() {
return messages;
}
//Setters
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setMembers(List<User> members) {
this.members = members;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
//Methods
public Message getLastMessage(){
return messages.isEmpty() ? null : messages.get(messages.size() - 1);
}
}
Map<String, Session> clients;
MessageEncoder encoder;
OnlineUsersStorage onlineUsersStorage
List<Message> messages;
Answer the question
In order to leave comments, you need to log in
the internal objects of the server and the client are not connected at all, even if the fields completely match the
special DTO objects (data transfer) on the client, and this is not the same as the entities from the client or server models
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question