简体   繁体   中英

How do you send data to the server onload using ratchet websockets?

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?

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:

  1. Create a hash when the user logins into the account and store it in the database to identify the user with it.

  2. 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).

  3. If the hash is valid at the Ratchet end, good. Otherwise, disconnect the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM