Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Alexandre Ducarne
ai12-othello
Commits
04a1c6a2
Commit
04a1c6a2
authored
Nov 26, 2019
by
alexandre.ducarne
Browse files
Add user & room specific websocket support
parent
e6fad37c
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/client/network/WebSocketStompSessionHandler.java
View file @
04a1c6a2
...
...
@@ -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
;
}
}
src/main/java/server/network/MessageServerController.java
View file @
04a1c6a2
...
...
@@ -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
.
send
Message
(
message
);
wsController
.
send
ToAll
(
message
);
}
/**
...
...
@@ -78,9 +80,9 @@ public class MessageServerController {
* @param message the message to send
*/
public
void
notifyNewUserAdded
(
NewUserAddedMessage
message
)
{
wsController
.
send
Message
(
message
);
wsController
.
send
ToAll
(
message
);
}
public
void
processData
(
MessageMeta
message
)
{
switch
(
message
.
getClass
().
toString
())
{
case
"LoginUserToServerMessage"
:
...
...
src/main/java/server/network/WebSocketController.java
View file @
04a1c6a2
...
...
@@ -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
send
Message
(
MessageMeta
message
)
{
LOGGER
.
info
(
"Message sent to
client
: "
+
message
.
toString
());
simpMessagingTemplate
.
convertAndSend
(
MESSAGE_TOPIC
,
message
);
public
void
send
ToAll
(
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
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment