From 519deeda315cd160fd282a9b9cda9f8ee44c32e8 Mon Sep 17 00:00:00 2001 From: SR-Lut3t1um <56112387+SR-Lut3t1um@users.noreply.github.com> Date: Sat, 15 Feb 2020 14:36:00 +0100 Subject: [PATCH] Start work on websocket --- README.md | 11 +- build.gradle | 1 + .../org/un1qu3/endpoints/AutomatonSocket.java | 64 ++++++++ .../resources/META-INF/resources/index.html | 154 +----------------- .../resources/META-INF/resources/main.css | 40 +++++ src/main/resources/META-INF/resources/main.js | 9 + src/main/resources/application.properties | 7 +- 7 files changed, 139 insertions(+), 147 deletions(-) create mode 100644 src/main/java/org/un1qu3/endpoints/AutomatonSocket.java create mode 100644 src/main/resources/META-INF/resources/main.css create mode 100644 src/main/resources/META-INF/resources/main.js diff --git a/README.md b/README.md index 13c4667..a82b1c5 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,13 @@ Or you can use Docker to build the native executable using: `./gradlew buildNati You can then execute your binary: `./build/machine-1.0.0-SNAPSHOT-runner` -If you want to learn more about building native executables, please consult https://quarkus.io/guides/gradle-tooling#building-a-native-executable . \ No newline at end of file +If you want to learn more about building native executables, please consult https://quarkus.io/guides/gradle-tooling#building-a-native-executable . + +## Websocket protocol: + +automaton/id/states (operation) [info] + +eg: + + automaton/id/states add q1 + automaton/id/name set "Cool Name" \ No newline at end of file diff --git a/build.gradle b/build.gradle index b497ad3..22f2794 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ dependencies { implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}") implementation 'io.quarkus:quarkus-resteasy' implementation 'io.quarkus:quarkus-resteasy-qute' + implementation 'io.quarkus:quarkus-jdbc-postgresql' testImplementation 'io.quarkus:quarkus-junit5' testImplementation 'io.rest-assured:rest-assured' diff --git a/src/main/java/org/un1qu3/endpoints/AutomatonSocket.java b/src/main/java/org/un1qu3/endpoints/AutomatonSocket.java new file mode 100644 index 0000000..ae6aff0 --- /dev/null +++ b/src/main/java/org/un1qu3/endpoints/AutomatonSocket.java @@ -0,0 +1,64 @@ +package org.un1qu3.endpoints; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import javax.enterprise.context.ApplicationScoped; +import javax.websocket.OnClose; +import javax.websocket.OnError; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +@ServerEndpoint("/websocket") +@ApplicationScoped +public class AutomatonSocket { + + Map sessions = new ConcurrentHashMap<>(); + + @OnOpen + public void onOpen(Session session) { + String username = "Hans"; + sessions.put(username, session); + broadcast("User " + username + " joined"); + } + + @OnClose + public void onClose(Session session) { + String username = "Hans"; + sessions.remove(username); + broadcast("User " + username + " left"); + } + + @OnError + public void onError(Session session, Throwable throwable) { + String username = "Hans"; + sessions.remove(username); + broadcast("User " + username + " left on error: " + throwable); + } + + @OnMessage + public void onMessage(String message) { + String username = "Hans"; + String[] msg = message.split(" "); + if (msg[0].startsWith("automaton")) { + if (msg[1].contains("add")) {} + } + } + + private void broadcast(String message) { + sessions + .values() + .forEach( + s -> { + s.getAsyncRemote() + .sendObject( + message, + result -> { + if (result.getException() != null) { + System.out.println("Unable to send message: " + result.getException()); + } + }); + }); + } +} diff --git a/src/main/resources/META-INF/resources/index.html b/src/main/resources/META-INF/resources/index.html index 889f22f..785e174 100644 --- a/src/main/resources/META-INF/resources/index.html +++ b/src/main/resources/META-INF/resources/index.html @@ -1,152 +1,16 @@ - - machine - 1.0.0-SNAPSHOT - + + Aut0m4t0n // Alpha + - - - -
-
-

Congratulations, you have created a new Quarkus application.

- -

Why do you see this?

- -

This page is served by Quarkus. The source is in - src/main/resources/META-INF/resources/index.html.

- -

What can I do from here?

- -

If not already done, run the application in dev mode using: mvn compile quarkus:dev. -

-
    -
  • Add REST resources, Servlets, functions and other services in src/main/java.
  • -
  • Your static assets are located in src/main/resources/META-INF/resources.
  • -
  • Configure your application in src/main/resources/application.properties. -
  • -
- -

How do I get rid of this page?

-

Just delete the src/main/resources/META-INF/resources/index.html file.

-
-
-
-

Application

-
    -
  • GroupId: org.un1qu3
  • -
  • ArtifactId: machine
  • -
  • Version: 1.0.0-SNAPSHOT
  • -
  • Quarkus Version: 1.2.0.Final
  • -
-
- -
-
- - + + + \ No newline at end of file diff --git a/src/main/resources/META-INF/resources/main.css b/src/main/resources/META-INF/resources/main.css new file mode 100644 index 0000000..234e7ff --- /dev/null +++ b/src/main/resources/META-INF/resources/main.css @@ -0,0 +1,40 @@ +* { + margin: 0; + padding: 0; +} + +@media (prefers-color-scheme: dark) { + :root { + --main-bg-color: black; + --main-ac-color: #212121; + --main-color: whitesmoke; + --toolbar-height: 2vh; + } +} + +ul { + list-style-type: none; + overflow: hidden; + background-color: var(--main-bg-color); +} + +li { + float: left; +} + +li a { + display: block; + color: var(--main-color); + text-align: center; + padding: var(--toolbar-height); + text-decoration: none; +} + +li a:hover { + background-color: var(--main-ac-color); +} + +canvas { + width: 100%; + height: 80%; +} \ No newline at end of file diff --git a/src/main/resources/META-INF/resources/main.js b/src/main/resources/META-INF/resources/main.js new file mode 100644 index 0000000..14164b1 --- /dev/null +++ b/src/main/resources/META-INF/resources/main.js @@ -0,0 +1,9 @@ +let canvas = document.getElementsByTagName('canvas')[0]; +canvas.height = 80; +canvas.width = canvas.style.width; + +let webSocket = new WebSocket( + 'ws://' + window.location.host + "/websocket"); +webSocket.onopen = function (event) { + console.log("Socket connected") +}; \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3c1ac56..e41bf5e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,7 @@ # Configuration file -# key = value \ No newline at end of file +quarkus.datasource.url=jdbc:postgresql://localhost:5432/hibernate_db +quarkus.datasource.driver=org.postgresql.Driver +quarkus.datasource.username=root +quarkus.datasource.password=test123 +# drop and create the database at startup (use `update` to only update the schema) +quarkus.hibernate-orm.database.generation=update \ No newline at end of file -- GitLab