|
| 1 | +# STOMP protocol via WebSocket for Android |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This library provide support for STOMP protocol https://stomp.github.io/ |
| 6 | +At now library works only as client for backend with support STOMP, such as |
| 7 | +NodeJS (stompjs or other) or Spring Boot (SockJS). |
| 8 | + |
| 9 | +## Example backend (Spring Boot) |
| 10 | + |
| 11 | +**WebSocketConfig.groovy** |
| 12 | +``` groovy |
| 13 | +@Configuration |
| 14 | +@EnableWebSocketMessageBroker |
| 15 | +public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { |
| 16 | +
|
| 17 | + @Override |
| 18 | + public void configureMessageBroker(MessageBrokerRegistry config) { |
| 19 | + config.enableSimpleBroker("/topic"); |
| 20 | + config.setApplicationDestinationPrefixes("/app"); |
| 21 | + } |
| 22 | +
|
| 23 | + @Override |
| 24 | + public void registerStompEndpoints(StompEndpointRegistry registry) { |
| 25 | + registry.addEndpoint("/hello")/*.setAllowedOrigins('*')*/.withSockJS(); |
| 26 | + } |
| 27 | +
|
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +**HelloSockController** |
| 32 | +``` groovy |
| 33 | +@RestController |
| 34 | +class BoardSockController { |
| 35 | +
|
| 36 | + @MessageMapping("/hello") |
| 37 | + @SendTo("/topic/greetings") |
| 38 | + public String greeting(String msg) throws Exception { |
| 39 | + println("Receive greeting ${msg}") |
| 40 | + return "ECHO: " + msg; |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Example library usage |
| 46 | + |
| 47 | +**Basic usage** |
| 48 | +``` java |
| 49 | + private StompClient mStompClient; |
| 50 | + |
| 51 | + // ... |
| 52 | + |
| 53 | + mStompClient = Stomp.over(WebSocketClient.class, Config.STOMP_BASE_URI); |
| 54 | + mStompClient.connect(); |
| 55 | + |
| 56 | + mStompClient.topic("/topic/greetings").subscribe(topicMessage -> { |
| 57 | + Log.d(TAG, topicMessage.getPayload()); |
| 58 | + }); |
| 59 | + |
| 60 | + mStompClient.send("/app/hello", "My first STOMP message!"); |
| 61 | + |
| 62 | + // ... |
| 63 | + |
| 64 | + mStompClient.disconnect(); |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +**Subscribe lifecycle connection** |
| 69 | +``` java |
| 70 | +mStompClient.lifecycle().subscribe(lifecycleEvent -> { |
| 71 | + switch (lifecycleEvent.getType()) { |
| 72 | + case ERROR: |
| 73 | + if (mCallback != null) mCallback.showError(lifecycleEvent.getException().getMessage()); |
| 74 | + break; |
| 75 | + case CLOSED: |
| 76 | + LOGD(TAG, "Stomp connection closed"); |
| 77 | + } |
| 78 | +}); |
| 79 | +``` |
0 commit comments