diff --git a/src/main/java/org/java_websocket/WebSocket.java b/src/main/java/org/java_websocket/WebSocket.java index 514d8d884..49fdcd5da 100644 --- a/src/main/java/org/java_websocket/WebSocket.java +++ b/src/main/java/org/java_websocket/WebSocket.java @@ -220,4 +220,21 @@ enum READYSTATE { * @return Returns the decoded path component of this URI. **/ String getResourceDescriptor(); + + /** + * Setter for an attachment on the socket connection. + * The attachment may be of any type. + * + * @param attachment The object to be attached to the user + * @since 1.3.7 + **/ + void setAttachment(T attachment); + + /** + * Getter for the connection attachment. + * + * @return Returns the user attachment + * @since 1.3.7 + **/ + T getAttachment(); } \ No newline at end of file diff --git a/src/main/java/org/java_websocket/WebSocketImpl.java b/src/main/java/org/java_websocket/WebSocketImpl.java index cd803a0c5..31e2cd7c2 100644 --- a/src/main/java/org/java_websocket/WebSocketImpl.java +++ b/src/main/java/org/java_websocket/WebSocketImpl.java @@ -145,6 +145,12 @@ public class WebSocketImpl implements WebSocket { */ private PingFrame pingFrame; + /** + * Attribute to store connection attachment + * @since 1.3.7 + */ + private Object attachment; + /** * Creates a websocket with server role * @@ -799,4 +805,15 @@ public WebSocketListener getWebSocketListener() { return wsl; } + @Override + @SuppressWarnings("unchecked") + public T getAttachment() { + return (T) attachment; + } + + @Override + public void setAttachment(T attachment) { + this.attachment = attachment; + } + }