Package org.java_websocket.drafts
Class Draft_6455
java.lang.Object
org.java_websocket.drafts.Draft
org.java_websocket.drafts.Draft_6455
Implementation for the RFC 6455 websocket protocol This is the recommended class for your
websocket connection
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List
<ByteBuffer> Attribute for the payload of the current continuous frameprivate static final String
Handshake specific field for the connectionprivate Framedata
Attribute for the current continuous frameprivate IExtension
Current active extension used to decode messagesprivate IExtension
Attribute for the default extensionprivate ByteBuffer
Attribute for the current incomplete frameprivate List
<IExtension> Attribute for all available extension in this draftAttribute for all available protocols in this draftprivate final org.slf4j.Logger
Logger instanceprivate int
Attribute for the maximum allowed size of a frameprivate IExtension
Attribute for the used extension in this draftprivate IProtocol
Attribute for the used protocol in this draftprivate final SecureRandom
Attribute for the reusable random instanceprivate static final String
Handshake specific field for the acceptprivate static final String
Handshake specific field for the extensionprivate static final String
Handshake specific field for the keyprivate static final String
Handshake specific field for the protocolprivate static final String
Handshake specific field for the upgradeFields inherited from class org.java_websocket.drafts.Draft
continuousFrameType, role
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor for the websocket protocol specified by RFC 6455 with default extensionsDraft_6455
(List<IExtension> inputExtensions) Constructor for the websocket protocol specified by RFC 6455 with custom extensionsDraft_6455
(List<IExtension> inputExtensions, int inputMaxFrameSize) Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocolsDraft_6455
(List<IExtension> inputExtensions, List<IProtocol> inputProtocols) Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocolsDraft_6455
(List<IExtension> inputExtensions, List<IProtocol> inputProtocols, int inputMaxFrameSize) Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocolsDraft_6455
(IExtension inputExtension) Constructor for the websocket protocol specified by RFC 6455 with custom extensions -
Method Summary
Modifier and TypeMethodDescriptionacceptHandshakeAsClient
(ClientHandshake request, ServerHandshake response) acceptHandshakeAsServer
(ClientHandshake handshakedata) private void
addToBufferList
(ByteBuffer payloadData) Add a payload to the current bytebuffer listprivate void
Check the current size of the buffer and throw an exception if the size is bigger than the max allowed frame sizeprivate void
Clear the current bytebuffer listprivate HandshakeState
containsRequestedProtocol
(String requestedProtocol) Check if the requested protocol is part of this draftDrafts must only be by one websocket at all.createBinaryFrame
(Framedata framedata) private ByteBuffer
createByteBufferFromFramedata
(Framedata framedata) createFrames
(String text, boolean mask) createFrames
(ByteBuffer binary, boolean mask) boolean
private byte
fromOpcode
(Opcode opcode) private String
Generate a final key from a input stringprivate long
Get the current size of the resulting bytebuffer in the bytebuffer listGetter for the extension which is used by this draftGetter for all available extensions for this draftGetter for all available protocols for this draftprivate byte
getMaskByte
(boolean mask) Get the mask byte if existingint
Getter for the maximum allowed payload size which is used by this draftprivate ByteBuffer
Method to generate a full bytebuffer out of all the fragmented frame payloadGetter for the protocol which is used by this draftprivate byte
getRSVByte
(int rsv) Get a byte that can set RSV bits when OR(|)'d.private String
Generate a date for for the date-headerprivate int
getSizeBytes
(ByteBuffer mes) Get the size bytes for the byte bufferint
hashCode()
private void
logRuntimeException
(WebSocketImpl webSocketImpl, RuntimeException e) Log the runtime exception to the specific WebSocketImplpostProcessHandshakeResponseAsServer
(ClientHandshake request, ServerHandshakeBuilder response) void
processFrame
(WebSocketImpl webSocketImpl, Framedata frame) Handle the frame specific to the draftprivate void
processFrameBinary
(WebSocketImpl webSocketImpl, Framedata frame) Process the frame if it is a binary frameprivate void
processFrameClosing
(WebSocketImpl webSocketImpl, Framedata frame) Process the frame if it is a closing frameprivate void
processFrameContinuousAndNonFin
(WebSocketImpl webSocketImpl, Framedata frame, Opcode curop) Process the frame if it is a continuous frame or the fin bit is not setprivate void
processFrameIsFin
(WebSocketImpl webSocketImpl, Framedata frame) Process the frame if it is the last frameprivate void
processFrameIsNotFin
(Framedata frame) Process the frame if it is not the last frameprivate void
processFrameText
(WebSocketImpl webSocketImpl, Framedata frame) Process the frame if it is a text framevoid
reset()
private byte[]
toByteArray
(long val, int bytecount) private Opcode
toOpcode
(byte opcode) toString()
translateFrame
(ByteBuffer buffer) private Framedata
translateSingleFrame
(ByteBuffer buffer) private void
translateSingleFrameCheckLengthLimit
(long length) Check if the frame size exceeds the allowed limitprivate void
translateSingleFrameCheckPacketSize
(int maxpacketsize, int realpacketsize) Check if the max packet size is smaller than the real packet sizetranslateSingleFramePayloadLength
(ByteBuffer buffer, Opcode optcode, int oldPayloadlength, int maxpacketsize, int oldRealpacketsize) Translate the buffer depending when it has an extended payload length (126 or 127)Methods inherited from class org.java_websocket.drafts.Draft
basicAccept, checkAlloc, continuousFrame, createHandshake, createHandshake, createHandshake, createHandshake, getRole, readLine, readStringLine, readVersion, setParseMode, translateHandshake, translateHandshakeHttp
-
Field Details
-
SEC_WEB_SOCKET_KEY
Handshake specific field for the key- See Also:
-
SEC_WEB_SOCKET_PROTOCOL
Handshake specific field for the protocol- See Also:
-
SEC_WEB_SOCKET_EXTENSIONS
Handshake specific field for the extension- See Also:
-
SEC_WEB_SOCKET_ACCEPT
Handshake specific field for the accept- See Also:
-
UPGRADE
Handshake specific field for the upgrade- See Also:
-
CONNECTION
Handshake specific field for the connection- See Also:
-
log
private final org.slf4j.Logger logLogger instance- Since:
- 1.4.0
-
negotiatedExtension
Attribute for the used extension in this draft -
defaultExtension
Attribute for the default extension -
knownExtensions
Attribute for all available extension in this draft -
currentDecodingExtension
Current active extension used to decode messages -
protocol
Attribute for the used protocol in this draft -
knownProtocols
Attribute for all available protocols in this draft -
currentContinuousFrame
Attribute for the current continuous frame -
byteBufferList
Attribute for the payload of the current continuous frame -
incompleteframe
Attribute for the current incomplete frame -
reuseableRandom
Attribute for the reusable random instance -
maxFrameSize
private int maxFrameSizeAttribute for the maximum allowed size of a frame- Since:
- 1.4.0
-
-
Constructor Details
-
Draft_6455
public Draft_6455()Constructor for the websocket protocol specified by RFC 6455 with default extensions- Since:
- 1.3.5
-
Draft_6455
Constructor for the websocket protocol specified by RFC 6455 with custom extensions- Parameters:
inputExtension
- the extension which should be used for this draft- Since:
- 1.3.5
-
Draft_6455
Constructor for the websocket protocol specified by RFC 6455 with custom extensions- Parameters:
inputExtensions
- the extensions which should be used for this draft- Since:
- 1.3.5
-
Draft_6455
Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols- Parameters:
inputExtensions
- the extensions which should be used for this draftinputProtocols
- the protocols which should be used for this draft- Since:
- 1.3.7
-
Draft_6455
Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols- Parameters:
inputExtensions
- the extensions which should be used for this draftinputMaxFrameSize
- the maximum allowed size of a frame (the real payload size, decoded frames can be bigger)- Since:
- 1.4.0
-
Draft_6455
public Draft_6455(List<IExtension> inputExtensions, List<IProtocol> inputProtocols, int inputMaxFrameSize) Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols- Parameters:
inputExtensions
- the extensions which should be used for this draftinputProtocols
- the protocols which should be used for this draftinputMaxFrameSize
- the maximum allowed size of a frame (the real payload size, decoded frames can be bigger)- Since:
- 1.4.0
-
-
Method Details
-
acceptHandshakeAsServer
public HandshakeState acceptHandshakeAsServer(ClientHandshake handshakedata) throws InvalidHandshakeException - Specified by:
acceptHandshakeAsServer
in classDraft
- Throws:
InvalidHandshakeException
-
containsRequestedProtocol
Check if the requested protocol is part of this draft- Parameters:
requestedProtocol
- the requested protocol- Returns:
- MATCHED if it is matched, otherwise NOT_MATCHED
-
acceptHandshakeAsClient
public HandshakeState acceptHandshakeAsClient(ClientHandshake request, ServerHandshake response) throws InvalidHandshakeException - Specified by:
acceptHandshakeAsClient
in classDraft
- Throws:
InvalidHandshakeException
-
getExtension
Getter for the extension which is used by this draft- Returns:
- the extension which is used or null, if handshake is not yet done
-
getKnownExtensions
Getter for all available extensions for this draft- Returns:
- the extensions which are enabled for this draft
-
getProtocol
Getter for the protocol which is used by this draft- Returns:
- the protocol which is used or null, if handshake is not yet done or no valid protocols
- Since:
- 1.3.7
-
getMaxFrameSize
public int getMaxFrameSize()Getter for the maximum allowed payload size which is used by this draft- Returns:
- the size, which is allowed for the payload
- Since:
- 1.4.0
-
getKnownProtocols
Getter for all available protocols for this draft- Returns:
- the protocols which are enabled for this draft
- Since:
- 1.3.7
-
postProcessHandshakeRequestAsClient
- Specified by:
postProcessHandshakeRequestAsClient
in classDraft
-
postProcessHandshakeResponseAsServer
public HandshakeBuilder postProcessHandshakeResponseAsServer(ClientHandshake request, ServerHandshakeBuilder response) throws InvalidHandshakeException - Specified by:
postProcessHandshakeResponseAsServer
in classDraft
- Throws:
InvalidHandshakeException
-
copyInstance
Description copied from class:Draft
Drafts must only be by one websocket at all. To prevent drafts to be used more than once the Websocket implementation should call this method in order to create a new usable version of a given draft instance.
The copy can be safely used in conjunction with a new websocket connection.- Specified by:
copyInstance
in classDraft
- Returns:
- a copy of the draft
-
createBinaryFrame
- Specified by:
createBinaryFrame
in classDraft
-
createByteBufferFromFramedata
-
translateSingleFrame
private Framedata translateSingleFrame(ByteBuffer buffer) throws IncompleteException, InvalidDataException -
translateSingleFramePayloadLength
private Draft_6455.TranslatedPayloadMetaData translateSingleFramePayloadLength(ByteBuffer buffer, Opcode optcode, int oldPayloadlength, int maxpacketsize, int oldRealpacketsize) throws InvalidFrameException, IncompleteException, LimitExceededException Translate the buffer depending when it has an extended payload length (126 or 127)- Parameters:
buffer
- the buffer to read fromoptcode
- the decoded optcodeoldPayloadlength
- the old payload lengthmaxpacketsize
- the max packet size allowedoldRealpacketsize
- the real packet size- Returns:
- the new payload data containing new payload length and new packet size
- Throws:
InvalidFrameException
- thrown if a control frame has an invalid lengthIncompleteException
- if the maxpacketsize is smaller than the realpackagesizeLimitExceededException
- if the payload length is to big
-
translateSingleFrameCheckLengthLimit
Check if the frame size exceeds the allowed limit- Parameters:
length
- the current payload length- Throws:
LimitExceededException
- if the payload length is to big
-
translateSingleFrameCheckPacketSize
private void translateSingleFrameCheckPacketSize(int maxpacketsize, int realpacketsize) throws IncompleteException Check if the max packet size is smaller than the real packet size- Parameters:
maxpacketsize
- the max packet sizerealpacketsize
- the real packet size- Throws:
IncompleteException
- if the maxpacketsize is smaller than the realpackagesize
-
getRSVByte
private byte getRSVByte(int rsv) Get a byte that can set RSV bits when OR(|)'d. 0 1 2 3 4 5 6 7 +-+-+-+-+-------+ |F|R|R|R| opcode| |I|S|S|S| (4) | |N|V|V|V| | | |1|2|3| |- Parameters:
rsv
- Can only be {0, 1, 2, 3}- Returns:
- byte that represents which RSV bit is set.
-
getMaskByte
private byte getMaskByte(boolean mask) Get the mask byte if existing- Parameters:
mask
- is mask active or not- Returns:
- -128 for true, 0 for false
-
getSizeBytes
Get the size bytes for the byte buffer- Parameters:
mes
- the current buffer- Returns:
- the size bytes
-
translateFrame
- Specified by:
translateFrame
in classDraft
- Throws:
InvalidDataException
-
createFrames
- Specified by:
createFrames
in classDraft
-
createFrames
- Specified by:
createFrames
in classDraft
-
reset
public void reset() -
getServerTime
Generate a date for for the date-header- Returns:
- the server time
-
generateFinalKey
Generate a final key from a input string- Parameters:
in
- the input string- Returns:
- a final key
-
toByteArray
private byte[] toByteArray(long val, int bytecount) -
fromOpcode
-
toOpcode
- Throws:
InvalidFrameException
-
processFrame
Description copied from class:Draft
Handle the frame specific to the draft- Specified by:
processFrame
in classDraft
- Parameters:
webSocketImpl
- the websocketimpl used for this draftframe
- the frame which is supposed to be handled- Throws:
InvalidDataException
- will be thrown on invalid data
-
processFrameContinuousAndNonFin
private void processFrameContinuousAndNonFin(WebSocketImpl webSocketImpl, Framedata frame, Opcode curop) throws InvalidDataException Process the frame if it is a continuous frame or the fin bit is not set- Parameters:
webSocketImpl
- the websocket implementation to useframe
- the current framecurop
- the current Opcode- Throws:
InvalidDataException
- if there is a protocol error
-
processFrameBinary
Process the frame if it is a binary frame- Parameters:
webSocketImpl
- the websocket implframe
- the frame
-
logRuntimeException
Log the runtime exception to the specific WebSocketImpl- Parameters:
webSocketImpl
- the implementation of the websockete
- the runtime exception
-
processFrameText
private void processFrameText(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException Process the frame if it is a text frame- Parameters:
webSocketImpl
- the websocket implframe
- the frame- Throws:
InvalidDataException
-
processFrameIsFin
private void processFrameIsFin(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException Process the frame if it is the last frame- Parameters:
webSocketImpl
- the websocket implframe
- the frame- Throws:
InvalidDataException
- if there is a protocol error
-
processFrameIsNotFin
Process the frame if it is not the last frame- Parameters:
frame
- the frame- Throws:
InvalidDataException
- if there is a protocol error
-
processFrameClosing
Process the frame if it is a closing frame- Parameters:
webSocketImpl
- the websocket implframe
- the frame
-
clearBufferList
private void clearBufferList()Clear the current bytebuffer list -
addToBufferList
Add a payload to the current bytebuffer list- Parameters:
payloadData
- the new payload
-
checkBufferLimit
Check the current size of the buffer and throw an exception if the size is bigger than the max allowed frame size- Throws:
LimitExceededException
- if the current size is bigger than the allowed size
-
getCloseHandshakeType
- Specified by:
getCloseHandshakeType
in classDraft
-
toString
-
equals
-
hashCode
public int hashCode() -
getPayloadFromByteBufferList
Method to generate a full bytebuffer out of all the fragmented frame payload- Returns:
- a bytebuffer containing all the data
- Throws:
LimitExceededException
- will be thrown when the totalSize is bigger then Integer.MAX_VALUE due to not being able to allocate more
-
getByteBufferListSize
private long getByteBufferListSize()Get the current size of the resulting bytebuffer in the bytebuffer list- Returns:
- the size as long (to not get an integer overflow)
-