Starting a session within a ratchet websocket connection

落花浮王杯 提交于 2019-12-01 04:40:21

问题


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.

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();
    }
    ...

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

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.

Am I missing something?


回答1:


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.



来源:https://stackoverflow.com/questions/16183917/starting-a-session-within-a-ratchet-websocket-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!