Skip to content

Commit

Permalink
Introduce websockets codestart
Browse files Browse the repository at this point in the history
(cherry picked from commit 2d55d09)
  • Loading branch information
ia3andy authored and geoand committed Sep 28, 2021
1 parent 5d9c68d commit 3da201a
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 248 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: websockets-codestart
ref: websockets
type: code
tags: extension-codestart
metadata:
title: WebSockets
description: WebSocket communication channel starter code
related-guide-section: https://quarkus.io/guides/websockets
language:
base:
dependencies:
- io.quarkus:quarkus-websockets
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ilove.quark.us;

import javax.enterprise.context.ApplicationScoped;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

import static java.util.Objects.requireNonNull;

@ServerEndpoint("/start-websocket/{name}")
@ApplicationScoped
public class StartWebSocket {

@OnOpen
public void onOpen(Session session, @PathParam("name") String name) {
System.out.println("onOpen> " + name);
}

@OnClose
public void onClose(Session session, @PathParam("name") String name) {
System.out.println("onClose> " + name);
}

@OnError
public void onError(Session session, @PathParam("name") String name, Throwable throwable) {
System.out.println("onError> " + name + ": " + throwable);
}

@OnMessage
public void onMessage(String message, @PathParam("username") String name) {
System.out.println("onMessage> " + name + ": " + message);
}

/**
* Send a message to a remote websocket session
*
* @param session the websocket session
* @param message the message to send
* @throws IOException if there is a communication error sending the message object.
* @throws EncodeException if there was a problem encoding the message.
* @throws IllegalArgumentException if the message parameter is {@code null}
*/
public void sendMessage(Session session, String message) throws EncodeException, IOException {
requireNonNull(session, "session is required");

session.getBasicRemote().sendObject(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ilove.quark.us

import java.io.IOException
import javax.enterprise.context.ApplicationScoped
import javax.websocket.*
import javax.websocket.server.PathParam
import javax.websocket.server.ServerEndpoint

@ServerEndpoint("/start-websocket/{name}")
@ApplicationScoped
class StartWebSocket {

@OnOpen
fun onOpen(session: Session?, @PathParam("name") name: String) {
println("onOpen> $name")
}

@OnClose
fun onClose(session: Session?, @PathParam("name") name: String) {
println("onClose> $name")
}

@OnError
fun onError(session: Session?, @PathParam("name") name: String, throwable: Throwable) {
println("onError> $name: $throwable")
}

@OnMessage
fun onMessage(message: String, @PathParam("username") name: String) {
println("onMessage> $name: $message")
}

/**
* Send a message to a remote websocket session
*
* @param session the websocket session
* @param message the message to send
* @throws IOException if there is a communication error sending the message object.
* @throws EncodeException if there was a problem encoding the message.
*/
@Throws(EncodeException::class, IOException::class)
fun sendMessage(session: Session, message: String) {
session.basicRemote.sendObject(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ metadata:
- "web"
status: "stable"
codestart:
name: "undertow-websockets"
kind: "example"
languages: "java"
name: "websockets"
languages:
- "java"
- "kotlin"
artifact: "io.quarkus:quarkus-project-core-extension-codestarts"
config:
- "quarkus.websocket."
Loading

0 comments on commit 3da201a

Please sign in to comment.