如何使用棘轮式Websocket将数据发送到服务器onload? [英] How do you send data to the server onload using ratchet websockets?

查看:90
本文介绍了如何使用棘轮式Websocket将数据发送到服务器onload?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在客户端上存储了会话变量.(在此示例中,会话var是值为"Foo"的用户名")我需要服务器知道刚加载的新连接的名称为"Foo".有什么办法让服务器知道会话变量的值?

解决方案

不,您不能这样做.会话存储在cookie中,而Ratchet无法访问它们.但是,在使用网络套接字时,我会使用这种方法:

  1. 当用户登录帐户时创建一个哈希并将其存储在数据库中以使用它来识别用户.

  2. 连接到Ratchet网络套接字,然后发送带有哈希值的初始化消息(很简单,只需将哈希回显到JavaScript或通过Ajax加载即可)./p>

  3. 如果哈希值在棘轮端有效,则很好.否则,请断开用户连接.

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.

这篇关于如何使用棘轮式Websocket将数据发送到服务器onload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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