1

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?

Piotr Müller
  • 5,323
  • 5
  • 55
  • 82

1 Answers1

0

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 );
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
  • 2
    even if you want to use container instantiation, there is a way - you can add properties you need to pass to ClientEndpointConfig and the retrieve it in @OnOpen (it can be added as another parameter). See EndpointConfig.getUserProperties. – Pavel Bucek Mar 21 '14 at 08:10