Skip to content
 
 

Latest commit

 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot WebSocket And Android Client

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.

Introduction

中文

Server now include three endpoints to receive message from client:

  1. /broadcast

This endpoint will simply transfer all messages it received to subscriber of /b.

  1. /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

  1. /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!

Server

spring boot starter

Broadcast(Browser)

broadcast in browser

Broadcast(Android)

broadcast in android

Dynamic Groups(Browser)

group in browser

Dynamic Groups(Android)

group in android

Point-to-Point Chat(Browser)

chat in browser

Point-to-Point Chat(Android)

chat in browser

LICENSE

MIT

About

Spring Boot WebSocket Server with Android client and browser client.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages