1

In my current code i had a servlet from which if i create post to the servlet it will open a new websocket client , that mean 10 client connection each running for same purpose but with different api and secret , so i need to close particular session

I am using Jetty :: Websocket :: Client v9.4.48.v20220622

Please suggest , as i can get the session details but unable to use because it's not working with String data type . only in Session session it is working and i am unable to store session details anywhere else , as only in String data type i can save .

Whereas a is my API and b is my Secret Key ;

PS : Websocket connection is working fine to send expected data

class connector  {
    
    String a;
    String b;
    
    
    public void start() {

            WebSocketClient client = new WebSocketClient();
            MyWebSocket socket = new MyWebSocket();
            
            try {
                
                client.start();
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            URI destUri = null;
            try {
                destUri = new URI("wss://socket.delta.exchange");
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            ClientUpgradeRequest request = new ClientUpgradeRequest();
            System.out.println("Connecting to: " + destUri);
            try {
                client.connect(socket, destUri, request);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                socket.awaitClose(3600, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                client.stop();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @WebSocket
        public class MyWebSocket {
        
            private final CountDownLatch closeLatch = new CountDownLatch(1);

            @OnWebSocketConnect
            public void onConnect(Session session) throws IOException {
                
                session.getRemoteAddress();
                
                System.out.println("Connection opened");
                PingPong newObj = new PingPong();
                newObj.session = session;
                Authorization authMe = new Authorization();
                Identifier getSt = new Identifier();
                newObj.enableHeartBeat();
                System.out.println(session);
               
                
                session.getRemote().sendString(authMe.data(a, b));
               
            }

            @OnWebSocketMessage
            public void onMessage(String message) {
                
                MessageHandler objmsg = new  MessageHandler();
                objmsg.check();
                
                 System.out.println(
                            "Current Thread ID: "
                            + Thread.currentThread().getId());
                    
                System.out.println("Message from Server:  -- " + message);
            }
            
            @OnWebSocketClose
            public void onClose(int statusCode, String reason) {
                System.out.println("WebSocket Closed. Code:" + statusCode);
            }
            


            public boolean awaitClose(int duration, TimeUnit unit) 
                    throws InterruptedException {
                return this.closeLatch.await(duration, unit);
            }
        }
}

I want to do session.close() for a particular session details which i got from

session.getRemoteAddress().toString();


Session session ;
String sessionDetailSaved ;

i want to search for sessionDetailSaved and compare with all the on running sessions and close it

Or else any other way i can close particular session with different method may be interrupting session thread but sure it will not completely close connection .

Maven Dependency i am using

<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-client</artifactId>
    <version>9.4.48.v20220622</version>
</dependency>
plplmax
  • 1,765
  • 8
  • 19
Pervinder
  • 11
  • 3
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Aug 15 '22 at 06:32
  • https://stackoverflow.com/a/15649791/17650498 — is it what are you looking for? – plplmax Aug 15 '22 at 08:54
  • here in chat if someone left the chat window it will close session as further it is not communicating . in my case client connection will remain open until i fire a POST request with STOP trigger . – Pervinder Aug 15 '22 at 09:53

1 Answers1

0

Calling Session.close() will initiate a close handshake where the remote endpoint should reply with a response close frame, and once the close response has been received the WebSocket connection will be closed. You can send custom close status code and reason with Session.close(int statusCode, String reason).

You also have the option to call Session.disconnect() which will do a hard close of the underlying connection without sending this close frame.

In regards to your code, it looks like you are never completing the closeLatch in the OnWebSocketClose method, so your awaitClose method will always timeout.

Also, if possible you should try to re-use the same WebSocketClient instance for multiple connections because it is a heavy weight object. It is expensive to create a new one for each request.

Lachlan
  • 356
  • 1
  • 7