Browse Source

Provide access to Netty's WebSocketFrame

Closes gh-25099
pull/25381/head
Rossen Stoyanchev 4 years ago
parent
commit
313a7836b0
  1. 32
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java
  2. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/NettyWebSocketSessionSupport.java

32
spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java

@ -39,6 +39,9 @@ public class WebSocketMessage { @@ -39,6 +39,9 @@ public class WebSocketMessage {
private final DataBuffer payload;
@Nullable
private final Object nativeMessage;
/**
* Constructor for a WebSocketMessage.
@ -47,13 +50,25 @@ public class WebSocketMessage { @@ -47,13 +50,25 @@ public class WebSocketMessage {
* then invoke this constructor.
*/
public WebSocketMessage(Type type, DataBuffer payload) {
this(type, payload, null);
}
/**
* Constructor for an inbound message with access to the underlying message.
* @param type the type of WebSocket message
* @param payload the message content
* @param nativeMessage the message from the API of the underlying WebSocket
* library, if applicable.
* @since 5.3
*/
public WebSocketMessage(Type type, DataBuffer payload, @Nullable Object nativeMessage) {
Assert.notNull(type, "'type' must not be null");
Assert.notNull(payload, "'payload' must not be null");
this.type = type;
this.payload = payload;
this.nativeMessage = nativeMessage;
}
/**
* Return the message type (text, binary, etc).
*/
@ -68,6 +83,21 @@ public class WebSocketMessage { @@ -68,6 +83,21 @@ public class WebSocketMessage {
return this.payload;
}
/**
* Return the message from the API of the underlying WebSocket library. This
* is applicable for inbound messages only and when the underlying message
* has additional fields other than the content. Currently this is the case
* for Reactor Netty only.
* @param <T> the type to cast the underlying message to
* @return the underlying message, or {@code null}
* @since 5.3
*/
@Nullable
@SuppressWarnings("unchecked")
public <T> T getNativeMessage() {
return (T) this.nativeMessage;
}
/**
* A variant of {@link #getPayloadAsText(Charset)} that uses {@code UTF-8}
* for decoding the raw content to text.

4
spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/NettyWebSocketSessionSupport.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -74,7 +74,7 @@ public abstract class NettyWebSocketSessionSupport<T> extends AbstractWebSocketS @@ -74,7 +74,7 @@ public abstract class NettyWebSocketSessionSupport<T> extends AbstractWebSocketS
protected WebSocketMessage toMessage(WebSocketFrame frame) {
DataBuffer payload = bufferFactory().wrap(frame.content());
return new WebSocketMessage(messageTypes.get(frame.getClass()), payload);
return new WebSocketMessage(messageTypes.get(frame.getClass()), payload, frame);
}
protected WebSocketFrame toFrame(WebSocketMessage message) {

Loading…
Cancel
Save