io.inverno.mod.http.server.ws.WebSocketExchangeHandler Maven / Gradle / Ivy
Show all versions of inverno-http-server Show documentation
/*
* Copyright 2022 Jeremy KUHN
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.inverno.mod.http.server.ws;
import io.inverno.mod.http.base.ExchangeContext;
import io.inverno.mod.http.base.ws.WebSocketException;
import reactor.core.publisher.Mono;
/**
*
* A WebSocket exchange handler is used to handle WebSocket exchange.
*
*
*
* It allows to specify the behaviour of the server when a WebSocket connection has been established between a client and the server.
*
*
* @author Jeremy Kuhn
* @since 1.5
*
* @param the type of the exchange context
* @param the type of WebSocket exchange handled by the handler
*/
@FunctionalInterface
public interface WebSocketExchangeHandler> {
/**
*
* Returns a Mono that defers the processing of the WebSocket exchange.
*
*
*
* The server subscribes to the returned Mono and after completion, emits received messages in the inbound frames stream and subscribes to the exchange outbound frames stream to send WebSocket
* frames to the client.
*
*
*
* By default, returns a Mono that defers the execution of {@link #handle(io.inverno.mod.http.server.ws.WebSocketExchange) }.
*
*
* @param webSocketExchange the WebSocket exchange to process
*
* @return an empty mono that completes when the WebSocket exchange has been processed
*
* @throws WebSocketException if an error occurs during the processing of the exchange
*/
default Mono defer(B webSocketExchange) throws WebSocketException {
return Mono.fromRunnable(() -> this.handle(webSocketExchange));
}
/**
*
* Processes the specified WebSocket server exchange.
*
*
*
* This method is more convenient than {@link #defer(io.inverno.mod.http.server.ws.WebSocketExchange)} when the handling logic does not need to be reactive.
*
*
* @param webSocketExchange the WebSocket exchange to process
*
* @throws WebSocketException if an error occurs during the processing of the exchange
*/
void handle(B webSocketExchange) throws WebSocketException;
}