]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/websocket/server/EventEndpoint.java
1 package org
.argeo
.cms
.websocket
.server
;
3 import java
.io
.IOException
;
4 import java
.nio
.channels
.ClosedChannelException
;
7 import javax
.websocket
.OnClose
;
8 import javax
.websocket
.OnError
;
9 import javax
.websocket
.OnOpen
;
10 import javax
.websocket
.RemoteEndpoint
;
11 import javax
.websocket
.Session
;
12 import javax
.websocket
.server
.PathParam
;
13 import javax
.websocket
.server
.ServerEndpoint
;
15 import org
.argeo
.api
.cms
.CmsEventBus
;
16 import org
.argeo
.api
.cms
.CmsEventSubscriber
;
17 import org
.argeo
.api
.cms
.CmsLog
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.FrameworkUtil
;
21 @ServerEndpoint(value
= "/cms/status/event/{topic}", configurator
= CmsWebSocketConfigurator
.class)
22 public class EventEndpoint
implements CmsEventSubscriber
{
23 private final static CmsLog log
= CmsLog
.getLog(EventEndpoint
.class);
24 private BundleContext bc
= FrameworkUtil
.getBundle(EventEndpoint
.class).getBundleContext();
26 private RemoteEndpoint
.Basic remote
;
27 private CmsEventBus cmsEventBus
;
29 // private String topic = "cms";
32 public void onOpen(Session session
, @PathParam("topic") String topic
) {
34 cmsEventBus
= bc
.getService(bc
.getServiceReference(CmsEventBus
.class));
35 cmsEventBus
.addEventSubscriber(topic
, this);
37 remote
= session
.getBasicRemote();
42 public void onClose(@PathParam("topic") String topic
) {
43 cmsEventBus
.removeEventSubscriber(topic
, this);
47 public void onEvent(String topic
, Map
<String
, Object
> properties
) {
49 remote
.sendText(topic
+ ": " + properties
);
50 } catch (IOException e
) {
51 throw new IllegalStateException(e
);
56 public void onError(Throwable e
) {
57 if (e
instanceof ClosedChannelException
) {
58 // ignore, as it probably means ping was closed on the other side
61 log
.error("Cannot process ping", e
);