如何实施';私人聊天';在play框架中使用akka、scala、WebSocket的模块?
我必须实现一个聊天模块来启用与用户的隐私聊天。我必须使用Scala、Akka和java.net在Play框架中完成这项工作。*如何实施';私人聊天';在play框架中使用akka、scala、WebSocket的模块?,scala,networking,playframework,websocket,akka,Scala,Networking,Playframework,Websocket,Akka,我必须实现一个聊天模块来启用与用户的隐私聊天。我必须使用Scala、Akka和java.net在Play框架中完成这项工作。* 我在网上有几个例子演示了WebSockets的使用,但没有任何例子可以帮助我使用WebSockets实现聊天模块。我知道我必须做什么,但我完全不知道对象、类的结构应该是什么,以及应该如何开始。 请,如果有人能帮我做这件事,或者给我推荐一篇好文章,这篇论文可以帮助我一路实现。谢谢。看看playframework中的官方示例 我是用Java做的。这是我从示例中修改的内容:
我在网上有几个例子演示了WebSockets的使用,但没有任何例子可以帮助我使用WebSockets实现聊天模块。我知道我必须做什么,但我完全不知道对象、类的结构应该是什么,以及应该如何开始。
请,如果有人能帮我做这件事,或者给我推荐一篇好文章,这篇论文可以帮助我一路实现。谢谢。看看playframework中的官方示例
我是用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
public class聊天室扩展了UntypedActor{
//添加了hashmap以保留对参与者(房间)的引用。
//(可以放在另一个类中)
public static HashMap openedChats=new HashMap();
//添加了唯一标识符以了解要加入的文件室
最后一个字符串chatId;
公共聊天室(字符串聊天ID){
this.chatId=chatId;
}
公共静态void连接(最终用户用户、最终字符串chatId、WebSocket.In、WebSocket.Out)抛出异常{
最后的ActorRef聊天室;
//在hashmap中找到要绑定到的好空间
if(openedChats.containsKey(chatId)){
chatRoom=openedChats.get(chatId);
//或者创建它并将其添加到hashmap
}否则{
聊天室=Akka.system().actorOf(新道具().withCreator(新的非类型DactorFactory)(){
公共非类型转换器create(){
返回新聊天室(chatId);
}
})
);
openedChats.put(聊天室,聊天室);
}
//将加入消息发送到会议室
stringresult=(String)wait.result(ask(聊天室,新加入(user.getId()+“”,out),10000),Duration.create(10秒));
//……剩下的时间里没什么事可做
这只是主要的修改,您还必须适应javascript和路由文件
请随意提问。先生,我已经看过了,但这不是我想要的。这是一个简单的聊天室的例子,任何人都可以登录聊天室并在聊天室中聊天。我必须实现一个私人的(相互的)通过特定端口上的WebSocket连接的两个客户端进行b/w聊天。创建一个仅支持两人加入的新聊天室有多难?是否可以共享完整的代码?提前感谢