大佬教程收集整理的这篇文章主要介绍了php – 如何将$_SESSION变量传递给websocket服务器?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个websocket服务器和一个在我的本地机器上一起运行的Web服务器.
当客户端使用浏览器API’new WebSocket(“ws:// localhost”)’连接到它时,我需要将$_SESSION数据传递给websocket服务器(请求使用反向代理发送到websocket,后者知道它当收到带有“升级”标题的请求时.
关键是客户端成功连接到ws服务器,但我还需要使用http Web服务器设置的$_SESSION变量来恢复其SESSION数据.
use Ratchet\Server\IoServer; use Ratchet\http\httpServer; use Ratchet\WebSocket\WsServer; use MyApp\MyAppClassChat; require dirname(__DIR__) . '/vendor/autoload.@L_874_6@'; $server = IoServer::factory(new httpServer(new WsServer(new MyAppClass())),8080); $server->run();@H_876_1@myAppClass非常简单:
<?@L_874_6@ namespace MyAppClass; use Ratchet\messageComponenTinterface; use Ratchet\ConnectionInterface; class MyAppClass implements messageComponenTinterface { protected $clients; public function __construct() { $this->clients = new \SplObjectStorage; } public function onOpen(ConnectionInterface $conn) { /* I would like to put recover the session infos of the clients here but the session_start() call returns an empty array ($_SESSION variables have been previuosly set by the web server)*/ session_start(); var_dump($_SESSION) // empty array... echo "New connection! ({$conn->resourcEID})\n"; } public function onmessage(ConnectionInterface $from,$msg) { $numberOfReceivers = count($this->clients) -1; echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n",$from->resourcEID,$msg,$numberOfReceivers,$numberOfReceivers == 1 ? '' : 's'); $this->clients->rewind(); while ($this->clients->valid()) { $client = $this->clients->current(); if ($client !== $from) { $client->send($msg); } $this->clients->next(); } } public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); echo "Connection {$conn->resourcEID} has disconnected\n"; } public function onError(ConnectionInterface $conn,\Exception $E) { echo "An error has occurred: {$e->getmessage()}\n"; $conn->close(); } }
有没有办法用我的实际布局来做到这一点,或者我应该配置apache才能使用@H_165_16@mod_proxy_wstunnel模块?
感谢帮助!!!
Apache服务器的index.html启动会话:
<?@L_874_6@ // Get the session ID. $ses_id = session_id(); if (empty($ses_id)) { session_start(); $ses_id = session_id(); } ?><!DOCTYPE html> ...
Ratchet messageComponenTinterface代码访问会话令牌:
public function onmessage(ConnectionInterface $from,$msg) { $sessionId = $from->WebSocket->request->getCookies()['@L_874_6@SESSID']; # Do stuff with the token... }
一旦两个服务器都知道用户的会话令牌,他们就可以使用令牌通过MysqL数据库共享信息(这就是我所做的):
# Access session data from a database: $stmt = $this->MysqLi->prepare("SELECT * FROM users WHERE cookie=?"); $stmt->bind_param('s',$sessionId); $stmt->execute(); $result = $stmt->get_result();
或者,您可以进行更奇特的进程间通信:
# Ratchet server: $opts = array( 'http'=>array( 'method'=>'GET','header'=>"Cookie: @L_874_6@SESSID=$sessionId\r\n" ) ); $context = stream_context_create($opts); $json = file_get_contents('http://localhost:80/get_session_info.@L_874_6@',false,$context); $session_data = json_decode($json); # Apache server's get_session_info.@L_874_6@ # Note: reStrict access to this path so that remote users can't dump # their own session data. echo json_encode($_SESSION);
以上是大佬教程为你收集整理的php – 如何将$_SESSION变量传递给websocket服务器?全部内容,希望文章能够帮你解决php – 如何将$_SESSION变量传递给websocket服务器?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。