I also asked this question
in http://stackoverflow.com/questions/35168660/akka-java-tcp-chatroom
I tried to extend the code about TCP in doc to make a simple chat room,
simply put, several clients connected to server, one client send a string,
server broadcast the string to all clients...the code I wrote showed below,
it doesn't work, can someone tell why?
the program behave wrong, say 2 clients connected to server, one send a
message, then the sender get 2 messages back, the other get nothing...
Main.java
public class Main {
public static void main(String[] args) {
akka.Main.main(new String[] { Server.class.getName()});
}}
Server.java
public class Server extends UntypedActor {
final ActorRef manager = Tcp.get(getContext().system()).manager();
public static Props props(ActorRef manager) {
return Props.create(Server.class, manager);
}
@Override
public void preStart() throws Exception {
final ActorRef tcp = Tcp.get(getContext().system()).manager();
tcp.tell(TcpMessage.bind(getSelf(), new InetSocketAddress("0.0.0.0", 8888),
100), getSelf());
}
@Override
public void onReceive(Object msg) throws Exception {
if (msg instanceof Bound) {
manager.tell(msg, getSelf());
} else if (msg instanceof CommandFailed) {
getContext().stop(getSelf());
} else if (msg instanceof Connected) {
final Connected conn = (Connected) msg;
manager.tell(conn, getSelf());
final ActorRef handler =
getContext().actorOf(Props.create(SimplisticHandler.class));
getContext().system().eventStream().subscribe(handler,
Notification.class);
getSender().tell(TcpMessage.register(handler), getSelf());
}
}
}
SimplisticHandler.java
public class SimplisticHandler extends UntypedActor {
@Override
public void onReceive(Object msg) throws Exception {
if (msg instanceof Received) {
final ByteString data = ((Received) msg).data();
System.out.println(data);
getContext().system().eventStream().publish(new Notification(getSender(),
getSelf(), 1, data));
} else if (msg instanceof ConnectionClosed) {
getContext().stop(getSelf());
} else if (msg instanceof Notification) {
Notification noti = (Notification)msg;
// TODO while the below statement don't broadcast ?
if (noti.id == 1)
noti.sender.tell(TcpMessage.write((ByteString)(noti.obj)), getSelf());
}
}}
Notification.java
public class Notification {
public final ActorRef sender;
public final ActorRef receiver;
public final int id;
public final Object obj;
public Notification(ActorRef sender, ActorRef receiver, int id, Object obj)
{
this.sender = sender;
this.receiver = receiver;
this.id = id;
this.obj = obj;
}}
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.