-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8739c7
commit c503767
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# STOMP protocol via WebSocket for Android | ||
|
||
## Overview | ||
|
||
This library provide support for STOMP protocol https://stomp.github.io/ | ||
At now library works only as client for backend with support STOMP, such as | ||
NodeJS (stompjs or other) or Spring Boot (SockJS). | ||
|
||
## Example backend (Spring Boot) | ||
|
||
**WebSocketConfig.groovy** | ||
``` groovy | ||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { | ||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry config) { | ||
config.enableSimpleBroker("/topic"); | ||
config.setApplicationDestinationPrefixes("/app"); | ||
} | ||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/hello")/*.setAllowedOrigins('*')*/.withSockJS(); | ||
} | ||
} | ||
``` | ||
|
||
**HelloSockController** | ||
``` groovy | ||
@RestController | ||
class BoardSockController { | ||
@MessageMapping("/hello") | ||
@SendTo("/topic/greetings") | ||
public String greeting(String msg) throws Exception { | ||
println("Receive greeting ${msg}") | ||
return "ECHO: " + msg; | ||
} | ||
} | ||
``` | ||
|
||
## Example library usage | ||
|
||
**Basic usage** | ||
``` java | ||
private StompClient mStompClient; | ||
|
||
// ... | ||
|
||
mStompClient = Stomp.over(WebSocketClient.class, Config.STOMP_BASE_URI); | ||
mStompClient.connect(); | ||
|
||
mStompClient.topic("/topic/greetings").subscribe(topicMessage -> { | ||
Log.d(TAG, topicMessage.getPayload()); | ||
}); | ||
|
||
mStompClient.send("/app/hello", "My first STOMP message!"); | ||
|
||
// ... | ||
|
||
mStompClient.disconnect(); | ||
|
||
``` | ||
|
||
**Subscribe lifecycle connection** | ||
``` java | ||
mStompClient.lifecycle().subscribe(lifecycleEvent -> { | ||
switch (lifecycleEvent.getType()) { | ||
case ERROR: | ||
if (mCallback != null) mCallback.showError(lifecycleEvent.getException().getMessage()); | ||
break; | ||
case CLOSED: | ||
LOGD(TAG, "Stomp connection closed"); | ||
} | ||
}); | ||
``` |