Spring Boot WebSocket Server, with a browser client and a simple Android client. Android client uses StompProtocolAndroid which implements ptotocol STOMP on Android, to subscribe or send message to server.
Server now include three endpoints to receive message from client:
/broadcast
This endpoint will simply transfer all messages it received to subscriber of /b.
/group/{groupID}
This endpoint is used to dynamicly create groups. For example, client can send a message to /group/1, and all subscriber of /g/1 will receive the message. Also, you can change the subscribe endpoint by changing the Controller and WebSocketConfig
/chat
Endpoint /chat is used to enable point-to-point communication. If Alice(with userID 1) want to chat with Bob(with userID 2), she should send a message to endpoint /chat with a special json object defined as ChatMessage:
// js code
function sendMessage() {
var message = $('#message').val();
stompClient.send('/chat', {}, JSON.stringify({
'userID': 2,
'fromUserID': 1,
'message': "Hello Bob"})
);
}userID is necessary, this field will be used in server code to transfer message:
simpMessagingTemplate.convertAndSendToUser(String.valueOf(chatMessage.getUserID()), "/msg", response);Through code above, the message from Alice will be sent to /user/2/msg, and if Bob subscribe it(also means subscribe himself), he will receive the message.
And Alice should also subscribe herself to receive message sent to her:
stompClient.subscribe('/user/' + 1 + '/msg', function (response) {
showResponse(JSON.parse(response.body).responseMessage);
});So when Bob send a message to Alice, she will receive it correctly.
Android client and Browser support all endpoints now!
MIT




