2

我必须实现一个聊天模块来启用隐私聊天 b/w 用户。我必须在使用 Scala、Akka 和 java.net 的 Play 框架中执行此操作。*
我在网上有几个示例演示了 WebSockets 的使用,但我没有任何可以帮助我使用 WebSockets 实现聊天模块的示例。我知道我必须做什么,但我完全不知道对象、类的结构应该是什么以及我应该如何开始。
拜托,如果有人可以帮助我,或者给我推荐一篇好文章,可以帮助我完成实施的论文。谢谢你。

4

2 回答 2

3

我是用 Java 做的。这是我从示例中修改的内容:

public class ChatRoom extends UntypedActor {


//Added hashmap to keep references to actors (rooms).
// (might be put in another class)
public static HashMap<String,ActorRef> openedChats=new HashMap<String,ActorRef>();

//Added unique identifier to know which room join
final String chatId;


public ChatRoom(String chatId) {
    this.chatId = chatId;
}

public static void join(final User user, final String chatId , WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out) throws Exception{
    final ActorRef chatRoom;

    //Find the good room to bind to in the hashmap
    if(openedChats.containsKey(chatId)){
        chatRoom = openedChats.get(chatId);

    //Or create it and add it to the hashmap
    }else{
        chatRoom = Akka.system().actorOf(new Props().withCreator(new UntypedActorFactory() {
              public UntypedActor create() {
                return new ChatRoom(chatId);
              }
            })
        );
        openedChats.put(chatId,chatRoom);
    }

    // Send the Join message to the room
    String result = (String)Await.result(ask(chatRoom,new Join(user.getId()+"", out), 10000), Duration.create(10, SECONDS));

    // ..... Nothing to do in the rest

这只是主要的修改,您还必须调整 javascript 和路由文件

随意问的问题。

于 2013-11-05T13:03:32.680 回答
2

看看playframework中的官方示例

https://github.com/playframework/playframework/tree/master/samples/scala/websocket-chat

于 2013-10-29T09:35:08.950 回答