Commit 04a1c6a2 authored by alexandre.ducarne's avatar alexandre.ducarne
Browse files

Add user & room specific websocket support

parent e6fad37c
......@@ -18,6 +18,7 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter {
public static final String SEND_MESSAGE_TOPIC = "/topic/serverMessages";
private static final Logger LOGGER = LogManager.getLogger(WebSocketStompSessionHandler.class);
private StompSession stompSession;
private String currentUser;
/**
* Send a message to the server
......@@ -25,7 +26,7 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter {
* @param message the message to be sent
*/
public void sendMessage(MessageMeta message) {
stompSession.send(SEND_MESSAGE_TOPIC, message);
stompSession.send("/topic/user/" + currentUser, message);
LOGGER.info("Message sent to server : " + message.toString());
}
......@@ -82,4 +83,17 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter {
MessageClientController.getInstance().processData(message);
}
public void subscribeToRoom(String roomId) {
final String destination = "/topic/" + roomId + "/room";
stompSession.subscribe(destination, this);
LOGGER.info("Subscribed to " + destination);
}
public void subscribeUser(String userId) {
final String destination = "/topic/" + userId + "/user";
stompSession.subscribe(destination, this);
LOGGER.info("Subscribed to " + destination);
this.currentUser = userId;
}
}
......@@ -15,6 +15,8 @@ import java.util.Optional;
import java.util.UUID;
import main.java.server.network.messages.serverToClient.NewGameNotificationToClientMessage;
import java.util.Optional;
/**
* Class handling sending & receiving messages from clients
*/
......@@ -36,25 +38,25 @@ public class MessageServerController {
return Optional.of(instance).orElseGet(MessageServerController::new);
}
/**
* Get the communication core of the server
*
* @return the communication core
*/
public ComCoreServer getComCore() {
return comCore;
}
/**
* Set the new communication core of the controller
*
* @param comCore the communication core to set
*/
public void setComCore(ComCoreServer comCore) {
this.comCore = comCore;
}
/**
/**
* Get the communication core of the server
*
* @return the communication core
*/
public ComCoreServer getComCore() {
return comCore;
}
/**
* Set the new communication core of the controller
*
* @param comCore the communication core to set
*/
public void setComCore(ComCoreServer comCore) {
this.comCore = comCore;
}
/**
* Set the new WebSocket controller to use for messaging
*
* @param wsController the WebSocket controller to use
......@@ -69,7 +71,7 @@ public class MessageServerController {
* @param message the message to send
*/
public void sendListsToNewUser(ServerStateOnConnectionMessage message) {
wsController.sendMessage(message);
wsController.sendToAll(message);
}
/**
......@@ -78,9 +80,9 @@ public class MessageServerController {
* @param message the message to send
*/
public void notifyNewUserAdded(NewUserAddedMessage message) {
wsController.sendMessage(message);
wsController.sendToAll(message);
}
public void processData(MessageMeta message) {
switch(message.getClass().toString()) {
case "LoginUserToServerMessage":
......
......@@ -5,6 +5,7 @@ import main.java.common.message.MessageMeta;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
......@@ -16,7 +17,10 @@ import org.springframework.stereotype.Controller;
public class WebSocketController {
private static final Logger LOGGER = LogManager.getLogger(WebSocketController.class);
private final String MESSAGE_TOPIC = "/topic/clientMessages";
private final String TOPIC_PREFIX = "/topic/";
private final String DESTINATION_USER_SUFFIX = "/user";
private final String DESTINATION_ROOM_SUFFIX = "/room";
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
......@@ -25,20 +29,24 @@ public class WebSocketController {
*
* @param message the message to be sent to the client
*/
public void sendMessage(MessageMeta message) {
LOGGER.info("Message sent to client : " + message.toString());
simpMessagingTemplate.convertAndSend(MESSAGE_TOPIC, message);
public void sendToAll(MessageMeta message) {
LOGGER.info("Message sent to whole server : " + message.toString());
simpMessagingTemplate.convertAndSend(TOPIC_PREFIX + "clientMessages", message);
}
/**
* Process data according to the message received
* This method is triggered when the server receives a message from the client app
*
* @param message the message sent by the client
*/
@MessageMapping("/serverMessages")
public void onMessageReceived(MessageMeta message) {
LOGGER.info("New client message received : " + message.toString());
MessageServerController.getInstance().processData(message);
private void sendToUser(MessageMeta message, String userId) {
LOGGER.info("Message sent to user with id : " + userId);
simpMessagingTemplate.convertAndSend(TOPIC_PREFIX + userId + DESTINATION_USER_SUFFIX, message);
}
private void sendToRoom(MessageMeta message, String roomId) {
LOGGER.info("Message sent to whole room with id : " + roomId);
simpMessagingTemplate.convertAndSend(TOPIC_PREFIX + roomId + DESTINATION_ROOM_SUFFIX, message);
}
@MessageMapping("/user/{id}")
public void onMessageUser(@DestinationVariable String id, MessageMeta message) {
LOGGER.info("New client message received : " + message.toString() + " from user with id : " + id);
//TODO: process user request
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment