Minimal java8 NiO secure websocket client (WSS)
It took me a long time to find a simple java websocket client that can use WSS without mess
I tried https://github.com/TooTallNate/Java-WebSocket
He added dependencies in descrirbes and copied sslclientexample Java to use websocket The org echo server is tested, but a compilation error is encountered on line 84. There is no such method setsocket()... (stuck here)
I tried Tyrus (it seems that this is a large library directly developed by Oracle), but it seems that I need to run some appserver (websocket container) to use it
I don't know. Is it so difficult for WebSockets that need to use netty or GlassFish or grizly?
I think we can use SSL engine (WSS) and pure Java SDK to implement one... What do I don't know about WebSockets? (I think it's very much like an ordinary socket)
Solution
NV websocket client is a new websocket client library written in Java It supports WSS and only needs JAVA se 1.5, so it can even run on Android
nv-websocket-client-1.3. Jar (released on May 6, 2015) has a size of 62854 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."); } }
Blog websocket client library (Java se 1.5, Android) http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html
On GitHub https://github.com/TakahikoKawasaki/nv-websocket-client
Javadoc for http://takahikokawasaki.github.io/nv-websocket-client/
Maven's
<dependency> <groupId>com.neovisionaries</groupId> <artifactId>nv-websocket-client</artifactId> <version>1.3</version> </dependency>