I am using javax.websocket.*
API right now but I don't know how to initialize an Endpoint
with some constructor parameters after searching on the Internet.
ServerContainer container = WebSocketServerContainerInitializer.configureContext(context); //jetty
container.addEndpoint(MyWebSocketEndpoint.class);
I want pass through some parameters when initializing MyWebSocketEndpoint
then I can use the parameter, say clientQueue
, in my onOpen
method doing something like:
clientQueue.add(new Client(session));
The Web Socket Endpoint represents an object that can handle websocket conversations. Developers may extend this class in order to implement a programmatic websocket endpoint. The Endpoint class holds lifecycle methods that may be overridden to intercept websocket open, error and close events.
A WebSocketContainer is an implementation provided object that provides applications a view on the container running it.
A Web Socket session represents a conversation between two web socket endpoints. As soon as the websocket handshake completes successfully, the web socket implementation provides the endpoint an open websocket session.
You need to call ServerContainer.addEndpoint(ServerEndpointConfig)
and need a ServerEndpointConfig.Configurator
implementation to make this work.
First create a custom ServerEndpointConfig.Configurator
class which acts as factory for your endpoint:
public class MyWebSocketEndpointConfigurator extends ServerEndpointConfig.Configurator {
private ClientQueue clientQueue_;
public MyWebSocketEndpoint(ClientQueue clientQueue) {
clientQueue_ = clientQueue;
}
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
return (T)new MyWebSocketEndpoint(clientQueue_);
}
}
and then register it on the ServerContainer
:
ClientQueue clientQueue = ...
ServerContainer container = ...
container.addEndpoint(ServerEndpointConfig.Builder
.create(MyWebSocketEndpoint.class, "/") // the endpoint url
.configurator(new MyWebSocketEndpointConfigurator(clientQueue _))
.build());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With