/** * Sends the given message to the current client. Cannot be called if * {@isConnected()} is false. * * @param message * The message to send */ protected void sendMessage(String message) { assert (isConnected()); // "Broadcast" the changes to the single client only outgoingMessage = getResource().getBroadcaster().broadcast(message, getResource()); }
@Override public Broadcaster getBroadcaster() { return atmResource.getBroadcaster(); }
@Override public Broadcaster getBroadcaster() { return atmResource.getBroadcaster(); }
public Broadcaster getBroadcaster() { return atm.getBroadcaster(); }
public Class<? extends Broadcaster> broadcaster() { return r.getBroadcaster().getClass(); } };
@Override public Broadcaster getValue() { return getAtmosphereResource(Broadcaster.class, true).getBroadcaster(); } };
@Override public void run() { synchronized (getMutex(uuid)) { List<JSONObject> messages = messagesForUuid.get(uuid); if (messages != null) { JSONObject batchMessage = new JSONObject(); JSONArray jsonMessages = new JSONArray(messages); batchMessage.put("data", jsonMessages); batchMessage.put("type", "batch"); r.getBroadcaster().broadcast(batchMessage.toString(), r); broadcastScheduledForUuid.remove(uuid); messages.clear(); } } } }, delay, TimeUnit.MILLISECONDS);
@Override public void onRequest(AtmosphereResource resource) throws IOException { String body = IOUtils.readEntirely(resource).toString(); if (!body.isEmpty()) { resource.getBroadcaster().broadcast(body); } } };
@RequestMapping(value = "/websockets", method = RequestMethod.GET) @ResponseBody public void websockets(final AtmosphereResource event) { AtmosphereUtils.suspend(event); final Broadcaster bc = event.getBroadcaster(); bc.scheduleFixedBroadcast(new Callable<String>() { public String call() throws Exception { return (new Date()).toString(); } }, 1, TimeUnit.SECONDS); }
/** * Check to see if this atmosphere resource is still in use by the system. * It will query the associated broadcaster if it is still referenced. * * @return */ @Override public boolean isAlive() { if (writer.isTerminated()) { return false; } if ((System.currentTimeMillis() - startTime) < WARMUP_TIME) { return true; } if (atmResource.getBroadcaster() == null) { return false; } Collection<AtmosphereResource> res = atmResource.getBroadcaster().getAtmosphereResources(); for (AtmosphereResource ar : res) { if (ar == atmResource) { return true; } } return suspended; }
private String path; private BroadcasterFactory broadcasterFactory; @Ready(value = Ready.DELIVER_TO.ALL) public void onReady(final AtmosphereResource r) { System.out.println("onConnect"); r.addEventListener(new AtmosphereConnectionController()); if(broadcasterFactory == null){ path = r.getBroadcaster().getID(); broadcasterFactory = r.getAtmosphereConfig().getBroadcasterFactory(); } } //later in code broadcasterFactory.lookup(path).broadcast("message");
/** * {@inheritDoc} */ @Override public void onClose(WebSocket webSocket) { AtmosphereResource resource = webSocket.resource(); if (resource != null) { resource.getBroadcaster().removeAtmosphereResource(resource); } }
private void ensureInitialized(AtmosphereResource resource) { if (userRepository == null) { Injector injector = (Injector) resource.getAtmosphereConfig().getServletContext().getAttribute(Injector.class.getName()); injector.injectMembers(this); } if (broadcastConsumer == null) { broadcastConsumer = new WorkQueueRepository.BroadcastConsumer() { @Override public void broadcastReceived(JSONObject json) { if (broadcaster != null) { broadcaster.broadcast(json.toString()); } } }; this.workQueueRepository.subscribeToBroadcastMessages(broadcastConsumer); } broadcaster = resource.getBroadcaster(); }
@Override public void onSuspend(final AtmosphereResourceEvent event) { super.onSuspend(event); event.getResource().getBroadcaster().setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy.EMPTY_DESTROY); // OK, we can start polling Twitter! suspendLatch.countDown(); } }).build();
@Ready public void onReady(final AtmosphereResource resource) throws IOException { log.info("[websocket] Client {} connected to build {}", resource.uuid(), resource.getBroadcaster().getID()); java.util.UUID buildId = BuildSerializer.uuidFromString(resource.getBroadcaster().getID()); if (build == null || build.getId() == null) { build = hub.getOrCreateBuild(buildId); } else { if (!build.getId().equals(buildId)) { log.debug("[websocket] this should never happen, no ? build.uuid {} !~= {} resource.getBroadcaster().getID", buildId, resource.getBroadcaster().getID()); } } }
private void delayBatchBroadcast(final AtmosphereResource r, Long delay) { String uuid = r.uuid(); r.getBroadcaster().getBroadcasterConfig().getScheduledExecutorService().schedule(new Runnable() { @Override public void run() { synchronized (getMutex(uuid)) { List<JSONObject> messages = messagesForUuid.get(uuid); if (messages != null) { JSONObject batchMessage = new JSONObject(); JSONArray jsonMessages = new JSONArray(messages); batchMessage.put("data", jsonMessages); batchMessage.put("type", "batch"); r.getBroadcaster().broadcast(batchMessage.toString(), r); broadcastScheduledForUuid.remove(uuid); messages.clear(); } } } }, delay, TimeUnit.MILLISECONDS); broadcastScheduledForUuid.put(uuid, true); }
@Disconnect public final void onDisconnect(final AtmosphereResourceEvent event){ Collection<AtmosphereResource> atmosphereResources = event.getResource().getBroadcaster().getAtmosphereResources(); if (atmosphereResources.size() < 2 && subscription != null) { synchronized (subscription) { subscription.unsubscribe(); subscription = null; } LOG.debug("[websocket {}] [broadcaster:#{}] unsubscribed !", scopeAsAString, event.getResource().getBroadcaster().getID()); } if (event.isCancelled()) { LOG.info("[websocket {}] Client {} cancelled the connection", scopeAsAString, event.getResource().uuid()); } else if(event.isClosedByClient()) { LOG.info("[websocket {}] Client {} closed the connection", scopeAsAString, event.getResource().uuid()); } }
/** * Sends the given message to the current client. Cannot be called if * {@link #isConnected()} returns false. * * @param message * The message to send */ void sendMessage(String message) { assert (isConnected()); // "Broadcast" the changes to the single client only outgoingMessage = getResource().getBroadcaster().broadcast(message, getResource()); }
/** * Sends the given message to the current client. Cannot be called if * {@link #isConnected()} returns false. * * @param message * The message to send */ protected void sendMessage(String message) { assert (isConnected()); // "Broadcast" the changes to the single client only outgoingMessage = getResource().getBroadcaster().broadcast(message, getResource()); }
private void setStatus(AtmosphereResource resource, UserStatus status) { broadcaster = resource.getBroadcaster(); try { String authUserId = CurrentUser.getUserId(resource.getRequest()); checkNotNull(authUserId, "Could not find user in session"); User authUser = userRepository.findById(authUserId); checkNotNull(authUser, "Could not find user with id: " + authUserId); if (authUser.getUserStatus() != status) { LOGGER.debug("Setting user %s status to %s", authUserId, status.toString()); userRepository.setStatus(authUserId, status); this.workQueueRepository.pushUserStatusChange(authUser, status); } } catch (Exception ex) { LOGGER.error("Could not update status", ex); } finally { // TODO session is held open by getAppSession // session.close(); } }