Merged many of the changes from the first attempt at an HTML5 Websocket implementation. Focused on the ones that didn't change the behavior of the web server.
This commit is contained in:
@@ -8,14 +8,62 @@ package com.foundation.web.interfaces;
|
||||
public interface IConnectionContext {
|
||||
/**
|
||||
* Gets the application data in the connection context's application data map by the given key.
|
||||
* This is connection specific data, not client specific data which should be stored in the session.
|
||||
* <br/>Warning: Websocket connections are multi threaded and as such, threads must synchronize before calling this method.
|
||||
* @return The application specific data element.
|
||||
*/
|
||||
public Object getApplicationData(String key);
|
||||
/**
|
||||
* Stores the application data in the connection context's application data map by the given key.
|
||||
* The application data may implement ISessionLifecycleAware if it should be called when the session is being released. This will only be called for a normal closing of the session. The session may still be restored at some time in the future.
|
||||
* The applicationData may implement ISessionLifecycleAware (ignore that this is not a session) if it should be called when the connection is being closed.
|
||||
* This is connection specific data, not client specific data which should be stored in the session.
|
||||
* <br/>Warning: Websocket connections are multi threaded and as such, threads must synchronize before calling this method.
|
||||
* @param key The key for the data. If the key is logically equal (equivalent) to an existing key, then the existing data will be replaced and returned.
|
||||
* @param applicationData The application specific data element.
|
||||
*/
|
||||
public void setApplicationData(String key, Object applicationData);
|
||||
/**
|
||||
* Upgrades the connection context to be a HTML5 websocket using the given optional protocol.
|
||||
* @param protocol The optional protocol which will be passed to the application when frames are received.
|
||||
* @param maxMessageLength The maximum number of bytes in an allowed websocket message (may be composed of multiple frames, does not include the frame header sizes).
|
||||
* @param websocketHandler The application provided handler of websocket messages.
|
||||
*/
|
||||
public void upgradeToWebsocket(String protocol, long maxMessageLength, WebsocketHandler websocketHandler);
|
||||
/**
|
||||
* Sends the message to the client if this connection has been upgraded to a websocket.
|
||||
* <br/>The call is ignored if this is not a websocket.
|
||||
* <br/>This is a thread safe call.
|
||||
* @param message The message. The message will be queued for sending and the call will return immediately.
|
||||
*/
|
||||
public void sendWebsocketMessage(byte[] message);
|
||||
/**
|
||||
* Sends the message to the client if this connection has been upgraded to a websocket.
|
||||
* <br/>The call is ignored if this is not a websocket.
|
||||
* <br/>This is a thread safe call.
|
||||
* @param message The message. The message will be queued for sending and the call will return immediately.
|
||||
*/
|
||||
public void sendWebsocketMessage(String message);
|
||||
/**
|
||||
* Sends the message to the client if this connection has been upgraded to a websocket.
|
||||
* <br/>The call is ignored if this is not a websocket.
|
||||
* <br/>This is a thread safe call.
|
||||
* @param message The message. The message will be queued for sending and the call will return immediately.
|
||||
*/
|
||||
public void sendWebsocketMessage(IStreamedWebsocketMessage message);
|
||||
/**
|
||||
* Sends the PING message to the client if this connection has been upgraded to a websocket.
|
||||
* <br/>The call is ignored if this is not a websocket.
|
||||
* <br/>This is a thread safe call.
|
||||
*/
|
||||
public void sendWebsocketPing();
|
||||
/**
|
||||
* Gets the protocol associated with this websocket.
|
||||
* @return The optional protocol associated with the connection context when it was upgraded to a websocket, or null if this context was never upgraded.
|
||||
*/
|
||||
public String getWebsocketProtocol();
|
||||
/**
|
||||
* Determines whether this connection context has been upgraded to a websocket.
|
||||
* @return Whether this is a websocket connection.
|
||||
*/
|
||||
public boolean isWebsocket();
|
||||
}//IConnectionContext//
|
||||
@@ -19,6 +19,8 @@ public interface IResponse {
|
||||
public static final int ERROR_TYPE_RESOURCE_NOT_MODIFIED = 3;
|
||||
/** Used to warn the user that their client did not use TLS + the domain extension, and the server cannot identify which certificate to use to allow them to connect. The user should upgrade their browser. */
|
||||
public static final int ERROR_TYPE_TLS_FAILURE = 4;
|
||||
/** Used to notify the client that the socket upgrade or protocol change failed or was rejected by the server. */
|
||||
public static final int ERROR_UPGRADE_REJECTED = 5;
|
||||
/**
|
||||
* Gets the request this response is responding to.
|
||||
* @return The request that created this response.
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.foundation.web.interfaces;
|
||||
|
||||
/**
|
||||
* Used to stream a message to the client when the size of the message is unknown, or when the content is large and loading it all into memory at once is undesireable.
|
||||
*/
|
||||
public interface IStreamedWebsocketMessage {
|
||||
/**
|
||||
* Gets the next part of the message.
|
||||
* @return The non-null byte[] or String for the message. The returned type must be consistent for every call.
|
||||
*/
|
||||
public Object getNextPart();
|
||||
/**
|
||||
* Whether there are more parts to the message than those already retrieved.
|
||||
* @return Whether additional parts exist.
|
||||
*/
|
||||
public boolean hasNextPart();
|
||||
}//IStreamedWebsocketMessage//
|
||||
@@ -161,6 +161,10 @@ public ISession createSession();
|
||||
* @return The newly created and indexed secure session.
|
||||
*/
|
||||
public void createSecureSession(ISession session);
|
||||
/**
|
||||
* Gives the web application a chance to handle the web socket upgrade.
|
||||
*/
|
||||
public void handleWebSocketUpgrade(IRequest request, IResponse response, ISession session, boolean isSecure, boolean clientHadBadSession, IConnectionContext connectionContext, String connection, String secWebSocketKey, String secWebSocketProtocol, String secWebSocketVersion, String origin);
|
||||
/**
|
||||
* Processes a request from the client associated with the session. The result is placed in the response object.
|
||||
* @param request The request metadata.
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.foundation.web.interfaces;
|
||||
|
||||
|
||||
/**
|
||||
* The base class for a websocket handler passed to the IConnectionContext object when converting an HTTP connection into a websocket.
|
||||
*/
|
||||
public abstract class WebsocketHandler {
|
||||
/** The websocket connection reference. */
|
||||
private IConnectionContext connection;
|
||||
/**
|
||||
* WebsocketHandler constructor.
|
||||
* @param connection The websocket connection reference.
|
||||
*/
|
||||
public WebsocketHandler(IConnectionContext connection) {
|
||||
this.connection = connection;
|
||||
}//WebsocketHandler()//
|
||||
/**
|
||||
* Receives a text message from the client.
|
||||
* @param message The message received.
|
||||
*/
|
||||
public abstract void receiveTextMessage(String message);
|
||||
/**
|
||||
* Receives a binary message from the client.
|
||||
* @param message The message received.
|
||||
*/
|
||||
public abstract void receiveBinaryMessage(byte[] message);
|
||||
/**
|
||||
* Receives a pong control message from the client (in reponse to a ping from the server).
|
||||
*/
|
||||
public void receivePong() {
|
||||
//Does nothing.//
|
||||
}//receivePong()//
|
||||
/**
|
||||
* Sends a text message to the client without blocking.
|
||||
* <br/>This is a thread safe call.
|
||||
* @param message The message to be sent.
|
||||
*/
|
||||
public void sendTextMessage(String message) {
|
||||
connection.sendWebsocketMessage(message);
|
||||
}//sendTextMessage()//
|
||||
/**
|
||||
* Sends a binary message to the client without blocking.
|
||||
* <br/>This is a thread safe call.
|
||||
* @param message The message to be sent.
|
||||
*/
|
||||
public void sendBinaryMessage(byte[] message) {
|
||||
connection.sendWebsocketMessage(message);
|
||||
}//sendBinaryMessage()//
|
||||
/**
|
||||
* Sends a streamed message to the client without blocking.
|
||||
* <br/>This is a thread safe call.
|
||||
* @param message The message to be sent.
|
||||
*/
|
||||
public void sendStreamedMessage(IStreamedWebsocketMessage message) {
|
||||
connection.sendWebsocketMessage(message);
|
||||
}//sendStreamedMessage()//
|
||||
/**
|
||||
* Sends a ping control message to the client which will respond with a pong control message.
|
||||
*/
|
||||
public void sendPing() {
|
||||
connection.sendWebsocketPing();
|
||||
}//sendPing()//
|
||||
/**
|
||||
* Called by the connection when it is closed for any reason.
|
||||
*/
|
||||
public abstract void connectionClosed();
|
||||
}//WebsocketHandler//
|
||||
Reference in New Issue
Block a user