Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Jetty 9.1 WebSocketCreator + javax.websocket.Endpoint

Thank you for the explanation. The problem is that I would like a single place to be able to receive all WebSocket requests and then create custom JSR WebSocket objects based on the request path. I accomplished this by simply having a single class annotated with @ServerEndpoint("/{path}"), but of course this means that I cannot have any slashes in my WebSocket paths.

This isn't ideal, but we can deal with it. And this way, we don't need any configurators or WebSocketCreator classes or anything. We just have a single ServerEndpoint-annotated class with all of the relevant annotated methods that passes along each request to our own method API.

I'm sure there's a better way to do all of this, but this way seems to be working.

On to the next stumbling block...


On Sat, Nov 2, 2013 at 7:23 AM, Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
Well ...

Pure Jetty WebSocket API Use

WebSocketCreator is a Jetty WebSocket API (predates the javax.websocket standard)

If you want to use WebSocketCreator, then you should return an object that either
  • implements org.eclipse.jetty.websocket.api.WebSocketListener
  • is annotated with org.eclipse.jetty.websocket.api.annotations.WebSocket

Pure javax.websocket API Use

However, if you want to use the javax.websocket standard (aka JSR-356), you have a different approach you can use.

Start by creating an object that implements javax.websocket.server.ServerEndpointConfig.Configurator.
eg:

public class MyCreator implements ServerEndpointConfig.Configurator

Be careful to implement the following methods.
  • checkOrigin(String originHeader)
  • modifyHandshake(ServerEndpointConfig sec, HandshakeRequest req, HandshakeResponse resp)
  • getEndpointInstance(Class endpointClass)
Then on your annotated websocket reference this custom configurator.
  @ServerEndpoint(value="/path", configurator=MyCreator.class)


The Deep Dark Underbelly of WebSocket in Jetty

Internally to Jetty, the entire javax.websocket support layer is built on top of the Pure Jetty WebSocket API.

Just to prove this point, here's the JsrCreator, the implementation of WebSocketCreator that does all of the work necessary to support the javax.websocket API.
But you might be asking, how can you return a javax.websocket from that!?
The support for JSR356 is added to the Jetty WebSocket API via the EventDriverFactory mechanism when the javax.websocket.server.ServerContainer.

As you can see, it is possible to use the WebSocketCreator pattern to make javax.websocket Endpoints, however, it requires a lot more work to wire up the JSR specifics.




--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Fri, Nov 1, 2013 at 5:05 PM, Brandon Mintern <mintern@xxxxxxxxxxx> wrote:
It surprised me that I could not return a javax.websocket.Endpoint from the WebSocketCreator.createWebSocket method. Where can I plug into Jetty in order to construct and return a custom Endpoint object that will manage my WebSocket interaction?

Thanks,
Brandon

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



Back to the top