wip: test ws
This commit is contained in:
parent
bbc8c470c5
commit
bd757fc731
5
pom.xml
5
pom.xml
@ -104,6 +104,11 @@
|
|||||||
<artifactId>jodd-util</artifactId>
|
<artifactId>jodd-util</artifactId>
|
||||||
<version>6.2.1</version>
|
<version>6.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.quarkus</groupId>
|
||||||
|
<artifactId>quarkus-websockets</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
84
src/main/java/fr/titionfire/ffsaf/ws/FileSocket.java
Normal file
84
src/main/java/fr/titionfire/ffsaf/ws/FileSocket.java
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package fr.titionfire.ffsaf.ws;
|
||||||
|
|
||||||
|
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.websocket.*;
|
||||||
|
import jakarta.websocket.server.PathParam;
|
||||||
|
import jakarta.websocket.server.ServerEndpoint;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
@ServerEndpoint("/api/ws/file/{id}")
|
||||||
|
@ApplicationScoped
|
||||||
|
public class FileSocket {
|
||||||
|
Map<String, FileRecv> sessions = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session, @PathParam("id") String id) {
|
||||||
|
try {
|
||||||
|
File file = File.createTempFile("safca-", ".tmp");
|
||||||
|
FileRecv fileRecv = new FileRecv(file, new FileOutputStream(file, true), System.currentTimeMillis(),
|
||||||
|
session);
|
||||||
|
System.out.println("File created: " + file.getAbsolutePath());
|
||||||
|
sessions.put(id, fileRecv);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClose
|
||||||
|
public void onClose(Session session, @PathParam("id") String id) {
|
||||||
|
if (sessions.containsKey(id)) {
|
||||||
|
FileRecv fileRecv = sessions.get(id);
|
||||||
|
if (fileRecv.fos != null) {
|
||||||
|
try {
|
||||||
|
fileRecv.fos.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sessions.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnError
|
||||||
|
public void onError(Session session, @PathParam("id") String id, Throwable throwable) {
|
||||||
|
if (sessions.containsKey(id)) {
|
||||||
|
FileRecv fileRecv = sessions.get(id);
|
||||||
|
if (fileRecv.fos != null) {
|
||||||
|
try {
|
||||||
|
fileRecv.fos.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sessions.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message, @PathParam("id") String id) {
|
||||||
|
if (sessions.containsKey(id)) {
|
||||||
|
FileRecv fileRecv = sessions.get(id);
|
||||||
|
try {
|
||||||
|
fileRecv.fos.write(message.getBytes());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@RegisterForReflection
|
||||||
|
class FileRecv {
|
||||||
|
File file;
|
||||||
|
FileOutputStream fos;
|
||||||
|
long time;
|
||||||
|
Session session;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user