I want to add some additional attributes to container-instantiated WebSocket client endpoint for grouping/statistics. I created a client WebSocket endpoint with JSR-356:
Session session = container.connectToServer( MyClientEndpoint.class , uri );
I want to pass some object to Session
or MyClientEndpoint
instance:
@ClientEndpoint
public class MyClientEndpoint {
@OnOpen
public void onWebSocketConnect( Session sess ) {
...i need my param here...
}
... @OnMessage, @OnClose, @OnError handlers...
}
Because MyClientEndpoint
instance is instantiated by container (in my example - Jetty), I cannot just pass argument in constructor. Also I cannot set my param in Session
user properties:
Session session = container.connectToServer( ClientSocket.class , uri );
session.getUserProperties().put( "group", this);
because I don't have my property in @OnOpen
handler and also I have no guarantee that my "group"
property will be set before any @OnMessage
call.
How to connect to JSR-356 WebSocket in a way that will allow me to use additional objects in @ClientEndpoint object instantiated by container?
Actually, we are not forced to container-instantiation of @ClientEndpoint
instances. We can pass our custom instance:
ClientSocket socket = new ClientSocket( ANY PARAMS WE WANT );
Session session = container.connectToServer( socket , uri );