Java websocket client maven

javax.websocket client simple example

Can someone please provide me very simple example of websocket client using javax.websocket ? I want to connect to websocket (ws://socket.example.com:1234), send message (add channel) and listen to messages. All messages (sent & listened) are in JSON format. And btw is this library the best for simple websocket communication?

6 Answers 6

I’ve found a great example using javax.websocket here:

Here the code based on the example linked above:

TestApp.java :

package testapp; import java.net.URI; import java.net.URISyntaxException; public class TestApp < public static void main(String[] args) < try < // open websocket final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI("wss://real.okcoin.cn:10440/websocket/okcoinapi")); // add listener clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() < public void handleMessage(String message) < System.out.println(message); >>); // send message to websocket clientEndPoint.sendMessage(""); // wait 5 seconds for messages from websocket Thread.sleep(5000); > catch (InterruptedException ex) < System.err.println("InterruptedException exception: " + ex.getMessage()); >catch (URISyntaxException ex) < System.err.println("URISyntaxException exception: " + ex.getMessage()); >> > 

WebsocketClientEndpoint.java :

package testapp; import java.net.URI; import javax.websocket.ClientEndpoint; import javax.websocket.CloseReason; import javax.websocket.ContainerProvider; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.WebSocketContainer; /** * ChatServer Client * * @author Jiji_Sasidharan */ @ClientEndpoint public class WebsocketClientEndpoint < Session userSession = null; private MessageHandler messageHandler; public WebsocketClientEndpoint(URI endpointURI) < try < WebSocketContainer container = ContainerProvider.getWebSocketContainer(); container.connectToServer(this, endpointURI); >catch (Exception e) < throw new RuntimeException(e); >> /** * Callback hook for Connection open events. * * @param userSession the userSession which is opened. */ @OnOpen public void onOpen(Session userSession) < System.out.println("opening websocket"); this.userSession = userSession; >/** * Callback hook for Connection close events. * * @param userSession the userSession which is getting closed. * @param reason the reason for connection close */ @OnClose public void onClose(Session userSession, CloseReason reason) < System.out.println("closing websocket"); this.userSession = null; >/** * Callback hook for Message Events. This method will be invoked when a client send a message. * * @param message The text message */ @OnMessage public void onMessage(String message) < if (this.messageHandler != null) < this.messageHandler.handleMessage(message); >> @OnMessage public void onMessage(ByteBuffer bytes) < System.out.println("Handle byte buffer"); >/** * register message handler * * @param msgHandler */ public void addMessageHandler(MessageHandler msgHandler) < this.messageHandler = msgHandler; >/** * Send a message. * * @param message */ public void sendMessage(String message) < this.userSession.getAsyncRemote().sendText(message); >/** * Message handler. * * @author Jiji_Sasidharan */ public static interface MessageHandler < public void handleMessage(String message); >> 

Hi, how to make this code work if the websocketServer is sending a continuous stream of messages and websocketClient needs to consume the messages one by one? I get the error «The decoded text message was too big for the output buffer and the endpoint does not support partial messages» after running the code for about a minute

Читайте также:  Что такое парсинг php

@deathgaze javax.websocket api is only the specification don’t have full implementation you may need to take the jar file tyrus-standalone-client-1.9.jar and try the same example that should solve your problem. i tested with my example and it is working fine. hope this will help you.

for those who is getting error «Could not find an implementation class.» should also include tyrus-standalone-client-1.9.jar while building . you can download it from here

Just add the java_websocket.jar in the dist folder into your project.

 import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft_10; import org.java_websocket.handshake.ServerHandshake; import org.json.JSONException; import org.json.JSONObject; WebSocketClient mWs = new WebSocketClient( new URI( "ws://socket.example.com:1234" ), new Draft_10() ) < @Override public void onMessage( String message ) < JSONObject obj = new JSONObject(message); String channel = obj.getString("channel"); >@Override public void onOpen( ServerHandshake handshake ) < System.out.println( "opened connection" ); >@Override public void onClose( int code, String reason, boolean remote ) < System.out.println( "closed connection" ); >@Override public void onError( Exception ex ) < ex.printStackTrace(); >>; //open websocket mWs.connect(); JSONObject obj = new JSONObject(); obj.put("event", "addChannel"); obj.put("channel", "ok_btccny_ticker"); String message = obj.toString(); //send message mWs.send(message); 

Great library, but it has problems with wss. The project has several open issues, and the developer comments he does not have time anymore.

Have a look at this Java EE 7 examples from Arun Gupta.

/** * @author Arun Gupta */ public class Client < final static CountDownLatch messageLatch = new CountDownLatch(1); public static void main(String[] args) < try < WebSocketContainer container = ContainerProvider.getWebSocketContainer(); String uri = "ws://echo.websocket.org:80/"; System.out.println("Connecting to " + uri); container.connectToServer(MyClientEndpoint.class, URI.create(uri)); messageLatch.await(100, TimeUnit.SECONDS); >catch (DeploymentException | InterruptedException | IOException ex) < Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); >> > 

ClientEndpoint

/** * @author Arun Gupta */ @ClientEndpoint public class MyClientEndpoint < @OnOpen public void onOpen(Session session) < System.out.println("Connected to endpoint: " + session.getBasicRemote()); try < String name = "Duke"; System.out.println("Sending message to endpoint: " + name); session.getBasicRemote().sendText(name); >catch (IOException ex) < Logger.getLogger(MyClientEndpoint.class.getName()).log(Level.SEVERE, null, ex); >> @OnMessage public void processMessage(String message) < System.out.println("Received message in client: " + message); Client.messageLatch.countDown(); >@OnError public void processError(Throwable t) < t.printStackTrace(); >> 

Use this library org.java_websocket

First thing you should import that library in build.gradle

then add the implementation in dependency<>

implementation "org.java-websocket:Java-WebSocket:1.3.0" 

Then you can use this code

In your activity declare object for Websocketclient like

private WebSocketClient mWebSocketClient; 

then add this method for callback

 private void ConnectToWebSocket() < URI uri; try < uri = new URI("ws://your web socket url"); >catch (URISyntaxException e) < e.printStackTrace(); return; >mWebSocketClient = new WebSocketClient(uri) < @Override public void onOpen(ServerHandshake serverHandshake) < Log.i("Websocket", "Opened"); mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL); >@Override public void onMessage(String s) < final String message = s; runOnUiThread(new Runnable() < @Override public void run() < TextView textView = (TextView)findViewById(R.id.edittext_chatbox); textView.setText(textView.getText() + "\n" + message); >>); > @Override public void onClose(int i, String s, boolean b) < Log.i("Websocket", "Closed " + s); >@Override public void onError(Exception e) < Log.i("Websocket", "Error " + e.getMessage()); >>; mWebSocketClient.connect(); 

I have Spring 4.2 in my project and many SockJS Stomp implementations usually work well with Spring Boot implementations. This implementation from Baeldung worked(for me without changing from Spring 4.2 to 5). After Using the dependencies mentioned in his blog, it still gave me ClassNotFoundError. I added the below dependency to fix it.

 org.springframework spring-core 4.2.3.RELEASE  

Spring was not questioned for. Other java-dependent client applications (e.g. android app’s) cannot make use of this

@MojioMS But what is the probability that someone will open this question for SpringMVC and I will offer that answer? Isn’t it better the OP ignores this answer because he has Java but not spring mvc? For those who have a websocket issue and have spring, this contribution could be useful.

Well, I’ve made that — lets call it ‘mistake’ — too. I didn’t vote down your answer but just wanted to give an explanation. It might be useful to add some kind of ‘Spring Framework solution’-title to your answer.

Here is such a solution from — com.neovisionary.ws.client.Web socket — https://github.com/TakahikoKawasaki/nv-websocket-client

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketFactory; import com.neovisionaries.ws.client.WebSocketFrame; public class WssM < public static ListListMessage = new ArrayList<>(); public static boolean WebSocketLog = true; public static final String WssURL = "wss://site.com/api/sport_tree_ws/v1"; public static final String WebsocketMessage = ">"; public static void main(String[] args) throws IOException, WebSocketException < WebSocket socket = connect(WssURL); BufferedReader in = getInput(); socket.sendText(WebsocketMessage); String text; try < while ((text = in.readLine()) != null) < if (text.equals("exit")) break; if (!socket.isOpen()) < System.out.println("Socket is closed. Trying to reconnect. "); socket.recreate().connect(); System.out.println("Reconnected!"); >> > catch (Exception e) < e.printStackTrace(); >finally < if (socket.isOpen()) < System.out.println("Disconnecting connection to server!"); socket.disconnect(); //Close the WebSocket. >> > private static WebSocket connect(String Host) throws IOException, WebSocketException < WebSocketFactory wsFactory = new WebSocketFactory().setConnectionTimeout(55000); WebSocket socket = wsFactory.createSocket(URI.create(Host)).addProtocol("json");//.addHeader("Sec-WebSocket-Protocol", "json") //WebSocket socket = wsFactory.createSocket(URI.create(HOST + "?Authorization=" + DUMMY_JWT_TOKEN)); socket.addListener(new WebSocketAdapter() < @Override public void onSendingHandshake(WebSocket websocket, String requestLine, Listheaders) < if (WebSocketLog) System.out.println(requestLine); for (String[] header : headers) < //Print the header, ": " if (WebSocketLog) System.out.format("%s: %s\n", header[0], header[1]); > > @Override public void onConnected(WebSocket websocket, Map headers) < if (WebSocketLog) System.out.println("Success! WebSocket - Connected!"); >@Override public void onTextMessage(WebSocket websocket, String text) < if (WebSocketLog) System.out.printf("MessageToClient: %s%n", text); ListMessage.add(text); >@Override public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) < if (WebSocketLog) System.out.println("Disconnecting. "); if (WebSocketLog) System.out.printf(" Opcode: %d%n", serverCloseFrame.getOpcode()); >@Override public void onPongFrame(WebSocket websocket, WebSocketFrame frame) < if (WebSocketLog) System.out.printf("Received some pong. Payload text: %s%n", frame.getPayloadText()); System.out.printf(" Opcode: %d%n", frame.getOpcode()); >@Override public void onPingFrame(WebSocket websocket, WebSocketFrame frame) < if (WebSocketLog) System.out.printf("I have been pinged by server at %s%n", LocalDateTime.now()); websocket.sendPong("Ponging from client"); >@Override public void onTextFrame(WebSocket websocket, WebSocketFrame frame) < if (WebSocketLog) System.out.printf("onTextFrame - %s%n", LocalDateTime.now()); websocket.sendPong("onTextFrame from client"); >@Override public void onError(WebSocket websocket, WebSocketException cause) < System.out.printf("I have received exception %s%n", cause.getMessage()); >>).connect(); return socket; > private static BufferedReader getInput() < return new BufferedReader(new InputStreamReader(System.in)); >> 

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A barebones WebSocket client and server implementation written in 100% Java.

License

TooTallNate/Java-WebSocket

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.markdown

This repository contains a barebones WebSocket server and client implementation written in 100% Java. The underlying classes are implemented java.nio , which allows for a non-blocking event-driven model (similar to the WebSocket API for web browsers).

Implemented WebSocket protocol versions are:

Here some more details about protocol versions/drafts. PerMessageDeflateExample enable the extension with reference to both a server and client example.

Dependency management tools

Below is a brief guide to using dependency management tools like maven or gradle.

To use maven add this dependency to your pom.xml:

dependency> groupId>org.java-websocketgroupId> artifactId>Java-WebSocketartifactId> version>1.5.3version> dependency>

To use Gradle add the maven central repository to your repositories list:

Then you can just add the latest version to your build.

compile "org.java-websocket:Java-WebSocket:1.5.3"

Or this option if you use gradle 7.0 and above.

implementation 'org.java-websocket:Java-WebSocket:1.5.3'

This library uses SLF4J for logging and does not ship with any default logging implementation.

Exceptions are using the log level ERROR and debug logging will be done with log level TRACE .

Feel free to use whichever logging framework you desire and use the corresponding binding in your dependency management.

If you want to get started, take a look at the SimpleLogger example.

If you do not use any dependency management tool, you can find the latest standalone jar here.

Writing your own WebSocket Server

The org.java_websocket.server.WebSocketServer abstract class implements the server-side of the WebSocket Protocol. A WebSocket server by itself doesn’t do anything except establish socket connections though HTTP. After that it’s up to your subclass to add purpose.

An example for a WebSocketServer can be found in both the wiki and the example folder.

Writing your own WebSocket Client

The org.java_websocket.client.WebSocketClient abstract class can connect to valid WebSocket servers. The constructor expects a valid ws:// URI to connect to. Important events onOpen , onClose , onMessage and onError get fired throughout the life of the WebSocketClient, and must be implemented in your subclass.

An example for a WebSocketClient can be found in both the wiki and the example folder.

You can find a lot of examples here.

This library supports wss. To see how to use wss please take a look at the examples.

If you do not have a valid certificate in place then you will have to create a self signed one. Browsers will simply refuse the connection in case of a bad certificate and will not ask the user to accept it. So the first step will be to make a browser to accept your self signed certificate. ( https://bugzilla.mozilla.org/show_bug.cgi?id=594502 ).
If the websocket server url is wss://localhost:8000 visit the url https://localhost:8000 with your browser. The browser will recognize the handshake and allow you to accept the certificate.

The vm option -Djavax.net.debug=all can help to find out if there is a problem with the certificate.

It is currently not possible to accept ws and wss connections at the same time via the same websocket server instance.

For some reason Firefox does not allow multiple connections to the same wss server if the server uses a different port than the default port (443).

If you want to use wss on the android platform you should take a look at this.

I ( @Davidiusdadi ) would be glad if you would give some feedback whether wss is working fine for you or not.

Java-WebSocket is known to work with:

Other JRE implementations may work as well, but haven’t been tested.

Everything found in this repo is licensed under an MIT license. See the LICENSE file for specifics.

About

A barebones WebSocket client and server implementation written in 100% Java.

Источник

Оцените статью