Debian Codegraph @

jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-client/src/test/java/examples/SimpleEchoSocket.java

raw symbols SCIP
1 //
2 // ========================================================================
3 // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4 //
5 // This program and the accompanying materials are made available under the
6 // terms of the Eclipse Public License v. 2.0 which is available at
7 // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8 // which is available at https://www.apache.org/licenses/LICENSE-2.0.
9 //
10 // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11 // ========================================================================
12 //
13
14 package examples;
15
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.TimeUnit;
18
19 import org.eclipse.jetty.ee9.websocket.api.Session;
20 import org.eclipse.jetty.ee9.websocket.api.StatusCode;
21 import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketClose;
22 import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketConnect;
23 import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketMessage;
24 import org.eclipse.jetty.ee9.websocket.api.annotations.WebSocket;
25
26 /**
27 * Basic Echo Client Socket
28 */
29 @WebSocket(maxTextMessageSize = 64 * 1024)
30 public class SimpleEchoSocket
31 {
32 private final CountDownLatch closeLatch;
33 @SuppressWarnings("unused")
34 private Session session;
35
36 public SimpleEchoSocket()
37 {
38 this.closeLatch = new CountDownLatch(1);
39 }
40
41 public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException
42 {
43 return this.closeLatch.await(duration, unit);
44 }
45
46 @OnWebSocketClose
47 public void onClose(int statusCode, String reason)
48 {
49 System.out.printf("Connection closed: %d - %s%n", statusCode, reason);
50 this.session = null;
51 this.closeLatch.countDown(); // trigger latch
52 }
53
54 @OnWebSocketConnect
55 public void onConnect(Session session)
56 {
57 System.out.printf("Got connect: %s%n", session);
58 this.session = session;
59 try
60 {
61 session.getRemote().sendString("Hello");
62 session.getRemote().sendString("Thanks for the conversation.");
63 session.close(StatusCode.NORMAL, "I'm done");
64 }
65 catch (Throwable t)
66 {
67 t.printStackTrace();
68 }
69 }
70
71 @OnWebSocketMessage
72 public void onMessage(String msg)
73 {
74 System.out.printf("Got msg: %s%n", msg);
75 }
76 }