LoginSignup
2
2

More than 5 years have passed since last update.

scalatraでAtmosphereのexampleを動かしたかった...

Last updated at Posted at 2014-07-29

環境構築

  • JDKインストール
  • giter8インストール

参考:PHPerだが、Scalaのフレームワーク「Scalatra」を使ってみる

新規プロジェクト作成

$ g8 scalatra/scalatra-sbt

色々聞かれるがとりあえず全て空Enter

起動ポートを3000に変更

project/build.scala
 project/build.scala | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/project/build.scala b/project/build.scala
index 01af59d..0e8a0c9 100644
--- a/project/build.scala
+++ b/project/build.scala
@@ -4,6 +4,8 @@ import org.scalatra.sbt._
 import org.scalatra.sbt.PluginKeys._
 import com.mojolly.scalate.ScalatePlugin._
 import ScalateKeys._
+import com.earldouglas.xsbtwebplugin.PluginKeys._
+import com.earldouglas.xsbtwebplugin.WebPlugin._

 object MyScalatraWebAppBuild extends Build {
   val Organization = "com.example"
@@ -16,6 +18,7 @@ object MyScalatraWebAppBuild extends Build {
     "my-scalatra-web-app",
     file("."),
     settings = ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
+      port in container.Configuration := 3000,
       organization := Organization,
       name := Name,
       version := Version,
-- 
1.7.11.1

参考:scalatra のポート番号を変更する

Atmosphere

ここに載っている通り project/build.scala にDependencyを追加。
MyScalatraServlet.scalaを書き換える。

$ ./sbt
> container:start
[info] Compiling 1 Scala source to /home/masa/my-scalatra-web-app/target/scala-2.11/classes...
[error] /home/masa/my-scalatra-web-app/src/main/scala/com/example/app/MyScalatraServlet.scala:14: class MyScalatraServlet needs to be abstract, since method jsonFormats in trait JValueResult of type => org.json4s.Formats is not defined
[error] class MyScalatraServlet extends MyScalatraWebAppStack 
[error]       ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 8 s, completed 2014/07/30 1:00:23

なんかエラーが出た!
class内に下記1行を追加すると解決するっぽい。

MyScalatraServlet.scala
+  implicit protected val jsonFormats: Formats = DefaultFormats

ここまでの過程は下記リポジトリに置いてあります。
https://github.com/letsspeak/my-scalatra-web-app/

よく見たらexampleの正規品があった

GitHub scalatra-atmosphere-example

git clone git@github.com/scalatra/scalatra-website-examples

上記と同様に起動ポートを3000に変更して...container:start

スクリーンショット 2014-07-30 1.27.39.png

なんか動いた!!
ちなみにApacheを噛ませるとやっぱりWebsocketのエラーが出る。

01:30:08.269 [qtp261964679-39] ERROR o.a.cpr.AsynchronousProcessor - Invalid request state. Websocket protocol not supported

おまけ

Apacheのリバースプロキシの設定

/etc/httpd/conf/httpd.conf
<VirtualHost *:80>
    ServerName yourdomain.com
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

Vim用の設定ファイル

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2