//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

[[websocket-jetty-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-jetty-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-jetty-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.
