I use Java language. The code of client are as follow:
import javax.websocket.*;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static WebSocketClient client;
public static void main(String[] args) throws Exception {
client = new WebSocketClient();
client.close();
}
}
And the content of WebSocketClient.java is:
import javax.websocket.*;
import java.io.IOException;
@ClientEndpoint
public class WebSocketClient implements AutoCloseable{
private final Session session;
public WebSocketClient() throws DeploymentException, IOException {
String URI = "ws://127.0.0.1:8080/ws";
this.session =
ContainerProvider.getWebSocketContainer()
.connectToServer(WebSocketClient.class, java.net.URI.create(URI));
}
@OnOpen
public void onOpen(Session session){
System.out.println("Connection Established:" + session.getId());
}
@OnMessage
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
@OnError
public void onError(Throwable throwable){
System.out.println(throwable.getMessage());
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
public void disconnect() throws IOException {
this.session.close();
}
@Override
public void close() throws Exception {
this.session.close();
}
}
Only 2 files in the project.
The content of pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>websocker-client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>1.13</version>
</dependency>
</dependencies>
</project>
I try to run this appliction. What I expected is when the connect established, the server will receive the request. The code of server when it handle the establishment is:
package com.example.websocketserver;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;
@Component
@ServerEndpoint("/ws")
public class WebSocketServer{
@OnOpen
public void onOpen(Session session){
System.out.println("Successfully establish the connection:" + session.getId());
}
@OnMessage
public void onMessage(Session session, String message){
System.out.println("Received message from:" + session.getId() + ", content is:" + message);
}
@OnClose
public void onClose(Session session){
System.out.println("Connection closed:" + session.getId());
}
}
The problem is: The server outputs lot of text of establishment of connection. I think the server is not the key of problem, because I transferred the client's connection establishment code directly to the main function and annotated the Main class with @ClientEndpoint (the corresponding logic has not changed), I can only establish a connection once, instead of establishing connections all the time, in cycles.
So the problem is how can I encapsulate this logic into the creation of the WebSocketClient object and still ensure that only one connection is established?