websocket
websocket copied to clipboard
Problem with @ClientEndpoint
Hi. I want to get the instance who called connectToServer in the client endpoint.
I can achieve it by the programmatic way.
public class BilibiliLiveClientEndpoint extends Endpoint {
public BilibiliLiveClientEndpoint(BilibiliLive bilibiliLive) {
this.bilibiliLive = bilibiliLive;
session = ContainerProvider.getWebSocketContainer().connectToServer(new BilibiliLiveClientEndpoint(this), ClientEndpointConfig.Builder.create().build(), new URI(URL));
For the programmatic way, I can also use the EndpointConfig.getUserProperties().put() to add custom properties to transfer the instance inside.
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
clientEndpointConfig.getUserProperties().put("bilibiliLive", this);
session = ContainerProvider.getWebSocketContainer().connectToServer(BilibiliLiveClientEndpoint.class, clientEndpointConfig, new URI(URL));
But I can't find a way to achieve it by the annotation way.
@ClientEndpoint
public class BilibiliLiveClientEndpointAnnotated {
@OnOpen
public void onOpen(Session session) {
try {
BilibiliLive bilibiliLive = BilibiliLiveJakarta.sessionBilibiliLiveJakartaMap.get(session);
session = ContainerProvider.getWebSocketContainer().connectToServer(BilibiliLiveClientEndpointAnnotated.class, new URI(URL));
sessionBilibiliLiveJakartaMap.put(session, this);
It seems that the only way to index the instance who called connectToServer is by session. But the session is not returned if the onOpen method is not completed. so the bilibiliLive instance is null.
Is there a way to achieve it by the annotation way? Thank you.
Since anyone using your Client Endpoint can call connectToServer
, either from the Client Container, or the Server Container, there's no real way to obtain this calling information before the upgrade process begins.
Why do you need this? (It's unclear from your example code)
I need some specific data from each caller. So I'd like to have a way to send it to the client endpoint. Maybe a client endpoint to connect with room 12345. Another one to connect with room 54321. I have to send the room id to the server. The server is not designed by me.
They way to do things like that is to map the server endpoint using a URI-template and use the @PathParam
annotation.