Provide access to Netty's WebSocketFrame

Closes gh-25099
This commit is contained in:
Rossen Stoyanchev 2020-07-09 15:42:28 +03:00
parent 37366e0c91
commit 313a7836b0
2 changed files with 33 additions and 3 deletions

View File

@ -39,6 +39,9 @@ public class WebSocketMessage {
private final DataBuffer payload; private final DataBuffer payload;
@Nullable
private final Object nativeMessage;
/** /**
* Constructor for a WebSocketMessage. * Constructor for a WebSocketMessage.
@ -47,13 +50,25 @@ public class WebSocketMessage {
* then invoke this constructor. * then invoke this constructor.
*/ */
public WebSocketMessage(Type type, DataBuffer payload) { 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(type, "'type' must not be null");
Assert.notNull(payload, "'payload' must not be null"); Assert.notNull(payload, "'payload' must not be null");
this.type = type; this.type = type;
this.payload = payload; this.payload = payload;
this.nativeMessage = nativeMessage;
} }
/** /**
* Return the message type (text, binary, etc). * Return the message type (text, binary, etc).
*/ */
@ -68,6 +83,21 @@ public class WebSocketMessage {
return this.payload; 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} * A variant of {@link #getPayloadAsText(Charset)} that uses {@code UTF-8}
* for decoding the raw content to text. * for decoding the raw content to text.

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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
protected WebSocketMessage toMessage(WebSocketFrame frame) { protected WebSocketMessage toMessage(WebSocketFrame frame) {
DataBuffer payload = bufferFactory().wrap(frame.content()); 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) { protected WebSocketFrame toFrame(WebSocketMessage message) {