Recently, the Qiita article has become a progress report. This time, it is a continuation of the article here.
When I write and execute a client program normally, I get the following error.
Exception in thread "main" java.lang.RuntimeException: Could not find an implementation class.
	at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73)
	at Main.main(Main.java:46)
According to stackoverrun, websocket provides a Client interface, but doesn't seem to provide an implementation. Therefore, we will introduce tyrus-standalone-client in glassfish. How to implement WebSocketContainer in StandardWebSocketClient class
Specifically, add it to the dependencies of build.gradle.
dependencies {
    //I explained last time
    compileOnly 'javax:javaee-api:8.0'
    //This is what I will add this time
    compile group: 'org.glassfish.tyrus.bundles', name: 'tyrus-standalone-client', version: '1.14'
}
So, let's write a program this time.
import javax.websocket.*;
import java.net.URI;
import java.util.Scanner;
//With this annotation, it is recognized as the endpoint of the client.
@ClientEndpoint
public class Client {
    public Client() {
        super();
    }
    //Processing when establishing a session
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("[Session establishment]");
    }
    //Processing when receiving a message
    @OnMessage
    public void onMessage(String message) {
        System.out.println("[Receive]:" + message);
    }
    //Processing when receiving a message
    @OnError
    public void onError(Throwable th) {
        System.err.println(th.getMessage());
    }
    //Processing at session release
    @OnClose
    public void onClose(Session session) {
        System.out.println("[Disconnect]");
    }
    static public void main(String[] args) throws Exception {
        //Get the object of WebSocket container for initialization
        WebSocketContainer container = ContainerProvider
                .getWebSocketContainer();
        //Server endpoint URI
        URI uri = URI
                .create("ws://localhost:8080/<Project name>/<Link>");
        //Establish a session with the server endpoint
        //From the running client, look for the same class passed as an argument and connect?
        Session session = container.connectToServer(Client.class, uri);
        while (!session.isOpen()) {
            Thread.sleep(500);
        }
        System.out.println("open");
        try (Scanner s = new Scanner(System.in)) {
            String str;
            System.out.println("start loop");
            while (true) {
                str = s.nextLine();
                if(str.equals("exit")) break;
                //Send the contents of str
                session.getBasicRemote().sendText(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Since it is troublesome, I wrote main in the Endpoint class this time, but I feel that it is better to separate it.
First, start the server containing Client with tomcatRun etc., and then execute main in another window.
Recommended Posts