Minimal client java8 nio secure websocket (wss)

I spent quite a bit of time finding a simple java-websocket client that can work with wss and will not be a mess ...

I tried https://github.com/TooTallNate/Java-WebSocket

a dependency is added because it is descirbes, copied SSLClientExample.java to test it with the websocket.org echo server, but received a compilation error on line 84 there is no such setSocket () method ... (stuck here)

I tried tyrus (it seems to be a large library developed directly by oracle), but it seems to me that I need to have the appserver application (websocket container) in order to be able to use it ...

Interestingly, it is so difficult in websockets that you need to have netty or glassfish or grizly for this?

I think it can be implemented using SSLEngine (wss) and pure java sdk ... is there something I don’t know about websites? (I imagine this is very similar to regular sockets)

+5
source share
3 answers

nv-websocket-client is the new WebSocket client library written in Java. It supports wss and requires only Java SE 1.5, so it can even work on Android.

The size of nv-websocket-client-1.3.jar (released on 2015-05-06) is 62,854 bytes and does not require any external dependencies.

The following is an example of "wss".

 import com.neovisionaries.ws.client.*; public class HelloWSS { public static void main(String[] args) throws Exception { // Connect to "wss://echo.websocket.org" and send "Hello." to it. // When a response from the WebSocket server is received, the // WebSocket connection is closed. new WebSocketFactory() .createSocket("wss://echo.websocket.org") .addListener(new WebSocketAdapter() { @Override public void onTextMessage(WebSocket ws, String message) { // Received a response. Print the received message. System.out.println(message); // Close the WebSocket connection. ws.disconnect(); } }) .connect() .sendText("Hello."); } } 

The blog
WebSocket Client Library (Java SE 1.5+, Android)
http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html

Github
https://github.com/TakahikoKawasaki/nv-websocket-client

Javoc
http://takahikokawasaki.imtqy.com/nv-websocket-client/

Maven

 <dependency> <groupId>com.neovisionaries</groupId> <artifactId>nv-websocket-client</artifactId> <version>1.3</version> </dependency> 
+16
source

Tyrus client does not need to have an application server! :)

See the Tirus Documentation and blogpost Reducing the size of the WebSocket client bank using ProGuard (you can get up to 500 kB with JDK 7 +).

About size - it can be minimized even more, but with some refactoring in Tyrus code. Comparing WebSocket and a regular socket is not very accurate - ordinary sockets do not need to implement HTTP and (traditionally) did not support NIO (which came with Java 7). The other part is the implementation of the WebSocket protocol, which is not so difficult, but also not just sending the byte [] to the wire. There is some handshake opening, signal frames and mandatory strict UTF-8 encoding / decoding.

So, I think you could find a simpler implementation of the API, but sticking to something that is supported and part of Java EE does not seem bad to me - you have the option to choose an implementation (The virus is only one of them, there is and others), and your client will be ready to be included in the Java EE application if this ever happens. (Editor's note: I am working on Tyrus, so my answer is most likely biased).

+1
source

Try using the simple Matthias web client. (I'm just one of his followers on Twitter). You can agree with his basic intention for this project.

From https://github.com/matzew/simple-websocket-client

JSR 356 describes a standard API for WebSocket Server and clients, but the client API is very complex. This project is designed to offer a simplified client.

BTW. Any WebSocket client does not require a WebSocket server and can write its own client in pure Java SE. However, since ease of integration with other technologies is more important than simplicity, you may feel some basic context there that seems complicated and unnecessary.

+1
source

Source: https://habr.com/ru/post/1216585/


All Articles