Skip to content

Commit 59c69f6

Browse files
committed
Release v0.1.0
1 parent 19468f4 commit 59c69f6

File tree

8 files changed

+480
-0
lines changed

8 files changed

+480
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target
2+
/.settings
3+
/.project
4+
/.classpath
5+
/.factorypath

CHANGELOG.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
= Changelog
2+
3+
== Version 0.1.0
4+
[cols="1,2,<10a", options="header"]
5+
|===
6+
|ID|Type|Description
7+
|https://github.com/Axway-API-Management-Plus/ws-client-simple/issues/1[#1]
8+
|Enhancement
9+
|Add simple client for WebSockets.
10+
|===

README.adoc

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
= Simple WebSocket Client
2+
:revnumber: 0.1.0
3+
:toc:
4+
:sectnums:
5+
:source-highlighter: prettify
6+
ifndef::env-github[]
7+
:icons: font
8+
:icon-set: far
9+
endif::[]
10+
ifdef::env-github[]
11+
:outfilesuffix: .adoc
12+
:!toc-title:
13+
:!sectnums:
14+
:caution-caption: :fire:
15+
:important-caption: :exclamation:
16+
:note-caption: :paperclip:
17+
:tip-caption: :bulb:
18+
:warning-caption: :warning:
19+
endif::[]
20+
21+
:prj-version: {revnumber}
22+
23+
image:https://img.shields.io/github/license/Axway-API-Management-Plus/ws-client-simple?style=plastic[]
24+
image:https://img.shields.io/github/v/release/Axway-API-Management-Plus/ws-client-simple?style=plastic[]
25+
26+
27+
== About the Tool
28+
29+
The tool is a simple client for WebSocket.
30+
It simulates multiple client endpoints and sends/received messages to/from a WebSocket server.
31+
It can be used to test WebSocket listeners of an API Gateway.
32+
Check Axway API Gateway documentation to https://docs.axway.com/bundle/axway-open-docs/page/docs/apim_policydev/apigw_gw_instances/general_websocket/index.html[configure WebSocket connections].
33+
34+
== Usage
35+
36+
The default behavior is to send a single text message to the `ws://echo.websocket.org` endpoint.
37+
38+
[source,shell,subs="verbatim,attributes"]
39+
----
40+
$ java -jar ws-simple-client-{prj-version}-jar-with-dependencies.jar
41+
INFO 10:29:06.452 - Connect to URI: ws://echo.websocket.org <1>
42+
INFO 10:29:07.512 - [1]: connected <2>
43+
INFO 10:29:07.513 - [1]: send - hello (#0) <3>
44+
INFO 10:29:07.628 - [1]: received - hello (#0) <4>
45+
INFO 10:29:09.522 - [1]: disconnected - 1 message(s) received <5>
46+
INFO 10:29:09.528 - all endpoints closed
47+
----
48+
<1> Client connects to `ws://echo.websocket.org`
49+
<2> The ID of the endpoint is shown in brackets.
50+
Each simulated endpoint has its own identifier.
51+
<3> Client endpoint sends first message to server.
52+
<4> Client endpoint received first message from server
53+
<5> WebSocket closed and client endpoint disconnected
54+
55+
To send two messages to the ws://192.168.56.201:8081/echo with three clients and delay of 500ms execute:
56+
57+
[source,shell,subs="verbatim,attributes"]
58+
----
59+
$ java -jar ws-simple-client-{proj-version}-jar-with-dependencies.jar --url ws://192.168.56.201:8081/echo --endpoints 3 --messages 2 --delay 500
60+
INFO 10:33:07.377 - Connect to URI: ws://192.168.56.201:8081/echo
61+
INFO 10:33:08.426 - [1]: connected <1>
62+
INFO 10:33:08.672 - [2]: connected
63+
INFO 10:33:08.890 - [3]: connected <2>
64+
INFO 10:33:08.890 - [1]: send - hello (#0)
65+
INFO 10:33:08.896 - [2]: send - hello (#0)
66+
INFO 10:33:08.897 - [3]: send - hello (#0)
67+
INFO 10:33:09.039 - [2]: received - hello (#0)
68+
INFO 10:33:09.048 - [3]: received - hello (#0)
69+
INFO 10:33:09.048 - [1]: received - hello (#0)
70+
INFO 10:33:09.400 - [1]: send - hello (#1)
71+
INFO 10:33:09.401 - [2]: send - hello (#1)
72+
INFO 10:33:09.402 - [3]: send - hello (#1)
73+
INFO 10:33:09.516 - [3]: received - hello (#1)
74+
INFO 10:33:09.516 - [2]: received - hello (#1)
75+
INFO 10:33:09.517 - [1]: received - hello (#1)
76+
INFO 10:33:10.906 - [1]: disconnected - 2 message(s) received
77+
INFO 10:33:10.907 - [2]: disconnected - 2 message(s) received
78+
INFO 10:33:10.908 - [3]: disconnected - 2 message(s) received
79+
INFO 10:33:10.912 - all endpoints closed
80+
----
81+
<1> First client endpoint connected to the server
82+
<2> Up to three endpoints are simulate with this call.
83+
84+
== Installation
85+
86+
For installation just download the latest version of the JAR from the https://github.com/orgs/Axway-API-Management-Plus/packages?repo_name=ws-client-simple[GitHub repository] and execute it (see example below):
87+
88+
.Show help screen
89+
[source,shell,subs="verbatim,attributes"]
90+
----
91+
$ java -jar ws-simple-client-{proj-version}-jar-with-dependencies.jar -h
92+
Usage: ws-client-simple [options]
93+
Options:
94+
-h, --help
95+
Show help screen.
96+
--url
97+
Target WebSocket endpoint
98+
Default: ws://echo.websocket.org
99+
--endpoints
100+
Number of parallel client endpoints
101+
Default: 1
102+
--messages
103+
Number of messages to be send to the server
104+
Default: 1
105+
--delay
106+
Delay between sending messages (milliseconds)
107+
Default: 1000
108+
----
109+
110+
111+
== Contributing
112+
Please read https://github.com/Axway-API-Management-Plus/Common/blob/master/Contributing.md[Contributing] for details on our code of conduct, and the process for submitting pull requests to us.
113+
114+
== Team
115+
image:https://raw.githubusercontent.com/Axway-API-Management-Plus/Common/master/img/AxwayLogoSmall.png[Axway Logo] Axway Team
116+
117+
== License
118+
link:./LICENSE[Apache License 2.0]

pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>io.github.axway-api-management-plus</groupId>
6+
<artifactId>ws-simple-client</artifactId>
7+
<version>0.1.0</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Simple WebSocket Client</name>
11+
<description>Simple WebSocket client sending and receiving text messages.</description>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
17+
<log4j.version>2.14.0</log4j.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>javax.websocket</groupId>
23+
<artifactId>javax.websocket-api</artifactId>
24+
<version>1.1</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.glassfish.tyrus.bundles</groupId>
28+
<artifactId>tyrus-standalone-client-jdk</artifactId>
29+
<version>1.17</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.beust</groupId>
33+
<artifactId>jcommander</artifactId>
34+
<version>1.78</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.apache.logging.log4j</groupId>
38+
<artifactId>log4j-api</artifactId>
39+
<version>${log4j.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.apache.logging.log4j</groupId>
43+
<artifactId>log4j-core</artifactId>
44+
<version>${log4j.version}</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<artifactId>maven-assembly-plugin</artifactId>
52+
<version>3.3.0</version>
53+
<configuration>
54+
<archive>
55+
<manifest>
56+
<mainClass>io.github.axway.apimplus.websocket.client.WebSocketClient</mainClass>
57+
</manifest>
58+
</archive>
59+
<descriptorRefs>
60+
<descriptorRef>jar-with-dependencies</descriptorRef>
61+
</descriptorRefs>
62+
</configuration>
63+
<executions>
64+
<execution>
65+
<id>dist</id>
66+
<phase>package</phase>
67+
<goals>
68+
<goal>single</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.github.axway.apimplus.websocket.client;
2+
3+
import com.beust.jcommander.Parameter;
4+
5+
class Config {
6+
@Parameter(names = { "-h", "--help" }, description = "Show help screen.", help = true, order = 1)
7+
private boolean help = false;
8+
9+
@Parameter(names = "--url", description = "Target WebSocket endpoint", order=2)
10+
private String url = "ws://echo.websocket.org";
11+
12+
@Parameter(names = "--endpoints", description = "Number of parallel client endpoints", order=3)
13+
private int endpointCount = 1;
14+
15+
@Parameter(names = "--messages", description = "Number of messages to be send to the server", order=4)
16+
private int messageCount = 1;
17+
18+
@Parameter(names = "--delay", description = "Delay between sending messages (milliseconds)", order=5)
19+
private long delayMillis = 1000;
20+
21+
22+
public String getUrl() {
23+
return url;
24+
}
25+
26+
public int getEndpointCount() {
27+
return endpointCount;
28+
}
29+
30+
public int getMessageCount() {
31+
return messageCount;
32+
}
33+
34+
public long getDelayMillis() {
35+
return delayMillis;
36+
}
37+
38+
public boolean isHelp() {
39+
return help;
40+
}
41+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.github.axway.apimplus.websocket.client;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
import javax.websocket.ClientEndpoint;
8+
import javax.websocket.DeploymentException;
9+
import javax.websocket.OnClose;
10+
import javax.websocket.OnMessage;
11+
import javax.websocket.OnOpen;
12+
import javax.websocket.Session;
13+
import javax.websocket.WebSocketContainer;
14+
15+
import org.apache.logging.log4j.LogManager;
16+
import org.apache.logging.log4j.Logger;
17+
18+
@ClientEndpoint
19+
public class Endpoint implements AutoCloseable {
20+
21+
private static Logger log = LogManager.getLogger(Endpoint.class);
22+
23+
private static AtomicInteger count = new AtomicInteger(0);
24+
25+
private final int id;
26+
private final Session session;
27+
28+
private int msgCountReceived = 0;
29+
private int msgCountSent = 0;
30+
31+
public Endpoint(WebSocketContainer container, URI uri) throws DeploymentException, IOException {
32+
this.id = Endpoint.count.incrementAndGet();
33+
this.session = container.connectToServer(this, uri);
34+
}
35+
36+
public int getId() {
37+
return this.id;
38+
}
39+
40+
public int getReceivedMessageCount() {
41+
return this.msgCountReceived;
42+
}
43+
44+
public int getSentMessageCount() {
45+
return this.msgCountSent;
46+
}
47+
48+
public int getMissingMessageCount() {
49+
return this.msgCountSent - this.msgCountReceived;
50+
}
51+
52+
public void sendText(String text) throws IOException {
53+
log("send - " + text);
54+
this.session.getBasicRemote().sendText(text);
55+
this.msgCountSent++;
56+
}
57+
58+
public boolean isOpen() {
59+
return this.session != null && this.session.isOpen();
60+
}
61+
62+
@Override
63+
public void close() throws IOException {
64+
if (this.session != null && this.session.isOpen()) {
65+
this.session.close();
66+
}
67+
}
68+
69+
@OnMessage
70+
public void onMessage(String message, Session session) {
71+
this.msgCountReceived++;
72+
log("received - " + message);
73+
}
74+
75+
@OnOpen
76+
public void onConnection(Session session) throws IOException {
77+
log("connected");
78+
}
79+
80+
@OnClose
81+
public void onClose() {
82+
log("disconnected - " + this.msgCountReceived + " message(s) received");
83+
}
84+
85+
private void log(String message) {
86+
log.info("[" + this.id + "]: " + message);
87+
}
88+
}

0 commit comments

Comments
 (0)