All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.sitebricks.example.Chatter Maven / Gradle / Ivy

The newest version!
package com.google.sitebricks.example;

import com.google.sitebricks.At;
import com.google.sitebricks.Show;
import com.google.sitebricks.channel.ChannelListener;
import com.google.sitebricks.channel.Observe;
import com.google.sitebricks.channel.Switchboard;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author [email protected] (Dhanji R. Prasanna)
 */
@At("/chat") @Show("Chatter.html") @Singleton
public class Chatter {
  private final List connected = new CopyOnWriteArrayList();

  @Inject
  private Switchboard switchboard;

  @Observe
  public void receiveChat(String msg) {
    System.out.println("Chat msg received: " + msg);

    for (String socketId : connected) {
      switchboard.named(socketId).send("rebound from server: " + msg + " =)");
    }
  }

  @Singleton
  public static class ChatterListener implements ChannelListener {
    @Inject
    private Chatter chatter;

    @Override
    public void connected(Switchboard.Channel channel) {
      System.out.println("Channel connected with name: " + channel.getName());
      chatter.connected.add(channel.getName());
    }

    @Override
    public void disconnected(Switchboard.Channel channel) {
      System.out.println("Channel disconnected with name: " + channel.getName());
      chatter.connected.remove(channel.getName());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy