public void sendNotification(Notification notification, int id) { ServerSession remoteClient = retrieveSessionFromId(id); remoteClient.deliver(serverSession, "/person/notification", notification); }
private void configureRoom(String roomName) { final String channelName = "/" + roomName + "/members"; ServerChannel channel = bayeuxServer.createChannelIfAbsent(channelName).getReference(); channel.addListener(new SubscriptionListener() { @Override public void subscribed(ServerSession session, ServerChannel channel, ServerMessage message) { // Send back the whole list to the subscriber. session.deliver(sender, channelName, memberList); } });
public class EchoService extends AbstractService { public EchoService(BayeuxServer bayeuxServer) { super(bayeuxServer, "echo"); addService("/echo", "processEcho"); } public void processEcho(ServerSession remote, Map<String, Object> data) { // if you want to echo the message to the client that sent the message remote.deliver(getServerSession(), "/echo", data, null); // if you want to send the message to all the subscribers of the "/myChannel" channel getBayeux().createIfAbsent("/myChannel"); getBayeux().getChannel("/myChannel").publish(getServerSession(), data, null); } }
/** * @param sender the session delivering the message * @param message the message to deliver * @deprecated use {@link #deliver(Session, ServerMessage.Mutable, Promise)} instead */ @Deprecated public default void deliver(Session sender, ServerMessage.Mutable message) { deliver(sender, message, Promise.noop()); }
/** * @param sender the session delivering the message * @param channel the channel of the message * @param data the data of the message * @deprecated use {@link #deliver(Session, String, Object, Promise)} instead */ @Deprecated public default void deliver(Session sender, String channel, Object data) { deliver(sender, channel, data, Promise.noop()); }
@Service public class GetPlayer { @Session private LocalSession sender; @Listener("/service/player/get") public void perform(ServerSession session, ServerMessage message) { Map<String, Object> player = retrievePlayerInfo(message.get("playerId")); session.deliver(sender, message.getChannel(), player); } }
/** * <p>Sends data to an individual remote client.</p> * <p>The data passed is sent to the client * as the "data" member of a message with the given channel. The * message is not published on the channel and is thus not broadcast to all * channel subscribers, but instead delivered directly to the target client.</p> * <p>Typically this method is only required if a service method sends * response(s) to clients other than the sender, or on different channels. * If the response is to be sent to the sender on the same channel, * then the data can simply be the return value of the method.</p> * * @param toClient The target client * @param onChannel The channel of the message * @param data The data of the message */ protected void send(ServerSession toClient, String onChannel, Object data) { toClient.deliver(_session.getServerSession(), onChannel, data, Promise.noop()); }
private boolean deliver(Object data, boolean successful) { boolean completed = complete.compareAndSet(false, true); if (completed) { ServerMessage.Mutable message = bayeux.newMessage(); message.setId(messageId); message.setSuccessful(successful); message.setChannel(channel); message.setData(data); session.deliver(sender, message, Promise.noop()); } return completed; } }