How to close a websocket connection using Java WebSocket API? I have used Java websocket API for both server and client end points. The application is working fine. But I don't know how to close the websocket, before the main thread ends.
This is my ClientEndpoint
package websocket.client;
import java.io.IOException;
import javax.websocket.MessageHandler;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
@ClientEndpoint
public class EchoClient {
Session session;
//request
@OnOpen
public void onOpen(Session session, EndpointConfig config) {
System.out.println("Connected to endpoint: " + session.getBasicRemote());
this.session = session;
sendMessage("Welcome to WebSocket");
}
//response
@OnMessage
public void onMessage(String text) {
System.out.println("Received response in client from server: " + text);
}
@OnError
public void onError(Session session, Throwable t) {
t.printStackTrace();
}
private void sendMessage(String message) {
System.out.println("Sending message from client to server: " + message);
System.out.println(session);
try {
session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
And I use the following code to start the ClientEndPoint
import java.io.IOException;
import java.net.URI;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.WebSocketContainer;
import websocket.client.EchoClient;
public class WebSocketDemo {
public static void main(String[] args) {
String uri = "ws://localhost:8080/websocket";
System.out.println("Connecting to " + uri);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
try {
container.connectToServer(EchoClient.class, URI.create(uri));
} catch (DeploymentException | IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
PS : I haven't used JavaScript.
If you are writing a server, you should make sure to send a close frame when the server closes a client connection. The normal TCP socket close method can sometimes be slow and cause applications to think the connection is still open even when it's not.
The whole WebSocket object should be deleted (alongside it's buffer) or collected by the garbage collector (make sure to remove any existing references to the object), or the data will live on in the memory. Thanks for your answer. I decided to just close connection and garbage collect original websocket.
You can check if a WebSocket is disconnected by doing either of the following: Specifying a function to the WebSocket. onclose event handler property, or; Using addEventListener to listen to the close event.
However, the connection between a client and your WebSocket app closes when no traffic is sent between them for 60 seconds.
The WebSocketContainer
's connectToServer
method returns websocket Session
object that has two close methods. That should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With