Debian Codegraph @

jetty-core/jetty-websocket/jetty-websocket-jetty-client/src/test/java/examples/SimpleEchoClient.java

raw
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.net.URI;
17 import java.util.concurrent.TimeUnit;
18
19 import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
20 import org.eclipse.jetty.websocket.client.WebSocketClient;
21
22 /**
23 * Example of a simple Echo Client.
24 */
25 public class SimpleEchoClient
26 {
27 public static void main(String[] args)
28 {
29 String destUri = "ws://echo.websocket.org";
30 if (args.length > 0)
31 {
32 destUri = args[0];
33 }
34
35 WebSocketClient client = new WebSocketClient();
36 SimpleEchoSocket socket = new SimpleEchoSocket();
37 try
38 {
39 client.start();
40
41 URI echoUri = new URI(destUri);
42 ClientUpgradeRequest request = new ClientUpgradeRequest();
43 client.connect(socket, echoUri, request);
44 System.out.printf("Connecting to : %s%n", echoUri);
45
46 // wait for closed socket connection.
47 socket.awaitClose(5, TimeUnit.SECONDS);
48 }
49 catch (Throwable t)
50 {
51 t.printStackTrace();
52 }
53 finally
54 {
55 try
56 {
57 client.stop();
58 }
59 catch (Exception e)
60 {
61 e.printStackTrace();
62 }
63 }
64 }
65 }