-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic websocket codecs reflection registration
- Loading branch information
1 parent
652c0a2
commit e541ba9
Showing
8 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
integration-tests/websockets/src/main/java/io/quarkus/websockets/ChatCodecServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkus.websockets; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.UUID; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.websocket.OnClose; | ||
import jakarta.websocket.OnError; | ||
import jakarta.websocket.OnMessage; | ||
import jakarta.websocket.OnOpen; | ||
import jakarta.websocket.Session; | ||
import jakarta.websocket.server.ServerEndpoint; | ||
|
||
@ServerEndpoint(value = "/codec", encoders = ChatMessageEncoder.class, decoders = ChatMessageDecoder.class) | ||
@ApplicationScoped | ||
public class ChatCodecServer { | ||
|
||
Collection<Session> sessions = new ArrayList<>(); | ||
|
||
@OnOpen | ||
public void onOpen(Session session) { | ||
sessions.add(session); | ||
} | ||
|
||
@OnClose | ||
public void onClose(Session session) { | ||
sessions.remove(session); | ||
broadcast("Session " + session.getId() + " closed"); | ||
} | ||
|
||
@OnError | ||
public void onError(Session session, Throwable throwable) { | ||
sessions.remove(session); | ||
broadcast("Session " + session.getId() + " closed on error: " + throwable); | ||
} | ||
|
||
@OnMessage | ||
public void onMessage(ChatMessageDTO message) { | ||
broadcast(String.format("%s in message [%s] said: %s", message.getFrom(), message.getId(), message.getContent())); | ||
} | ||
|
||
private void broadcast(String message) { | ||
sessions.forEach(s -> { | ||
ChatMessageDTO chatMessageDTO = new ChatMessageDTO(); | ||
chatMessageDTO.setId(UUID.randomUUID().toString()); | ||
chatMessageDTO.setFrom("SuperCoolWebsocket"); | ||
chatMessageDTO.setContent(message); | ||
s.getAsyncRemote().sendObject(chatMessageDTO, result -> { | ||
if (result.getException() != null) { | ||
System.out.println("Unable to send message: " + result.getException()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
integration-tests/websockets/src/main/java/io/quarkus/websockets/ChatMessageDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.websockets; | ||
|
||
public class ChatMessageDTO { | ||
|
||
private String id; | ||
|
||
private String from; | ||
|
||
private String content; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getFrom() { | ||
return from; | ||
} | ||
|
||
public void setFrom(String from) { | ||
this.from = from; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
integration-tests/websockets/src/main/java/io/quarkus/websockets/ChatMessageDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.quarkus.websockets; | ||
|
||
import jakarta.json.bind.Jsonb; | ||
import jakarta.json.bind.JsonbBuilder; | ||
import jakarta.websocket.DecodeException; | ||
import jakarta.websocket.Decoder; | ||
|
||
public class ChatMessageDecoder implements Decoder.Text<ChatMessageDTO> { | ||
|
||
private final Jsonb jsonb = JsonbBuilder.create(); | ||
|
||
@Override | ||
public ChatMessageDTO decode(String string) throws DecodeException { | ||
return jsonb.fromJson( | ||
string, | ||
ChatMessageDTO.class); | ||
} | ||
|
||
@Override | ||
public boolean willDecode(String string) { | ||
return Boolean.TRUE; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
integration-tests/websockets/src/main/java/io/quarkus/websockets/ChatMessageEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.websockets; | ||
|
||
import jakarta.json.bind.Jsonb; | ||
import jakarta.json.bind.JsonbBuilder; | ||
import jakarta.websocket.EncodeException; | ||
import jakarta.websocket.Encoder; | ||
|
||
public class ChatMessageEncoder implements Encoder.Text<ChatMessageDTO> { | ||
|
||
private final Jsonb jsonb = JsonbBuilder.create(); | ||
|
||
@Override | ||
public String encode(ChatMessageDTO t) throws EncodeException { | ||
return jsonb.toJson(t); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
integration-tests/websockets/src/test/java/io/quarkus/websockets/ChatCodecIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkus.websockets; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
public class ChatCodecIT extends ChatCodecTest { | ||
} |
51 changes: 51 additions & 0 deletions
51
integration-tests/websockets/src/test/java/io/quarkus/websockets/ChatCodecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.quarkus.websockets; | ||
|
||
import java.net.URI; | ||
import java.util.UUID; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.json.bind.JsonbBuilder; | ||
import jakarta.websocket.ClientEndpoint; | ||
import jakarta.websocket.ContainerProvider; | ||
import jakarta.websocket.OnMessage; | ||
import jakarta.websocket.Session; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class ChatCodecTest { | ||
|
||
private static final LinkedBlockingDeque<ChatMessageDTO> MESSAGES = new LinkedBlockingDeque<>(); | ||
|
||
@TestHTTPResource("/codec") | ||
URI uri; | ||
|
||
@Test | ||
public void testWebsocketChat() throws Exception { | ||
try (Session session = ContainerProvider.getWebSocketContainer().connectToServer(Client.class, uri)) { | ||
ChatMessageDTO chatMessageDTO = new ChatMessageDTO(); | ||
chatMessageDTO.setId(UUID.randomUUID().toString()); | ||
chatMessageDTO.setFrom("SuperCoolProgrammer"); | ||
chatMessageDTO.setContent("Hello my young padawan!"); | ||
session.getAsyncRemote().sendText(JsonbBuilder.create().toJson(chatMessageDTO)); | ||
Assertions.assertEquals(String.format("%s in message [%s] said: %s", chatMessageDTO.getFrom(), | ||
chatMessageDTO.getId(), chatMessageDTO.getContent()), MESSAGES.poll(10, TimeUnit.SECONDS).getContent()); | ||
} | ||
} | ||
|
||
@ClientEndpoint(encoders = ChatMessageEncoder.class, decoders = ChatMessageDecoder.class) | ||
public static class Client { | ||
|
||
@OnMessage | ||
void message(ChatMessageDTO msg) { | ||
MESSAGES.add(msg); | ||
} | ||
|
||
} | ||
|
||
} |