2
我正在嘗試使用spray-can來設置一個非常基本的HTTP服務器。如果我爲端點設置了一個映射,我會得到一個超時(儘管使用調試器,我可以看到這個actor收到了這個消息)。爲什麼spray-can服務器不響應http請求?
我的來源是這樣的:
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.io.IO
import spray.can.Http
import spray.http.{HttpMethods, HttpRequest, HttpResponse, Uri}
class App extends Actor {
implicit val system = context.system
override def receive = {
case "start" =>
val listener: ActorRef = system.actorOf(Props[HttpListener])
IO(Http) ! Http.Bind(listener, interface = "localhost", port = 8080)
}
}
class HttpListener extends Actor {
def receive = {
case _: Http.Connected =>
sender() ! Http.Register(self)
case HttpRequest(HttpMethods.GET, Uri.Path("/ping"), _, _, _) =>
HttpResponse(entity = "PONG")
}
}
object Main {
def main(args: Array[String]) {
val system = ActorSystem("my-actor-system")
val app: ActorRef = system.actorOf(Props[App], "app")
app ! "start"
}
}
執行run
顯示:
> run
[info] Running Main
[INFO] [09/10/2014 21:33:38.839] [my-actor-system-akka.actor.default-dispatcher-3] [akka://my-actor-system/user/IO-HTTP/listener-0] Bound to localhost/127.0.0.1:8080
下HTTP/1.1 500 Internal Server Error
顯示出來時,我打http://localhost:8080/ping
:
➜ ~ curl --include http://localhost:8080/ping
HTTP/1.1 500 Internal Server Error
Server: spray-can/1.3.1
Date: Wed, 10 Sep 2014 19:34:08 GMT
Content-Type: text/plain; charset=UTF-8
Connection: close
Content-Length: 111
Ooops! The server was not able to produce a timely response to your request.
Please try again in a short while!
我build.sbt
是這樣的:
scalaVersion := "2.11.2"
resolvers += "spray repo" at "http://repo.spray.io"
libraryDependencies ++= Seq(
"io.spray" %% "spray-can" % "1.3.1",
"io.spray" %% "spray-routing" % "1.3.1",
"com.typesafe.akka" %% "akka-actor" % "2.3.5"
)
關於我在做什麼的錯誤?
當然,這是問題 - 非常感謝! – manub 2014-09-10 10:07:05
我不明白爲什麼Scala編譯器沒有抱怨HttpResponse在單元預期時返回。 – 2014-09-10 11:00:49
當預期類型爲Unit時,Ravi - Scala執行從任何值到Unit的隱式轉換。 - 從規範:價值拋棄。如果e具有某種值類型,並且預期類型爲Unit,則通過將e嵌入術語{e; ()} – Bryan 2014-09-10 20:35:12