forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-252(A Java Client to consume a WebSockets API) (eugenp#1791)
* @BAEL-252 - Initial checkin * BAEL-252 : Added junit tests
- Loading branch information
1 parent
2c790c4
commit 4a3662b
Showing
5 changed files
with
162 additions
and
4 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
25 changes: 25 additions & 0 deletions
25
spring-boot/src/main/java/org/baeldung/websocket/client/Message.java
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,25 @@ | ||
package org.baeldung.websocket.client; | ||
|
||
public class Message { | ||
|
||
private String from; | ||
private String text; | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public String getFrom() { | ||
return from; | ||
} | ||
|
||
public void setFrom(String from) { | ||
this.from = from; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
|
||
} |
58 changes: 58 additions & 0 deletions
58
spring-boot/src/main/java/org/baeldung/websocket/client/MyStompSessionHandler.java
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,58 @@ | ||
package org.baeldung.websocket.client; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.springframework.messaging.simp.stomp.StompCommand; | ||
import org.springframework.messaging.simp.stomp.StompHeaders; | ||
import org.springframework.messaging.simp.stomp.StompSession; | ||
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* This class is an implementation for <code>StompSessionHandlerAdapter</code>. | ||
* Once a connection is established, We subscribe to /topic/messages and | ||
* send a sample message to server. | ||
* | ||
* @author Kalyan | ||
* | ||
*/ | ||
public class MyStompSessionHandler extends StompSessionHandlerAdapter { | ||
|
||
private Logger logger = Logger.getLogger(MyStompSessionHandler.class); | ||
|
||
@Override | ||
public void afterConnected(StompSession session, StompHeaders connectedHeaders) { | ||
logger.info("New session established : "+session.getSessionId()); | ||
session.subscribe("/topic/messages", this); | ||
logger.info("Subscribed to /topic/messages"); | ||
session.send("/app/chat", getSampleMessage()); | ||
logger.info("Message sent to websocket server"); | ||
} | ||
|
||
@Override | ||
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) { | ||
logger.error("Got an exception", exception); | ||
} | ||
|
||
@Override | ||
public Type getPayloadType(StompHeaders headers) { | ||
return Message.class; | ||
} | ||
|
||
@Override | ||
public void handleFrame(StompHeaders headers, Object payload) { | ||
Message msg = (Message)payload; | ||
logger.info("Received : "+ msg.getText()+ " from : "+msg.getFrom()); | ||
} | ||
|
||
/** | ||
* A sample message instance. | ||
* @return instance of <code>Message</code> | ||
*/ | ||
private Message getSampleMessage(){ | ||
Message msg = new Message(); | ||
msg.setFrom("Nicky"); | ||
msg.setText("Howdy!!"); | ||
return msg; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spring-boot/src/main/java/org/baeldung/websocket/client/StompClient.java
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,37 @@ | ||
package org.baeldung.websocket.client; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
import org.springframework.messaging.converter.MappingJackson2MessageConverter; | ||
import org.springframework.messaging.simp.stomp.StompSessionHandler; | ||
import org.springframework.web.socket.client.standard.StandardWebSocketClient; | ||
import org.springframework.web.socket.messaging.WebSocketStompClient; | ||
import org.springframework.web.socket.sockjs.client.SockJsClient; | ||
import org.springframework.web.socket.sockjs.client.Transport; | ||
import org.springframework.web.socket.sockjs.client.WebSocketTransport; | ||
|
||
/** | ||
* Stand alone WebSocketStompClient. | ||
* @author Kalyan | ||
* | ||
*/ | ||
public class StompClient { | ||
|
||
private static String URL = "ws://localhost:8080/spring-mvc-java/chat"; | ||
|
||
public static void main(String[] args) { | ||
Transport webSocketTransport = new WebSocketTransport(new StandardWebSocketClient()); | ||
List<Transport> transports = Collections.singletonList(webSocketTransport); | ||
SockJsClient sockJsClient = new SockJsClient(transports); | ||
|
||
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient); | ||
stompClient.setMessageConverter(new MappingJackson2MessageConverter()); | ||
|
||
StompSessionHandler sessionHandler = new MyStompSessionHandler(); | ||
stompClient.connect(URL, sessionHandler); | ||
|
||
new Scanner(System.in).nextLine(); // Don't close immediately. | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerTest.java
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,26 @@ | ||
package com.baeldung.websocket.client; | ||
|
||
import org.baeldung.websocket.client.MyStompSessionHandler; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.messaging.simp.stomp.StompHeaders; | ||
import org.springframework.messaging.simp.stomp.StompSession; | ||
|
||
/** | ||
* Test class for MyStompSessionHandler | ||
* @author Kalyan | ||
* | ||
*/ | ||
public class MyStompSessionHandlerTest { | ||
|
||
@Test | ||
public void testAfterConnectedSuccessfull() { | ||
StompSession mockSession = Mockito.mock(StompSession.class); | ||
StompHeaders mockHeader = Mockito.mock(StompHeaders.class); | ||
MyStompSessionHandler sessionHandler = new MyStompSessionHandler(); | ||
sessionHandler.afterConnected(mockSession, mockHeader); | ||
Mockito.verify(mockSession).subscribe("/topic/messages", sessionHandler); | ||
Mockito.verify(mockSession).send(Mockito.anyString(), Mockito.anyObject()); | ||
} | ||
} | ||
|