Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Session Playframework 2、WebSocket和会话_Session_Websocket_Playframework 2.0_Session Cookies - Fatal编程技术网

Session Playframework 2、WebSocket和会话

Session Playframework 2、WebSocket和会话,session,websocket,playframework-2.0,session-cookies,Session,Websocket,Playframework 2.0,Session Cookies,我知道在处理WebSocket时,我不能使用会话。playframework 2 websocket聊天示例将用户名添加到url中,但是如果我有一个已经登录的用户,并且我不想通过url传递他的用户名,那么有没有其他方法可以从websocket请求中获取用户登录凭据 以下是我迄今为止所尝试的: 请求页面,我将从其中连接到套接字: public static Result index(){ session("username", "mike") return ok(indexpage

我知道在处理WebSocket时,我不能使用会话。playframework 2 websocket聊天示例将用户名添加到url中,但是如果我有一个已经登录的用户,并且我不想通过url传递他的用户名,那么有没有其他方法可以从websocket请求中获取用户登录凭据

以下是我迄今为止所尝试的: 请求页面,我将从其中连接到套接字:

public static Result index(){
    session("username", "mike")
    return ok(indexpage.render());
}
以下是Websocket部分:

public static Websocket<JsonNode> chat(){
    System.out.println(session("username"));    
    ....all the websocket stuff
}
publicstaticwebsocketchat(){
System.out.println(会话(“用户名”);
……所有websocket的东西
}
我打印出来的都是空值
我做错了什么?

您可以访问该会话:

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.iteratee._

object Application extends Controller {
  def index = Action {
    Ok(views.html.index()).withSession("connected" -> "user@gmail.com")
  }

  //I used for views.html.index the html markup from http://www.websocket.org/echo.html
  def wstest = WebSocket.using[String] { request =>

    val in = Iteratee.consume[String]()

    val username = request.session.get("connected").getOrElse("missing username")

    val out = Enumerator("Hello " + username) //puts  Hello user@gmail.com on the screen

    (in, out)
  }
}
Redirect(.....).withSession("login" -> user)
现在,对于Java(控制器中的方法):


websocket连接不是Http。第一个握手请求看起来有点像http,但确实包含其他字段。这就是为什么不能在websocket请求中添加http cookie

来自维基百科:

握手类似于HTTP,因此服务器可以处理HTTP 连接以及同一端口上的WebSocket连接。然而 所涉及的具体字段以及握手后的操作 不符合HTTP协议


如果您不想在url中使用用户名,则可以在url中使用令牌/会话id;如果您无法在启动websocket连接之前实现登录/pw检索,则可以通过websocket通信实现您自己的登录/密码检索方法(例如,服务器要求以特定格式登录/pw,客户端js作出反应,收集并发送回服务器)。

允许访问会话,因为会话初始化为HTTP握手。 您可以通过将用户名密钥和值粘贴到会话来定义通过身份验证的操作的用户名密钥和值:

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.iteratee._

object Application extends Controller {
  def index = Action {
    Ok(views.html.index()).withSession("connected" -> "user@gmail.com")
  }

  //I used for views.html.index the html markup from http://www.websocket.org/echo.html
  def wstest = WebSocket.using[String] { request =>

    val in = Iteratee.consume[String]()

    val username = request.session.get("connected").getOrElse("missing username")

    val out = Enumerator("Hello " + username) //puts  Hello user@gmail.com on the screen

    (in, out)
  }
}
Redirect(.....).withSession("login" -> user)
然后:

object WebsocketController extends Controller {

  def wsConnection = WebSocket.async[JsValue] { request =>

    val userOption: Option[String] = request.session.get("login")

    //do your credential check or login ....

    (in, out)
  }

}

这确实是由playframework保护的

谢谢你的回复,很遗憾我在使用java,忘了提到这一点。这是我到目前为止尝试过的,无法添加代码来评论,我会做出回复。我已经尝试了你的建议,添加了“final Http.Session Session=Session(),但当我尝试从会话中提取用户名时,仍然会得到null。非常感谢您在这方面的帮助。如果您尝试“final String name=session”(“username”),则'在构建WebSocket之前?你确定你真的将名称存储到会话中了吗?Google Chrome可以显示你的cookie。你能在github上发布你的项目或一个简单的示例吗?Did'final String name=session(“用户名”);return new WebSocket(){…Logger.info(name)…为你工作吗Mike?我添加了一个带有测试的Java示例,请试试这个。