WebSockets
Websockets are mainly used for cross browser communication so in most cases the browser act as the client to connect to the socket server, But in some cases you might need a java client to send messages to a socket server. According to my research this area is not well documented. So I will try to implement a Java Client to access a websocket.
Prerequisites
For this sample application I will be Using a sample echo Websoket server located at "http://www.websocket.org/echo.html". You can also use wesocket server which I developed in a previous Blog post, You can Find it here.
I will be using Jetty for this implementation and Eclipse IDE for coding.
Developing the sample
Step 01
Create a new Java Project and lets call it "WebSocketJavaClient" and in the "src" directory create a new java Class with the following name in the mentioned package.
Step 02
Now add the following code segment to the newly created class.
Step 03
Now add the following Jar file as a External jar to your Class Path "jetty-all-9.2.1.v20140609.jar".
Note : I have tested it with the above version and you can use any other version and tryout, the above jar can be downloaded from here.
Step 04
Create a another java class with the following details.
Step 05
Now add the following Code snippet to the newly created class.
Note : I have added a delay in order to synchronize the Outgoing and incoming messages viewed on console, It is not necessary
Step 06
Now run the "SocketClient.java". When You enter a message the messages will be echoed back by the server.
Note : The code can be improved alot and this is the simplest sample that I can come-up with, You can refer to Jetty documentation for more detailed information. Please fell free to comment below if you have any questions.
You cqan Fing the Code from following Location, https://github.com/ycrnet/WebSocketJavaClient.git
Thanks for reading.
Websockets are mainly used for cross browser communication so in most cases the browser act as the client to connect to the socket server, But in some cases you might need a java client to send messages to a socket server. According to my research this area is not well documented. So I will try to implement a Java Client to access a websocket.
Prerequisites
For this sample application I will be Using a sample echo Websoket server located at "http://www.websocket.org/echo.html". You can also use wesocket server which I developed in a previous Blog post, You can Find it here.
I will be using Jetty for this implementation and Eclipse IDE for coding.
Developing the sample
Step 01
Create a new Java Project and lets call it "WebSocketJavaClient" and in the "src" directory create a new java Class with the following name in the mentioned package.
Step 02
Now add the following code segment to the newly created class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package org.socket.client; import java.io.BufferedReader; import java.io.InputStreamReader; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @WebSocket public class WebSocketObject { public Session session; @OnWebSocketClose public void onClose(int statusCode, String reason) { System.out.printf("Connection closed: %d - %s%n", statusCode, reason); this.session = null; } @OnWebSocketConnect public void onConnect(Session session) { System.out.printf("Got connect: %s%n", session); this.session = session; } @OnWebSocketMessage public void onMessage(String msg) { System.out.println("Message Recieved : "+ msg.toString()); } public void sendMessage(String message){ session.getRemote().sendStringByFuture(message); } public void closeConnection() { session.close(StatusCode.NORMAL, "[Consumer]Closing the session with the Server!!"); } } |
Step 03
Now add the following Jar file as a External jar to your Class Path "jetty-all-9.2.1.v20140609.jar".
Note : I have tested it with the above version and you can use any other version and tryout, the above jar can be downloaded from here.
Step 04
Create a another java class with the following details.
Step 05
Now add the following Code snippet to the newly created class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package org.socket.client; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.WebSocketClient; /** * Sample Websocket Client */ public class SocketClient { public static void main(String[] args) { // String destUri = "ws://localhost:8080/webSocketSample/server"; String destUri = "ws://echo.websocket.org"; String message; WebSocketClient client = new WebSocketClient(); WebSocketObject socket = new WebSocketObject(); try { client.start(); URI echoUri = new URI(destUri); ClientUpgradeRequest request = new ClientUpgradeRequest(); client.connect(socket, echoUri, request); System.out.println("Connecting to :" +echoUri); Thread.sleep(1000); BufferedReader myReader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("Enter Message : "); message = myReader.readLine(); socket.sendMessage(message); Thread.sleep(1000); } } catch (Throwable t) { t.printStackTrace(); } finally { try { socket.closeConnection(); client.stop(); } catch (Exception e) { e.printStackTrace(); } } } } |
Note : I have added a delay in order to synchronize the Outgoing and incoming messages viewed on console, It is not necessary
Step 06
Now run the "SocketClient.java". When You enter a message the messages will be echoed back by the server.
Note : The code can be improved alot and this is the simplest sample that I can come-up with, You can refer to Jetty documentation for more detailed information. Please fell free to comment below if you have any questions.
You cqan Fing the Code from following Location, https://github.com/ycrnet/WebSocketJavaClient.git
Thanks for reading.
Thanks for posting such an excellent informative content.
ReplyDelete