GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - HertelDavid/StompedAndroidClient: Android WebSocket client built on top of OkHttp's WebSocket class using the Stomp protocol.

BranchesTags
Open more actions menu

Latest commit

 

History

6 Commits
6 Commits

Folders and files

NameName
Last commit message
Last commit date
.idea
.idea
 
 
app
app
 
 
gradle/wrapper
gradle/wrapper
 
 
stomped-lib
stomped-lib
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

Stomped

An Android Stomp WebSocket client built using OkHttp WebSockets.

Getting Started

Adding the Library to your list of dependencies.

Note: Adding this dependency will also add OkHttp to your project.

In your Project build.gradle file add this line in the appropriate spot.

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Next, in your app build.gradle file under dependencies, add this.

compile 'com.github.HertelDavid:StompedAndroidClient:1.0'

Use

Here is a simple example of what can be done in response to a button being pressed.

The backend Spring WebSocket server code.

@Controller
public class QueueController extends AbstractQueueController {

    @Autowired
    public QueueController(PersistentQueueService queueService, PersistentQueueThreadService queueThreadService) {
        super(queueService, queueThreadService);
    }

    @MessageMapping("/add/persistent-queue")
    @SendTo("/private/topic/persistent-queue")
    public String addUserToQueue(Principal principal, SimpMessageHeaderAccessor headerAccessor, UserImpl user){

        //Adds the designated user to the queue with the principal.
        //This is for later use when the queue needs to send a message back to the user.
        user.setUserPrincipal(principal);
        user.setMessagingTemplate(simpMessagingTemplate);
        persistentQueue.enqueue(user);

        notifyPersistentThread();

        return "Hello " + user.getUsername();
    }

And the Client code.

public void testClient(View view){

        final StompedClient client = new StompedClient.StompedClientBuilder().build("ws://10.0.2.2:8080/persistent-queue");
        final EditText editText = (EditText) findViewById(R.id.client_send_test);
        final TextView textView = (TextView) findViewById(R.id.client_return_test);

        client.subscribe("/private/topic/persistent-queue", new StompedListener() {

            @Override
            public void onNotify(final StompedFrame frame) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(frame.getStompedBody());
                        client.disconnect();
                    }
                });
            }
        });

        client.send("/app/add/persistent-queue", "{\"username\":\"" + editText.getText().toString() + "\"}");
    }

Explanation

In this example, a button was pressed and a new connection to the Spring Stomp server was created. In the backend code there is a mapping to the server (\app\add\persistent-queue) and a mapping back to the client (/private/topic/persistent-queue). The client subscribes to the path that sends messages from the server back to the client and then sends a message to the server with the associated payload which is of the JSON MIME type.

You can either create an anonymous StompedListener object or implement the StompedListener class in another file. Only the listener object passed to the subscribe method will be called when a server sends a message back to the client.

Note: The parameter type in the onNotify method contains all information related to the message sent back to the client. This includes details to the Command sent back, the headers, and, most importantly, the body.

More Details

Connecting to the Server example

final StompedClient stompedClient = new StompedClient.StompedClientBuilder()
	.build("ws://10.0.2.2:8080/persistent-queue");

Subscribing to a path

stompedClient.subscribe("/your/path", new StompedListener(){

	@Override
	public void onNotify(final StompedFrame frame){
		//Do work here.
	}
})

Retrieving Your Data

frame.getStompedBody();
// Or
frame.getStompedHeaders();
frame.getHeaderValueFromKey(StompedHeaders.STOMP_HEADER_DESTINATION);

Sending a Message with a Payload (You can always send a message with no payload too)

stompedClient.send("/your/path/here", "{\"firstName\":\"David\"}");

You can either create an anonymous StompedListener object or implement the StompedListener class in another file. Only the listener object passed to the subscribe method will be called when a server sends a message back to the client.

Some Important Points

  • Always make sure to add this to your Manifest.
<uses-permission android:name="android.permission.INTERNET" />
  • If you expect a message back from some method on the server in response to sending a message to the server, then always subscribe to the desired path before sending the message to the server. This will get rid of issues of subscribing to a path after the server has already sent a message back.

  • In my opinion, it is good practice to disconnect from the server if no data is being passed to the server or back to the client or if there are no future messages to be sent or received.

About

Android WebSocket client built on top of OkHttp's WebSocket class using the Stomp protocol.

Topics

androidandroid-libraryjavaokhttpstompstompmessageclientstompwebsocketwebsocketwebsocket-clientwebsockets

Resources

Readme
MIT license
Activity

Stars

10 stars

Watchers

1 watching

Forks

5 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.