|
package ai.giskard.learnspringwebsockets; |
|
|
|
import org.springframework.context.annotation.Bean; |
|
import org.springframework.context.annotation.Configuration; |
|
import org.springframework.web.socket.CloseStatus; |
|
import org.springframework.web.socket.WebSocketHandler; |
|
import org.springframework.web.socket.WebSocketMessage; |
|
import org.springframework.web.socket.WebSocketSession; |
|
import org.springframework.web.socket.config.annotation.EnableWebSocket; |
|
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; |
|
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; |
|
|
|
@Configuration |
|
@EnableWebSocket |
|
public class WebSocketConfig implements WebSocketConfigurer { |
|
|
|
@Override |
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
|
registry.addHandler(myHandler(), "/ws"); |
|
} |
|
|
|
@Bean |
|
public WebSocketHandler myHandler() { |
|
return new WebSocketHandler() { |
|
@Override |
|
public void afterConnectionEstablished(WebSocketSession session) throws Exception { |
|
System.out.println("afterConnectionEstablished"); |
|
} |
|
|
|
@Override |
|
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { |
|
System.out.println("handleMessage"); |
|
} |
|
|
|
@Override |
|
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { |
|
System.out.println("handleTransportError"); |
|
} |
|
|
|
@Override |
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { |
|
System.out.println("afterConnectionClosed"); |
|
} |
|
|
|
@Override |
|
public boolean supportsPartialMessages() { |
|
return false; |
|
} |
|
}; |
|
} |
|
|
|
} |