在棘轮 websocket 连接中启动会话 [英] Starting a session within a ratchet websocket connection

查看:46
本文介绍了在棘轮 websocket 连接中启动会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经构建了一个 Ratchet websocket 服务器,它为一个独立的客户端应用程序提供服务.服务器在端口 8080 上运行,而我们的 Symfony 应用程序在端口 80 上运行.我们在 websocket 服务器中运行会话非常重要,这样我们就可以唯一地识别每个客户端.问题是 Ratchet 不会通过端口 8080 发送 set-cookie 标头.

We have built a Ratchet websocket server that serves a standalone client application. The server is running on port 8080, and our Symfony app is running on port 80. It's critical that we have sessions working within the websocket server so we can uniquely identify each client. The problem is that Ratchet does not send a set-cookie header over port 8080.

希望找到一种方法来响应 8080 上的升级请求发送 set-cookie 标头,我尝试在 onOpen() 中启动一个新会话:

Hoping to find a way to send the set-cookie header in response to the upgrade request on 8080, I tried to start a new session in onOpen():

use Symfony\Component\HttpFoundation\Session\Session;

class ClientApi implements MessageComponentInterface {
    /**
     * @inheritDoc
     */
    public function onOpen(ConnectionInterface $conn) {
        $conn->Session = new Session();
        $conn->Session->start();
    }
    ...

但这并没有导致在对升级请求的响应中发回 set-cookie 标头.

But this did not result in a set-cookie header being sent back in the response to the upgrade request.

我们有一个解决方法,客户端必须首先在端口 80 上执行 GET 以获取 cookie,然后才能通过 8080 发送 websocket 升级请求,因此它可以随升级请求一起发送 cookie.我希望能像上面尝试的那样工作,这样客户端就不必打扰端口 80 上的网络应用程序.

We have a workaround where the client has to first do a GET on port 80 to get a cookie before sending the websocket upgrade request over 8080, so it can send the cookie with the upgrade request. I'm hoping to get something working like I tried above, so the client does not have to bother the web app on port 80.

我错过了什么吗?

推荐答案

我相信默认情况下创建一个新的 Symfony Session 类将使用全局变量.Ratchet 提供了一个 Session 中间件类,它将把一个 Symfony Session 对象附加到每个 ConnectionInterface.

I believe by default creating a new Symfony Session class will use global variables. Ratchet provides a Session middleware class that will attach a Symfony Session object to each ConnectionInterface.

这篇关于在棘轮 websocket 连接中启动会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆