feat: Started basic implementation of socket comm

This commit is contained in:
2026-01-31 17:02:24 +01:00
parent bfcce054d5
commit ef14edffde
23 changed files with 394 additions and 27 deletions

View File

@@ -18,5 +18,49 @@
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.42</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-api</artifactId>
<version>2.3.0-M2</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-server</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-container-grizzly-server</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-client</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.17</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.25</version>
</dependency>
</dependencies>
</project>

View File

@@ -1,7 +1,18 @@
package cz.jzitnik.server;
public class Main {
static void main() {
import jakarta.websocket.DeploymentException;
import org.glassfish.tyrus.server.Server;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
static void main() throws DeploymentException {
Map<String, Object> properties = new HashMap<>();
Server server = new Server("localhost", 8025, "/", properties, WebSocket.class);
server.start();
new Scanner(System.in).nextLine();
}
}

View File

@@ -0,0 +1,53 @@
package cz.jzitnik.server;
import cz.jzitnik.common.socket.SocketMessage;
import cz.jzitnik.common.socket.messages.Test;
import jakarta.websocket.*;
import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
@Slf4j
@ServerEndpoint("/ws")
public class WebSocket {
@OnOpen
public void onOpen(Session session) {
log.debug("Client connected: " + session.getId());
try {
SocketMessage response = new Test();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(response);
oos.flush();
session.getBasicRemote().sendBinary(java.nio.ByteBuffer.wrap(baos.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
}
// Receive binary data from client
@OnMessage
public void onMessage(byte[] bytes, Session session) {
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais)) {
SocketMessage socketMessage = (SocketMessage) ois.readObject();
System.out.println("Received: " + socketMessage);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@OnClose
public void onClose(Session session, CloseReason reason) {
System.out.println("Connection closed: " + reason);
}
@OnError
public void onError(Session session, Throwable throwable) {
throwable.printStackTrace();
}
}