Search in sources :

Example 1 with OnMessage

use of io.micronaut.websocket.annotation.OnMessage in project micronaut-core by micronaut-projects.

the class BinaryChatServerWebSocket method onMessage.

@OnMessage
public void onMessage(String topic, String username, byte[] message, WebSocketSession session) {
    assert ServerRequestContext.currentRequest().isPresent();
    Set<? extends WebSocketSession> openSessions = session.getOpenSessions();
    System.out.println("Server received message = " + new String(message));
    System.out.println("Server openSessions = " + openSessions);
    for (WebSocketSession openSession : openSessions) {
        if (isValid(topic, session, openSession)) {
            String msg = "[" + username + "] " + new String(message);
            System.out.println("Server sending msg = " + msg);
            openSession.sendSync(msg.getBytes());
        }
    }
}
Also used : WebSocketSession(io.micronaut.websocket.WebSocketSession) OnMessage(io.micronaut.websocket.annotation.OnMessage)

Example 2 with OnMessage

use of io.micronaut.websocket.annotation.OnMessage in project micronaut-core by micronaut-projects.

the class ReactivePojoChatServerWebSocket method onClose.

// end::onmessage[]
@OnClose
public Publisher<Message> onClose(String topic, String username, WebSocketSession session) {
    String text = "[" + username + "] Disconnected!";
    Message message = new Message(text);
    assert ServerRequestContext.currentRequest().isPresent();
    return broadcaster.broadcast(message, isValid(topic, session));
}
Also used : OnMessage(io.micronaut.websocket.annotation.OnMessage) OnClose(io.micronaut.websocket.annotation.OnClose)

Example 3 with OnMessage

use of io.micronaut.websocket.annotation.OnMessage in project micronaut-core by micronaut-projects.

the class ReactivePojoChatServerWebSocket method onMessage.

// tag::onmessage[]
@OnMessage
public Publisher<Message> onMessage(String topic, String username, Message message, WebSocketSession session) {
    String text = "[" + username + "] " + message.getText();
    Message newMessage = new Message(text);
    assert ServerRequestContext.currentRequest().isPresent();
    return broadcaster.broadcast(newMessage, isValid(topic, session));
}
Also used : OnMessage(io.micronaut.websocket.annotation.OnMessage) OnMessage(io.micronaut.websocket.annotation.OnMessage)

Example 4 with OnMessage

use of io.micronaut.websocket.annotation.OnMessage in project micronaut-core by micronaut-projects.

the class ReactivePojoChatServerWebSocket method onClose.

// end::onmessage[]
@OnClose
public Publisher<Message> onClose(String topic, String username, WebSocketSession session) {
    String text = "[" + username + "] Disconnected!";
    Message message = new Message(text);
    return broadcaster.broadcast(message, isValid(topic, session));
}
Also used : OnMessage(io.micronaut.websocket.annotation.OnMessage) OnClose(io.micronaut.websocket.annotation.OnClose)

Example 5 with OnMessage

use of io.micronaut.websocket.annotation.OnMessage in project micronaut-core by micronaut-projects.

the class ReactivePojoChatServerWebSocket method onMessage.

// tag::onmessage[]
@OnMessage
public Publisher<Message> onMessage(String topic, String username, Message message, WebSocketSession session) {
    String text = "[" + username + "] " + message.getText();
    Message newMessage = new Message(text);
    return broadcaster.broadcast(newMessage, isValid(topic, session));
}
Also used : OnMessage(io.micronaut.websocket.annotation.OnMessage) OnMessage(io.micronaut.websocket.annotation.OnMessage)

Aggregations

OnMessage (io.micronaut.websocket.annotation.OnMessage)5 OnClose (io.micronaut.websocket.annotation.OnClose)2 WebSocketSession (io.micronaut.websocket.WebSocketSession)1