1
@ServerEndpoint("/websocket/blah")
public class LeadChangeWebsocketEndpoint {
    @Inject
    private SessionScopedBean bean;

    @OnOpen
    public void onOpen(Session session) {
        bean.takeWebsocketAndDoSomething(session);
    }
    //....
}

That code results in an exception. How does one associate a websocket with a user's session? I can't find any documentation explicitly stating what the CDI scope of a @ServerEndpoint bean is, even though it is capable of receiving CDI injections.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • Before `onOpen` is called, the HTTP request is still alive with all it's cookies, tokens etc'. That's the time to authenticate the user or refuse the connection... especially as (on some servers) the `onMessage` callback might be called while `onOpen` is still processing... I'm not much into Java, so I'm not sure how you would apply this (I can show you in Ruby, but that won't help much). – Myst Jul 27 '15 at 02:03
  • 1
    It looks like it is not so well supported. http://stackoverflow.com/questions/21049387/using-a-cdi-sessionscoped-bean-from-a-websocket-serverendpoint Did you try to annotated your class with @Stateless. Maybe this one could work. – user3584190 Jul 27 '15 at 07:55
  • Yikes... I'll see if I can get the Java EE expert group to take a look at that and clarify. I didn't try it as an EJB though, seems scary haha. I'll post the results for TomEE – Jonathan S. Fisher Jul 27 '15 at 15:00
  • @exabrial you forgot the result – Ced May 08 '16 at 01:51
  • You won't like it, it's not pretty... see here: http://stackoverflow.com/questions/21888425/accessing-servletcontext-and-httpsession-in-onmessage-of-a-jsr-356-serverendpo – Jonathan S. Fisher May 09 '16 at 14:24

0 Answers0