播放框架和akka到实时套接字更新

时间:2014-07-14 22:09:45

标签: playframework websocket akka playframework-2.3

我是一个新的游戏框架,我已经创建了一个轻松管理帖子和评论的应用程序。现在作为概念证明,我想实现一个websocket来在页面中推送实时评论(添加,更新和删除)。

我的想法是在我的控制器中的休息动作中发送更新消息但是,我无法弄清楚最佳方式(akka?)

EDIT。  目前我会和Akka一起去,但是我不知道如何设置一个基本的POC。

我用:

更改了我的Application.java
package controllers;

import play.*;
import play.mvc.*;

import views.html.*;
import java.util.*;

import akka.MyWebSocketActor;
import play.libs.F.*;
import play.mvc.WebSocket;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }

    // ///////////////////////////////////////////////////////////////////////////
    //
    // Socket and Actors definition 
    //
    // ///////////////////////////////////////////////////////////////////////////
    public static WebSocket<String> socket() {
        return WebSocket.withActor(MyWebSocketActor::props);
    }
    // ///////////////////////////////////////////////////////////////////////////
    //
    // Socket and Actors definition 
    //
    // ///////////////////////////////////////////////////////////////////////////

}

我已经写了一个简单的MyWebSocketActor.java:

package akka;

import akka.actor.*;


public class MyWebSocketActor extends UntypedActor {

    public static Props props(ActorRef out) {
        return Props.create(MyWebSocketActor.class, out);
    }

    private final ActorRef out;

    public MyWebSocketActor(ActorRef out) {
        this.out = out;
    }

    public void onReceive(Object message) throws Exception {
        if (message instanceof String) {
            out.tell("I received your message: " + message, self());
        }
    }
}

不幸的是我收到了编译错误:

! @6ipf58l8c - Internal server error, for (GET) [/] ->

play.PlayExceptions$CompilationException: Compilation error[error: cannot find symbol]
    at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14$$anonfun$apply$16.apply(PlayReloader.scala:304) ~[na:na]
    at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14$$anonfun$apply$16.apply(PlayReloader.scala:304) ~[na:na]
    at scala.Option.map(Option.scala:145) ~[scala-library-2.10.3.jar:na]
    at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14.apply(PlayReloader.scala:304) ~[na:na]
    at play.PlayReloader$$anon$1$$anonfun$reload$2$$anonfun$apply$14.apply(PlayReloader.scala:298) ~[na:na]
    at scala.Option.map(Option.scala:145) ~[scala-library-2.10.3.jar:na]
    at play.PlayReloader$$anon$1$$anonfun$reload$2.apply(PlayReloader.scala:298) ~[na:na]
    at play.PlayReloader$$anon$1$$anonfun$reload$2.apply(PlayReloader.scala:296) ~[na:na]
    at scala.util.Either$LeftProjection.map(Either.scala:377) ~[scala-library-2.10.3.jar:na]
    at play.PlayReloader$$anon$1.reload(PlayReloader.scala:296) ~[na:na]
    at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:104) ~[play_2.10-2.2.3.jar:2.2.3]
    at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:102) ~[play_2.10-2.2.3.jar:2.2.3]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) ~[scala-library-2.10.3.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) ~[scala-library-2.10.3.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1361) ~[scala-library-2.10.3.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library-2.10.3.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library-2.10.3.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library-2.10.3.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library-2.10.3.jar:na]

这似乎与:

有关
    return WebSocket.withActor(MyWebSocketActor::props);
                    ^

在Application.java文件中。 我错过了什么?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用akka actor来强制websocket进行实时更新。

Play Web Sockets

除此之外,还有一个由typesafe提供的聊天模板。你可以从中得到这个想法:

Chat Template