|
| 1 | +# STOMP protocol via WebSocket for Android |
| 2 | + |
| 3 | +[](https://jitpack.io/#NaikSoftware/StompProtocolAndroid) |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This library provide support for STOMP protocol https://stomp.github.io/ |
| 8 | +At now library works only as client for backend with support STOMP, such as |
| 9 | +NodeJS (stompjs or other) or Spring Boot (SockJS). |
| 10 | + |
| 11 | +Add library as gradle dependency |
| 12 | + |
| 13 | +```gradle |
| 14 | +repositories { |
| 15 | + jcenter() |
| 16 | + maven { url "https://jitpack.io" } |
| 17 | +} |
| 18 | +dependencies { |
| 19 | + compile 'com.github.NaikSoftware:StompProtocolAndroid:{latest version}' |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +## Example backend (Spring Boot) |
| 24 | + |
| 25 | +**WebSocketConfig.groovy** |
| 26 | +```groovy |
| 27 | +@Configuration |
| 28 | +@EnableWebSocketMessageBroker |
| 29 | +public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { |
| 30 | +
|
| 31 | + @Override |
| 32 | + public void configureMessageBroker(MessageBrokerRegistry config) { |
| 33 | + config.enableSimpleBroker("/topic"); |
| 34 | + config.setApplicationDestinationPrefixes("/app"); |
| 35 | + } |
| 36 | +
|
| 37 | + @Override |
| 38 | + public void registerStompEndpoints(StompEndpointRegistry registry) { |
| 39 | + registry.addEndpoint("/hello")/*.setAllowedOrigins('*')*/.withSockJS(); |
| 40 | + } |
| 41 | +
|
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +**HelloSockController.groovy** |
| 46 | +``` groovy |
| 47 | +@RestController |
| 48 | +class HelloSockController { |
| 49 | +
|
| 50 | + @MessageMapping("/hello") |
| 51 | + @SendTo("/topic/greetings") |
| 52 | + def greeting(String msg) throws Exception { |
| 53 | + println("Receive greeting ${msg}") |
| 54 | + "ECHO: " + msg; |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## Example library usage |
| 60 | + |
| 61 | +**Basic usage** |
| 62 | +``` java |
| 63 | + private StompClient mStompClient; |
| 64 | + |
| 65 | + // ... |
| 66 | + |
| 67 | + mStompClient = Stomp.over(WebSocket.class, "ws://localhost:8080/app/hello/websocket"); |
| 68 | + mStompClient.connect(); |
| 69 | + |
| 70 | + mStompClient.topic("/topic/greetings").subscribe(topicMessage -> { |
| 71 | + Log.d(TAG, topicMessage.getPayload()); |
| 72 | + }); |
| 73 | + |
| 74 | + mStompClient.send("/app/hello", "My first STOMP message!").subscribe(); |
| 75 | + |
| 76 | + // ... |
| 77 | + |
| 78 | + mStompClient.disconnect(); |
| 79 | + |
| 80 | +``` |
| 81 | + |
| 82 | +Method `Stomp.over` consume class for create connection as first parameter. |
| 83 | +You must provide dependency for lib and pass class. |
| 84 | +At now supported connection providers: |
| 85 | +- WebSocket.class ('org.java-websocket:Java-WebSocket:1.3.0') |
| 86 | + |
| 87 | +You can add own connection provider. Just implement interface `ConnectionProvider`. |
| 88 | +If you implement new provider, please create pull request :) |
| 89 | + |
| 90 | +**Subscribe lifecycle connection** |
| 91 | +``` java |
| 92 | +mStompClient.lifecycle().subscribe(lifecycleEvent -> { |
| 93 | + switch (lifecycleEvent.getType()) { |
| 94 | + |
| 95 | + case OPENED: |
| 96 | + Log.d(TAG, "Stomp connection opened"); |
| 97 | + break; |
| 98 | + |
| 99 | + case ERROR: |
| 100 | + Log.e(TAG, "Error", lifecycleEvent.getException()); |
| 101 | + break; |
| 102 | + |
| 103 | + case CLOSED: |
| 104 | + Log.d(TAG, "Stomp connection closed"); |
| 105 | + break; |
| 106 | + } |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +Library support just send & receive messages. ACK messages, transactions not implemented yet. |
0 commit comments