You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID> on 2021/07/17 10:59:56 UTC

broadcast to all sessions through websocket

Trying to broadcast to all sessions on a WebSocket but it is a bit hit 
and miss.  the WebSocket is got by

DefaultServerEndpointConfigurator dsec = new 
DefaultServerEndpointConfigurator();
NewsWebSocket nws = dsec.getEndpointInstance(NewsWebSocket.class);
nws.broadcast(myString);

and in the WebSocket I am trying to broadcast on looks like this

@ServerEndpoint(value = "/websockets/admin/news")
public class NewsWebSocket {
     private static final Set<Session> sessions = 
Collections.synchronizedSet(new HashSet<Session>());

     // ... @OnOpen, @OnMessage, @OnClose, @OnError

     public void broadcast(String string) {
         synchronized (sessions) {
             for (Session session : sessions) {
                  try {
                     session.getBasicRemote().sendText(string);
                 } catch (IOException ex) {
                     ex.printStackTrace();
                 }
             }
         }
     }
}

I also tried adding an "@Observes @MyEvent MyEvent blah" and firing an 
"Event<MyEvent>" but the breakpoint is never reached

Re: broadcast to all sessions through websocket

Posted by Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID>.
after investigation it seems that proxying the websocket through httpd 
is timing out the connection.  as it is not happening on my local machine

On 17/07/2021 12:59, Matthew Broadhead wrote:
> Trying to broadcast to all sessions on a WebSocket but it is a bit hit 
> and miss.  the WebSocket is got by
>
> DefaultServerEndpointConfigurator dsec = new 
> DefaultServerEndpointConfigurator();
> NewsWebSocket nws = dsec.getEndpointInstance(NewsWebSocket.class);
> nws.broadcast(myString);
>
> and in the WebSocket I am trying to broadcast on looks like this
>
> @ServerEndpoint(value = "/websockets/admin/news")
> public class NewsWebSocket {
>     private static final Set<Session> sessions = 
> Collections.synchronizedSet(new HashSet<Session>());
>
>     // ... @OnOpen, @OnMessage, @OnClose, @OnError
>
>     public void broadcast(String string) {
>         synchronized (sessions) {
>             for (Session session : sessions) {
>                  try {
>                     session.getBasicRemote().sendText(string);
>                 } catch (IOException ex) {
>                     ex.printStackTrace();
>                 }
>             }
>         }
>     }
> }
>
> I also tried adding an "@Observes @MyEvent MyEvent blah" and firing an 
> "Event<MyEvent>" but the breakpoint is never reached