001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.socket.messaging; 018 019import java.util.List; 020 021import org.springframework.lang.Nullable; 022import org.springframework.messaging.Message; 023import org.springframework.messaging.MessageChannel; 024import org.springframework.web.socket.CloseStatus; 025import org.springframework.web.socket.WebSocketMessage; 026import org.springframework.web.socket.WebSocketSession; 027 028/** 029 * A contract for handling WebSocket messages as part of a higher level protocol, 030 * referred to as "sub-protocol" in the WebSocket RFC specification. 031 * Handles both {@link WebSocketMessage WebSocketMessages} from a client 032 * as well as {@link Message Messages} to a client. 033 * 034 * <p>Implementations of this interface can be configured on a 035 * {@link SubProtocolWebSocketHandler} which selects a sub-protocol handler to 036 * delegate messages to based on the sub-protocol requested by the client through 037 * the {@code Sec-WebSocket-Protocol} request header. 038 * 039 * @author Andy Wilkinson 040 * @author Rossen Stoyanchev 041 * @since 4.0 042 */ 043public interface SubProtocolHandler { 044 045 /** 046 * Return the list of sub-protocols supported by this handler (never {@code null}). 047 */ 048 List<String> getSupportedProtocols(); 049 050 /** 051 * Handle the given {@link WebSocketMessage} received from a client. 052 * @param session the client session 053 * @param message the client message 054 * @param outputChannel an output channel to send messages to 055 */ 056 void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> message, MessageChannel outputChannel) 057 throws Exception; 058 059 /** 060 * Handle the given {@link Message} to the client associated with the given WebSocket session. 061 * @param session the client session 062 * @param message the client message 063 */ 064 void handleMessageToClient(WebSocketSession session, Message<?> message) throws Exception; 065 066 /** 067 * Resolve the session id from the given message or return {@code null}. 068 * @param message the message to resolve the session id from 069 */ 070 @Nullable 071 String resolveSessionId(Message<?> message); 072 073 /** 074 * Invoked after a {@link WebSocketSession} has started. 075 * @param session the client session 076 * @param outputChannel a channel 077 */ 078 void afterSessionStarted(WebSocketSession session, MessageChannel outputChannel) throws Exception; 079 080 /** 081 * Invoked after a {@link WebSocketSession} has ended. 082 * @param session the client session 083 * @param closeStatus the reason why the session was closed 084 * @param outputChannel a channel 085 */ 086 void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) 087 throws Exception; 088 089}