Previous Topic

Next Topic

Book Contents

Book Index

Registering Websocket Server Endpoint

There are two ways to register a server endpoint in OSGi:

When registering the endpoint class as a service, the service registration property com.prosyst.mbs.services.websocket.EndpointWhiteboardConstants.WS_ENDPOINT_ACTIVATION_PROPERTY must be set. This is needed for the Websocket Container to distinguish the server endpoint classes among other services of Class type.

The value of the property is not important as long as it is not null, only its presence is tested. The following example shows, how the DemoEndpoint from above can be registered:

import org.osgi.framework.BundleContext;

import org.osgi.framework.ServiceRegistration;

import com.prosyst.mbs.services.websocket.EndpointWhiteboardConstants;

...

private ServiceRegistration<Class> endpointClassRegistration;

void start(BundleContext bc) {

Dictionary<String, Object> properties = new Hashtable<>();

properties.put(EndpointWhiteboardConstants.WS_ENDPOINT_ACTIVATION_PROPERTY, Boolean.TRUE);

endpointClassRegistration = bc.registerService(Class.class, DemoEndpoint.class, properties);

}

The Activation property is not required, when registering endpoint using ServerEndpointConfig:

private ServiceRegistration<ServerEndpointConfig> endpointConfigRegistration;

...

void start(BundleContext bc) {

ServerEndpointConfig endpointConfig = ServerEndpointConfig.Builder.create(DemoEndpoint.class, "/ws/test").build();

endpointConfigRegistration = bc.registerService(ServerEndpointConfig.class, endpointConfig, null);

}

When registering a ServerEndpointConfig service, a different path from the one in the ServerEndpoint annotation can be specified. This can be used to register a single endpoint class for different paths – using several ServerEndpointConfig service registrations.

When registering an endpoint either as a Class or a ServerEndpointConfig service, the registration property HttpWhiteboardConstants.html.HTTP_WHITEBOARD_TARGET can be used to associate the endpoint with only specific HTTP service instances. If the property is not specified, the endpoint will be registered will all HTTP Whiteboard implementations.

The service registration property HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT can be used to specify a ServletContextHelper which will handle security during HTTP protocol upgrade. If the property is omitted, the default ServletContextHelper will be used.

When server endpoint is unregistered from OSGi, all of the active connections associated with it are closed with the Websocket status code 1001, Going Away.