Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b0992e208 | ||
|
|
2b64b10646 | ||
|
|
5cb57127de |
@@ -0,0 +1,34 @@
|
||||
name: Gradle Build
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*' # Run when a version tag like v0.1.0 is pushed
|
||||
pull_request: # Optional: test PRs too
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
# 1. Checkout code
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
# 2. Set up JDK 17
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '17'
|
||||
distribution: 'temurin'
|
||||
|
||||
# 3. Set up Gradle
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
|
||||
# 4. Build project
|
||||
- name: Build with Gradle
|
||||
run: ./gradlew build
|
||||
|
||||
# 5. Run tests
|
||||
- name: Run tests
|
||||
run: ./gradlew test
|
||||
@@ -1,44 +0,0 @@
|
||||
# This workflow uses actions that are not certified by GitHub.
|
||||
# They are provided by a third-party and are governed by
|
||||
# separate terms of service, privacy policy, and support
|
||||
# documentation.
|
||||
# This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created
|
||||
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-gradle
|
||||
|
||||
name: Gradle Package
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [created]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '17'
|
||||
distribution: 'temurin'
|
||||
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
|
||||
settings-path: ${{ github.workspace }} # location for the settings.xml file
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
|
||||
|
||||
- name: Build with Gradle
|
||||
run: ./gradlew build
|
||||
|
||||
# The USERNAME and TOKEN need to correspond to the credentials environment variables used in
|
||||
# the publishing section of your build.gradle
|
||||
- name: Publish to GitHub Packages
|
||||
run: ./gradlew publish
|
||||
env:
|
||||
USERNAME: ${{ github.actor }}
|
||||
TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -26,22 +26,14 @@ It ensures compile-time guarantees for message structure, reduces runtime errors
|
||||
|
||||
```groovy
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
maven {
|
||||
url = uri("https://maven.pkg.github.com/selimaj-dev/session-java")
|
||||
credentials {
|
||||
username = project.findProperty("gpr.user") ?: System.getenv("GITHUB_USERNAME")
|
||||
password = project.findProperty("gpr.key") ?: System.getenv("GITHUB_TOKEN")
|
||||
}
|
||||
}
|
||||
maven { url 'https://jitpack.io' }
|
||||
}
|
||||
```
|
||||
|
||||
#### Add the dependency
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation "dev.selimaj.session:session-java:0.1.0"
|
||||
implementation 'com.github.selimaj-dev:session-java:v0.1.3'
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
+1
-22
@@ -4,7 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "dev.selimaj.session"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
@@ -34,24 +34,3 @@ dependencies {
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
gpr(MavenPublication) {
|
||||
from(components.java)
|
||||
groupId = project.group
|
||||
artifactId = "session-java"
|
||||
version = project.version
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name = "GitHubPackages"
|
||||
url = uri("https://maven.pkg.github.com/selimaj-dev/session-java")
|
||||
credentials {
|
||||
username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
|
||||
password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.WebSocket;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public final class Session {
|
||||
|
||||
@@ -34,6 +35,18 @@ public final class Session {
|
||||
return new Session(ws, listener);
|
||||
}
|
||||
|
||||
public static Session connect(String uri, long timeout, TimeUnit unit) throws Exception {
|
||||
SessionListener listener = new SessionListener();
|
||||
|
||||
WebSocket ws = HttpClient.newHttpClient()
|
||||
.newWebSocketBuilder()
|
||||
.buildAsync(URI.create(uri), listener)
|
||||
.orTimeout(timeout, unit)
|
||||
.join();
|
||||
|
||||
return new Session(ws, listener);
|
||||
}
|
||||
|
||||
public void send(Message msg) throws Exception {
|
||||
listener.send(ws, msg);
|
||||
}
|
||||
@@ -50,12 +63,12 @@ public final class Session {
|
||||
listener.notify(ws, method, data);
|
||||
}
|
||||
|
||||
<Req extends JsonNode, Res extends JsonNode, Err extends JsonNode> CompletableFuture<Res> request(
|
||||
<Req, Res, Err> CompletableFuture<Res> request(
|
||||
Method<Req, Res, Err> method, Req req) throws Exception {
|
||||
return listener.request(ws, method.getName(), req, method.getResClass());
|
||||
}
|
||||
|
||||
<Req extends JsonNode, Res extends JsonNode, Err extends JsonNode> void onRequest(Method<Req, Res, Err> method,
|
||||
<Req, Res, Err> void onRequest(Method<Req, Res, Err> method,
|
||||
MethodHandler<Req> handler) {
|
||||
MethodHandler<JsonNode> wrapper = (id, value) -> {
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.selimaj.session;
|
||||
|
||||
import java.net.http.WebSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -13,7 +14,7 @@ import dev.selimaj.session.types.Message;
|
||||
import dev.selimaj.session.types.MethodHandler;
|
||||
|
||||
public class SessionListener implements WebSocket.Listener {
|
||||
public final ConcurrentHashMap<String, MethodHandler<JsonNode>> methods = new ConcurrentHashMap<>();
|
||||
final ConcurrentHashMap<String, MethodHandler<JsonNode>> methods = new ConcurrentHashMap<>();
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
private final ConcurrentHashMap<Integer, CompletableFuture<JsonNode>> pending = new ConcurrentHashMap<>();
|
||||
@@ -60,6 +61,11 @@ public class SessionListener implements WebSocket.Listener {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onPing(WebSocket ws, ByteBuffer message) {
|
||||
return ws.sendPong(message);
|
||||
}
|
||||
|
||||
void send(WebSocket ws, Message msg) throws Exception {
|
||||
ws.sendText(mapper.writeValueAsString(msg), true);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package dev.selimaj.session.types;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class Method<Req extends JsonNode, Res extends JsonNode, Err extends JsonNode> {
|
||||
public class Method<Req, Res, Err> {
|
||||
private final String name;
|
||||
private final Class<Req> reqClass;
|
||||
private final Class<Res> resClass;
|
||||
|
||||
@@ -2,9 +2,7 @@ package dev.selimaj.session.types;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MethodHandler<Req extends JsonNode> {
|
||||
public interface MethodHandler<Req> {
|
||||
CompletableFuture<SessionResult> handle(int id, Req data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user