Skip to content

IvanCL/spring-websocket-java-client-demo

 
 

Repository files navigation

spring-websocket-java-client-demo

Demo of connecting java client to spring web application via websocket

Usage

WebSocket Spring Boot Server

git clone this project, run the "./make.ps1" powershell script in the project root directory to build spring-boot-application.jar into the "bin" folder.

Run the following command to start the spring-boot-application at http://localhost:8080

java -jar bin/spring-boot-application.jar

The spring-boot-application defines an end point at http://localhost:8080/my-ws and sends a ping message to any connected client that subscribe to its topic "/topics/event" every 10 seconds. the angularjs demo can be viewed by navigating to http://localhost:8080 on your web browser.

Java WebSocket Client

The spring-boot-java-client project contains the implementation of the java websocket client. To subscribe to the "/topics/event" at http://localhost:8080/my-ws, run the following java codes:

SpringBootWebSocketClient client = new SpringBootWebSocketClient();
client.setId("sub-001");
TopicHandler handler = client.subscribe("/topics/event");
handler.addListener(new StompMessageListener() {
 @Override
 public void onMessage(StompMessage message) {
    System.out.println(message.getHeader("destination") + ": " + message.getContent());
 }
});
client.connect("ws://localhost:8080/my-ws/websocket");
Thread.sleep(60000L);
client.disconnect();

About

Demo on how to integrate spring websocket on the server with java client

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 62.3%
  • JavaScript 15.0%
  • HTML 12.2%
  • CSS 9.6%
  • PowerShell 0.9%