【发布时间】:2013-05-12 23:00:48
【问题描述】:
我正在尝试学习如何在 Play 2.1 中使用网络套接字,但我无法让网络套接字 URL 与我的应用程序的路由配置一起使用。我从一个新的 Play 应用程序和Play framework documentation on websockets开始。
这是我的conf/routes:
# Home page
GET / controllers.Application.index
# Websocket test site
GET /wstest controllers.Application.wstest
然后我将wstest 函数添加到我的控制器类中:
object Application extends Controller {
def index = Action {
Ok(views.html.index("Websocket Test"))
}
def wstest = WebSocket.using[String] { request =>
// Log events to the console
val in = Iteratee.foreach[String](println).mapDone { _ =>
Logger.info("Disconnected")
}
// Send a single 'Hello!' message
val out = Enumerator("Hello!")
(in, out)
}
}
但是,到目前为止,我只能使用 URL ws://localhost:9000/wstest 访问 websocket(使用 websocket.org/echo.html 的示例代码)。我在看Play框架自带的sample/scala/websocket-chat应用,它使用路由配置文件来引用websocket,像这样:
var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
var chatSocket = new WS("@routes.Application.chat(username).webSocketURL()")
我尝试用@routes.Application.wstest.webSocketURL() 和@routes.Application.wstest 替换我的websocket URL。第一个没有编译。第二个编译,但客户端和服务器不交换任何消息。
如何使用我的 Play 路由配置来访问这个 websocket?我在这里做错了什么?
编辑
这是我的编译错误截图,“Cannot find any HTTP Request Header here”:
【问题讨论】:
标签: scala playframework websocket playframework-2.0