2013-10-29 27 views
2

我必須實現一個聊天模塊來啓用隱私聊天黑白用戶。我必須在使用Scala,Akka和java.net的Play框架中執行此操作。*
我已經通過網絡獲得了幾個示例,這些示例演示了使用WebSockets但我沒有任何可以幫助我實現Chat模塊的示例WebSockets的。我有我必須做的想法,但我對什麼應該是對象,類的結構以及我應該如何開始感到困惑。
如果有人可以幫我解決這個問題,或者給我推薦一篇好文章,這篇文章可以幫助我完成整個實施過程。謝謝。如何在遊戲框架中使用akka,scala,websockets實現'Private Chat'模塊?

回答

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和路由文件

隨意問的問題。

+1

是否可以共享完整的代碼?提前致謝 – enigma969