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

☆樱花仙子☆ 提交于 2021-01-29 12:07:02

问题


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:

  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.



来源:https://stackoverflow.com/questions/55094719/how-do-you-send-data-to-the-server-onload-using-ratchet-websockets

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