Class WebSocketFrame
- java.lang.Object
-
- com.neovisionaries.ws.client.WebSocketFrame
-
public class WebSocketFrame extends java.lang.ObjectWebSocket frame.- See Also:
- RFC 6455, 5. Data Framing
-
-
Constructor Summary
Constructors Constructor Description WebSocketFrame()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidappendPayloadBinary(java.lang.StringBuilder builder)private voidappendPayloadClose(java.lang.StringBuilder builder)private booleanappendPayloadCommon(java.lang.StringBuilder builder)private voidappendPayloadText(java.lang.StringBuilder builder)private static byte[]compress(byte[] data, PerMessageCompressionExtension pmce)(package private) static WebSocketFramecompressFrame(WebSocketFrame frame, PerMessageCompressionExtension pmce)static WebSocketFramecreateBinaryFrame(byte[] payload)Create a binary frame.static WebSocketFramecreateCloseFrame()Create a close frame.static WebSocketFramecreateCloseFrame(int closeCode)Create a close frame.static WebSocketFramecreateCloseFrame(int closeCode, java.lang.String reason)Create a close frame.static WebSocketFramecreateContinuationFrame()Create a continuation frame.static WebSocketFramecreateContinuationFrame(byte[] payload)Create a continuation frame.static WebSocketFramecreateContinuationFrame(java.lang.String payload)Create a continuation frame.static WebSocketFramecreatePingFrame()Create a ping frame.static WebSocketFramecreatePingFrame(byte[] payload)Create a ping frame.static WebSocketFramecreatePingFrame(java.lang.String payload)Create a ping frame.static WebSocketFramecreatePongFrame()Create a pong frame.static WebSocketFramecreatePongFrame(byte[] payload)Create a pong frame.static WebSocketFramecreatePongFrame(java.lang.String payload)Create a pong frame.static WebSocketFramecreateTextFrame(java.lang.String payload)Create a text frame.intgetCloseCode()Parse the first two bytes of the payload as a close code.java.lang.StringgetCloseReason()Parse the third and subsequent bytes of the payload as a close reason.booleangetFin()Get the value of FIN bit.(package private) booleangetMask()Get the value of MASK bit.intgetOpcode()Get the opcode.byte[]getPayload()Get the unmasked payload.intgetPayloadLength()Get the payload length.java.lang.StringgetPayloadText()Get the unmasked payload as a text.booleangetRsv1()Get the value of RSV1 bit.booleangetRsv2()Get the value of RSV2 bit.booleangetRsv3()Get the value of RSV3 bit.booleanhasPayload()Check if this frame has payload.booleanisBinaryFrame()Check if this frame is a binary frame.booleanisCloseFrame()Check if this frame is a close frame.booleanisContinuationFrame()Check if this frame is a continuation frame.booleanisControlFrame()Check if this frame is a control frame.booleanisDataFrame()Check if this frame is a data frame.booleanisPingFrame()Check if this frame is a ping frame.booleanisPongFrame()Check if this frame is a pong frame.booleanisTextFrame()Check if this frame is a text frame.(package private) static byte[]mask(byte[] maskingKey, byte[] payload)Mask/unmask payload.WebSocketFramesetCloseFramePayload(int closeCode, java.lang.String reason)Set the payload that conforms to the payload format of close frames.WebSocketFramesetFin(boolean fin)Set the value of FIN bit.(package private) WebSocketFramesetMask(boolean mask)Set the value of MASK bit.WebSocketFramesetOpcode(int opcode)Set the opcodeWebSocketFramesetPayload(byte[] payload)Set the unmasked payload.WebSocketFramesetPayload(java.lang.String payload)Set the payload.WebSocketFramesetRsv1(boolean rsv1)Set the value of RSV1 bit.WebSocketFramesetRsv2(boolean rsv2)Set the value of RSV2 bit.WebSocketFramesetRsv3(boolean rsv3)Set the value of RSV3 bit.private static java.util.List<WebSocketFrame>split(WebSocketFrame frame, int maxPayloadSize)(package private) static java.util.List<WebSocketFrame>splitIfNecessary(WebSocketFrame frame, int maxPayloadSize, PerMessageCompressionExtension pmce)java.lang.StringtoString()
-
-
-
Method Detail
-
getFin
public boolean getFin()
Get the value of FIN bit.- Returns:
- The value of FIN bit.
-
setFin
public WebSocketFrame setFin(boolean fin)
Set the value of FIN bit.- Parameters:
fin- The value of FIN bit.- Returns:
thisobject.
-
getRsv1
public boolean getRsv1()
Get the value of RSV1 bit.- Returns:
- The value of RSV1 bit.
-
setRsv1
public WebSocketFrame setRsv1(boolean rsv1)
Set the value of RSV1 bit.- Parameters:
rsv1- The value of RSV1 bit.- Returns:
thisobject.
-
getRsv2
public boolean getRsv2()
Get the value of RSV2 bit.- Returns:
- The value of RSV2 bit.
-
setRsv2
public WebSocketFrame setRsv2(boolean rsv2)
Set the value of RSV2 bit.- Parameters:
rsv2- The value of RSV2 bit.- Returns:
thisobject.
-
getRsv3
public boolean getRsv3()
Get the value of RSV3 bit.- Returns:
- The value of RSV3 bit.
-
setRsv3
public WebSocketFrame setRsv3(boolean rsv3)
Set the value of RSV3 bit.- Parameters:
rsv3- The value of RSV3 bit.- Returns:
thisobject.
-
getOpcode
public int getOpcode()
Get the opcode.WebSocket opcode Value Description 0x0 Frame continuation 0x1 Text frame 0x2 Binary frame 0x3-0x7 Reserved 0x8 Connection close 0x9 Ping 0xA Pong 0xB-0xF Reserved - Returns:
- The opcode.
- See Also:
WebSocketOpcode
-
setOpcode
public WebSocketFrame setOpcode(int opcode)
Set the opcode- Parameters:
opcode- The opcode.- Returns:
thisobject.- See Also:
WebSocketOpcode
-
isContinuationFrame
public boolean isContinuationFrame()
Check if this frame is a continuation frame.This method returns
truewhen the value of the opcode is 0x0 (WebSocketOpcode.CONTINUATION).- Returns:
trueif this frame is a continuation frame (= if the opcode is 0x0).
-
isTextFrame
public boolean isTextFrame()
Check if this frame is a text frame.This method returns
truewhen the value of the opcode is 0x1 (WebSocketOpcode.TEXT).- Returns:
trueif this frame is a text frame (= if the opcode is 0x1).
-
isBinaryFrame
public boolean isBinaryFrame()
Check if this frame is a binary frame.This method returns
truewhen the value of the opcode is 0x2 (WebSocketOpcode.BINARY).- Returns:
trueif this frame is a binary frame (= if the opcode is 0x2).
-
isCloseFrame
public boolean isCloseFrame()
Check if this frame is a close frame.This method returns
truewhen the value of the opcode is 0x8 (WebSocketOpcode.CLOSE).- Returns:
trueif this frame is a close frame (= if the opcode is 0x8).
-
isPingFrame
public boolean isPingFrame()
Check if this frame is a ping frame.This method returns
truewhen the value of the opcode is 0x9 (WebSocketOpcode.PING).- Returns:
trueif this frame is a ping frame (= if the opcode is 0x9).
-
isPongFrame
public boolean isPongFrame()
Check if this frame is a pong frame.This method returns
truewhen the value of the opcode is 0xA (WebSocketOpcode.PONG).- Returns:
trueif this frame is a pong frame (= if the opcode is 0xA).
-
isDataFrame
public boolean isDataFrame()
Check if this frame is a data frame.This method returns
truewhen the value of the opcode is in between 0x1 and 0x7.- Returns:
trueif this frame is a data frame (= if the opcode is in between 0x1 and 0x7).
-
isControlFrame
public boolean isControlFrame()
Check if this frame is a control frame.This method returns
truewhen the value of the opcode is in between 0x8 and 0xF.- Returns:
trueif this frame is a control frame (= if the opcode is in between 0x8 and 0xF).
-
getMask
boolean getMask()
Get the value of MASK bit.- Returns:
- The value of MASK bit.
-
setMask
WebSocketFrame setMask(boolean mask)
Set the value of MASK bit.- Parameters:
mask- The value of MASK bit.- Returns:
thisobject.
-
hasPayload
public boolean hasPayload()
Check if this frame has payload.- Returns:
trueif this frame has payload.
-
getPayloadLength
public int getPayloadLength()
Get the payload length.- Returns:
- The payload length.
-
getPayload
public byte[] getPayload()
Get the unmasked payload.- Returns:
- The unmasked payload.
nullmay be returned.
-
getPayloadText
public java.lang.String getPayloadText()
Get the unmasked payload as a text.- Returns:
- A string constructed by interrupting the payload as a UTF-8 bytes.
-
setPayload
public WebSocketFrame setPayload(byte[] payload)
Set the unmasked payload.Note that the payload length of a control frame must be 125 bytes or less.
- Parameters:
payload- The unmasked payload.nullis accepted. An empty byte array is treated in the same way asnull.- Returns:
thisobject.
-
setPayload
public WebSocketFrame setPayload(java.lang.String payload)
Set the payload. The given string is converted to a byte array in UTF-8 encoding.Note that the payload length of a control frame must be 125 bytes or less.
- Parameters:
payload- The unmasked payload.nullis accepted. An empty string is treated in the same way asnull.- Returns:
thisobject.
-
setCloseFramePayload
public WebSocketFrame setCloseFramePayload(int closeCode, java.lang.String reason)
Set the payload that conforms to the payload format of close frames.The given parameters are encoded based on the rules described in "5.5.1. Close" of RFC 6455.
Note that the reason should not be too long because the payload length of a control frame must be 125 bytes or less.
- Parameters:
closeCode- The close code.reason- The reason.nullis accepted. An empty string is treated in the same way asnull.- Returns:
thisobject.- See Also:
- RFC 6455, 5.5.1. Close,
WebSocketCloseCode
-
getCloseCode
public int getCloseCode()
Parse the first two bytes of the payload as a close code.If any payload is not set or the length of the payload is less than 2, this method returns 1005 (
WebSocketCloseCode.NONE).The value returned from this method is meaningless if this frame is not a close frame.
- Returns:
- The close code.
- See Also:
- RFC 6455, 5.5.1. Close,
WebSocketCloseCode
-
getCloseReason
public java.lang.String getCloseReason()
Parse the third and subsequent bytes of the payload as a close reason.If any payload is not set or the length of the payload is less than 3, this method returns
null.The value returned from this method is meaningless if this frame is not a close frame.
- Returns:
- The close reason.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
appendPayloadCommon
private boolean appendPayloadCommon(java.lang.StringBuilder builder)
-
appendPayloadText
private void appendPayloadText(java.lang.StringBuilder builder)
-
appendPayloadClose
private void appendPayloadClose(java.lang.StringBuilder builder)
-
appendPayloadBinary
private void appendPayloadBinary(java.lang.StringBuilder builder)
-
createContinuationFrame
public static WebSocketFrame createContinuationFrame()
Create a continuation frame. Note that the FIN bit of the returned frame is false.- Returns:
- A WebSocket frame whose FIN bit is false, opcode is
CONTINUATIONand payload isnull.
-
createContinuationFrame
public static WebSocketFrame createContinuationFrame(byte[] payload)
Create a continuation frame. Note that the FIN bit of the returned frame is false.- Parameters:
payload- The payload for a newly create frame.- Returns:
- A WebSocket frame whose FIN bit is false, opcode is
CONTINUATIONand payload is the given one.
-
createContinuationFrame
public static WebSocketFrame createContinuationFrame(java.lang.String payload)
Create a continuation frame. Note that the FIN bit of the returned frame is false.- Parameters:
payload- The payload for a newly create frame.- Returns:
- A WebSocket frame whose FIN bit is false, opcode is
CONTINUATIONand payload is the given one.
-
createTextFrame
public static WebSocketFrame createTextFrame(java.lang.String payload)
Create a text frame.- Parameters:
payload- The payload for a newly created frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
TEXTand payload is the given one.
-
createBinaryFrame
public static WebSocketFrame createBinaryFrame(byte[] payload)
Create a binary frame.- Parameters:
payload- The payload for a newly created frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
BINARYand payload is the given one.
-
createCloseFrame
public static WebSocketFrame createCloseFrame()
Create a close frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
CLOSEand payload isnull.
-
createCloseFrame
public static WebSocketFrame createCloseFrame(int closeCode)
Create a close frame.- Parameters:
closeCode- The close code.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
CLOSEand payload contains a close code. - See Also:
WebSocketCloseCode
-
createCloseFrame
public static WebSocketFrame createCloseFrame(int closeCode, java.lang.String reason)
Create a close frame.- Parameters:
closeCode- The close code.reason- The close reason. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
CLOSEand payload contains a close code and a close reason. - See Also:
WebSocketCloseCode
-
createPingFrame
public static WebSocketFrame createPingFrame()
Create a ping frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PINGand payload isnull.
-
createPingFrame
public static WebSocketFrame createPingFrame(byte[] payload)
Create a ping frame.- Parameters:
payload- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PINGand payload is the given one.
-
createPingFrame
public static WebSocketFrame createPingFrame(java.lang.String payload)
Create a ping frame.- Parameters:
payload- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PINGand payload is the given one.
-
createPongFrame
public static WebSocketFrame createPongFrame()
Create a pong frame.- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PONGand payload isnull.
-
createPongFrame
public static WebSocketFrame createPongFrame(byte[] payload)
Create a pong frame.- Parameters:
payload- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PONGand payload is the given one.
-
createPongFrame
public static WebSocketFrame createPongFrame(java.lang.String payload)
Create a pong frame.- Parameters:
payload- The payload for a newly created frame. Note that a control frame's payload length must be 125 bytes or less (RFC 6455, 5.5. Control Frames).- Returns:
- A WebSocket frame whose FIN bit is true, opcode is
PONGand payload is the given one.
-
mask
static byte[] mask(byte[] maskingKey, byte[] payload)Mask/unmask payload.The logic of masking/unmasking is described in "5.3. Client-to-Server Masking" in RFC 6455.
- Parameters:
maskingKey- The masking key. Ifnullis given or the length of the masking key is less than 4, nothing is performed.payload- Payload to be masked/unmasked.- Returns:
payload.- See Also:
- 5.3. Client-to-Server Masking
-
compressFrame
static WebSocketFrame compressFrame(WebSocketFrame frame, PerMessageCompressionExtension pmce)
-
compress
private static byte[] compress(byte[] data, PerMessageCompressionExtension pmce)
-
splitIfNecessary
static java.util.List<WebSocketFrame> splitIfNecessary(WebSocketFrame frame, int maxPayloadSize, PerMessageCompressionExtension pmce)
-
split
private static java.util.List<WebSocketFrame> split(WebSocketFrame frame, int maxPayloadSize)
-
-