| // ======================================================================== |
| // Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd. |
| // ======================================================================== |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // and Apache License v2.0 which accompanies this distribution. |
| // |
| // The Eclipse Public License is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // The Apache License v2.0 is available at |
| // http://www.opensource.org/licenses/apache2.0.php |
| // |
| // You may elect to redistribute this code under either of these licenses. |
| // ======================================================================== |
| |
| [[jetty-websocket-client-api]] |
| === Jetty WebSocket Client API |
| |
| Jetty also provides a Jetty WebSocket Client Library to write make talking to WebSocket servers easier. |
| |
| To use the Jetty WebSocket Client on your own Java project you will need the following maven artifacts. |
| |
| [source, xml, subs="{sub-order}"] |
| ---- |
| <dependency> |
| <groupId>org.eclipse.jetty.websocket</groupId> |
| <artifactId>websocket-client</artifactId> |
| <version>${project.version}</version> |
| </dependency> |
| ---- |
| |
| ==== The WebSocketClient |
| |
| To use the WebSocketClient you will need to hook up a WebSocket object instance to a specific destination WebSocket URI. |
| |
| [source, java, subs="{sub-order}"] |
| ---- |
| include::{SRCDIR}/jetty-websocket/websocket-client/src/test/java/examples/SimpleEchoClient.java[] |
| ---- |
| |
| The above example connects to a remote WebSocket server and hands off a SimpleEchoSocket to perform the logic on the websocket once connected, waiting for the socket to register that it has closed. |
| |
| [source, java, subs="{sub-order}"] |
| ---- |
| include::{SRCDIR}/jetty-websocket/websocket-client/src/test/java/examples/SimpleEchoSocket.java[] |
| ---- |
| |
| When the SimpleEchoSocket connects, it sends 2 Text messages and then closes the socket. |
| |
| The onMessage(String msg) receives the responses from the remote server WebSocket and outputs them to the console. |