From 2cb7f67f067f45727447cb92d9fe2094be8eea7f Mon Sep 17 00:00:00 2001 From: Toni Almeida Date: Thu, 30 Nov 2017 15:39:56 +0000 Subject: [PATCH 1/2] Added setAttachment and getAttachment to WebSocket interface. This allows a WebSocket connection object to have any kind of data associated. --- src/main/java/org/java_websocket/WebSocket.java | 15 +++++++++++++++ .../java/org/java_websocket/WebSocketImpl.java | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/main/java/org/java_websocket/WebSocket.java b/src/main/java/org/java_websocket/WebSocket.java index 514d8d884..98d26d57c 100644 --- a/src/main/java/org/java_websocket/WebSocket.java +++ b/src/main/java/org/java_websocket/WebSocket.java @@ -220,4 +220,19 @@ 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 + **/ + void setAttachment(T attachment); + + /** + * Getter for the connection attachment. + * + * @return Returns the user attachment + **/ + 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..5a38babdc 100644 --- a/src/main/java/org/java_websocket/WebSocketImpl.java +++ b/src/main/java/org/java_websocket/WebSocketImpl.java @@ -145,6 +145,11 @@ public class WebSocketImpl implements WebSocket { */ private PingFrame pingFrame; + /** + * Attribute to store connection attachment + */ + private Object attachment; + /** * Creates a websocket with server role * @@ -799,4 +804,15 @@ public WebSocketListener getWebSocketListener() { return wsl; } + @Override + @SuppressWarnings("unchecked") + public T getAttachment() { + return (T) attachment; + } + + @Override + public void setAttachment(T attachment) { + this.attachment = attachment; + } + } From c9e234bac7af54df838cf6d570932201942d8658 Mon Sep 17 00:00:00 2001 From: Toni Almeida Date: Mon, 4 Dec 2017 12:03:56 +0000 Subject: [PATCH 2/2] Added javadoc since tag --- src/main/java/org/java_websocket/WebSocket.java | 2 ++ src/main/java/org/java_websocket/WebSocketImpl.java | 1 + 2 files changed, 3 insertions(+) diff --git a/src/main/java/org/java_websocket/WebSocket.java b/src/main/java/org/java_websocket/WebSocket.java index 98d26d57c..49fdcd5da 100644 --- a/src/main/java/org/java_websocket/WebSocket.java +++ b/src/main/java/org/java_websocket/WebSocket.java @@ -226,6 +226,7 @@ enum READYSTATE { * 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); @@ -233,6 +234,7 @@ enum READYSTATE { * 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 5a38babdc..31e2cd7c2 100644 --- a/src/main/java/org/java_websocket/WebSocketImpl.java +++ b/src/main/java/org/java_websocket/WebSocketImpl.java @@ -147,6 +147,7 @@ public class WebSocketImpl implements WebSocket { /** * Attribute to store connection attachment + * @since 1.3.7 */ private Object attachment;