Skip to content

Commit

Permalink
WebSocket: An experimental websocket implementation!
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Nov 23, 2024
1 parent 3a6a8c2 commit ded5ec4
Show file tree
Hide file tree
Showing 8 changed files with 811 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Select a subproject to get started.
[Functional](/functional/) • Functional code helpers.
[IPC](/ipc/) • An in-progress IPC framework.
[IO](/io/) • Utilties for handling information.
[WebSocket](/websocket/) • An in-progress WebSocket client. Not recommended for production use as it doesn't strictly adhere to the spec.

## Repository

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<module>ipc</module>
<module>io</module>
<module>localization</module>
<module>websocket</module>
</modules>

<properties>
Expand Down
81 changes: 81 additions & 0 deletions websocket/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Casterlabs Commons/WebSocket

An experimental WebSocket implementation. Not recommended for production use as this currently doesn't strictly adhere to the spec.

## Examples

```java
WebSocketClient client = new WebSocketClient(URI.create("wss://echo.casterlabs.co"));
client.setListener(new WebSocketListener() {
@SneakyThrows
@Override
public void onOpen(WebSocketClient client, Map<String, String> headers, @Nullable String acceptedProtocol) {
System.out.println("Connected. Headers: " + headers);
}

@Override
public void onText(WebSocketClient client, String string) {
System.out.println("Got text message: '" + string + "'");
}

@Override
public void onBinary(WebSocketClient client, byte[] bytes) {
System.out.println("Got binary message: len=" + bytes.length);
}

@Override
public void onClosed(WebSocketClient client) {
System.out.println("Closed");
}
});
client.connect();

Scanner in = new Scanner(System.in);
while (true) {
client.send(in.nextLine());
}

// Type into the console to send messages to the server. It will echo them back :^)
```

## Adding to your project

Replace `VERSION_OR_HASH` with the latest version or commit in this repo and make sure to add the [Repository](https://github.com/Casterlabs/Commons#Repository) to your build system.

<details>
<summary>Maven</summary>

```xml
<dependency>
<groupId>co.casterlabs.commons</groupId>
<artifactId>websocket</artifactId>
<version>VERSION_OR_HASH</version>
</dependency>
```
</details>

<details>
<summary>Gradle</summary>

```gradle
dependencies {
implementation 'co.casterlabs.commons:websocket:VERSION_OR_HASH'
}
```
</details>

<details>
<summary>SBT</summary>

```
libraryDependencies += "co.casterlabs.commons" % "websocket" % "VERSION_OR_HASH"
```
</details>

<details>
<summary>Leiningen</summary>

```
:dependencies [[co.casterlabs.commons/websocket "VERSION_OR_HASH"]]
```
</details>
22 changes: 22 additions & 0 deletions websocket/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>websocket</artifactId>

<parent>
<groupId>co.casterlabs.commons</groupId>
<artifactId>commons</artifactId>
<version>PLACEHOLDER</version>
<relativePath>../pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>co.casterlabs.commons</groupId>
<artifactId>io</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit ded5ec4

Please sign in to comment.