问题
I have session variables stored on the client. (in this example, session var is "username" with value "Foo") I need the server to know that the new connection that just loaded in has the name "Foo." is there any way to have the server know the value of a session variable?
回答1:
No, you cannot do that. Sessions are stored in the cookies and Ratchet cannot access them. However, I use this approach when I am working with web-sockets:
Create a hash when the user logins into the account and store it in the database to identify the user with it.
Connect to the Ratchet web-socket and then send an initialize message with the value of the hash (easy, just echo the hash into the JavaScript or load it through Ajax).
If the hash is valid at the Ratchet end, good. Otherwise, disconnect the user.
来源:https://stackoverflow.com/questions/55094719/how-do-you-send-data-to-the-server-onload-using-ratchet-websockets