我正在使用Scalatra Atmosphere的websocket,如下所示:
package com.sweetsoft.app
import org.scalatra._
import org.json4s.JsonDSL._
import org.json4s._
import org.scalatra._
import org.scalatra.atmosphere._
import org.scalatra.json.{JValueResult, JacksonJsonSupport}
import org.scalatra.scalate.ScalateSupport
import scala.concurrent.ExecutionContext.Implicits.global
import java.util.Date
class SapBroker extends ScalatraServlet
with ScalateSupport with JValueResult
with JacksonJsonSupport with SessionSupport
with AtmosphereSupport {
implicit protected val jsonFormats: Formats = DefaultFormats
get("/") {
contentType="text/html"
}
atmosphere("/broker") {
new AtmosphereClient {
def receive: AtmoReceive = {
case Connected =>
println("Client %s is connected" format uuid)
broadcast(("author" -> "Someone") ~ ("message" -> "joined the room") ~ ("time" -> (new Date().getTime.toString )), Everyone)
case Disconnected(ClientDisconnected, _) =>
println("Client %s is connected" format uuid)
case Disconnected(ServerDisconnected, _) =>
println("Server disconnected the client %s" format uuid)
case JsonMessage(json) =>
println("Server disconnected the client %s" format uuid)
}
}
}
error {
case t: Throwable => t.printStackTrace()
}
}
服务器运行无任何错误,但是我尝试使用websocket客户端连接服务器,如下所示:
如您所见,它无法连接到服务器。但是在控制台上,它显示:
2019-03-15 21:24:25.466:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@1c53fd30{/,file:///home/developer/scala/sapbroker/target/webapp/,AVAILABLE}{file:///home/developer/scala/sapbroker/target/webapp/}
2019-03-15 21:24:25.474:INFO:oejs.AbstractConnector:main: Started ServerConnector@5dda6f9{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2019-03-15 21:24:25.475:INFO:oejs.Server:main: Started @1611ms
21:24:25.698 [Thread-16] INFO o.atmosphere.cpr.AtmosphereFramework - Latest version of Atmosphere's JavaScript Client 2.3.8
21:24:25.698 [Thread-16] INFO o.atmosphere.cpr.AtmosphereFramework -
Atmosphere Framework Updates:
Major Update available (new features): 2.5.2
Client 67262482-3527-4522-babf-e9b3f932eb14 is connected
Client 67262482-3527-4522-babf-e9b3f932eb14 is connected
客户端已连接,但是Smart Websocket客户端说,它无法连接到服务器。 怎么了?