Skip to content

Commit 2cb7f67

Browse files
committed
Added setAttachment and getAttachment to WebSocket interface.
This allows a WebSocket connection object to have any kind of data associated.
1 parent c332701 commit 2cb7f67

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/main/java/org/java_websocket/WebSocket.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,19 @@ enum READYSTATE {
220220
* @return Returns the decoded path component of this URI.
221221
**/
222222
String getResourceDescriptor();
223+
224+
/**
225+
* Setter for an attachment on the socket connection.
226+
* The attachment may be of any type.
227+
*
228+
* @param attachment The object to be attached to the user
229+
**/
230+
<T> void setAttachment(T attachment);
231+
232+
/**
233+
* Getter for the connection attachment.
234+
*
235+
* @return Returns the user attachment
236+
**/
237+
<T> T getAttachment();
223238
}

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public class WebSocketImpl implements WebSocket {
145145
*/
146146
private PingFrame pingFrame;
147147

148+
/**
149+
* Attribute to store connection attachment
150+
*/
151+
private Object attachment;
152+
148153
/**
149154
* Creates a websocket with server role
150155
*
@@ -799,4 +804,15 @@ public WebSocketListener getWebSocketListener() {
799804
return wsl;
800805
}
801806

807+
@Override
808+
@SuppressWarnings("unchecked")
809+
public <T> T getAttachment() {
810+
return (T) attachment;
811+
}
812+
813+
@Override
814+
public <T> void setAttachment(T attachment) {
815+
this.attachment = attachment;
816+
}
817+
802818
}

0 commit comments

Comments
 (0)