php - PHP中的websocket是否存在踩踏?
问题描述
我使用 Node.js 在 WebSocket 上运行 STOMP,因为它支持自定义标头。当 HTTP 握手时,服务器需要一个 cookie 来检查身份验证,而不是地址中的令牌。
作为PHPer,我想在PHP中使用这个,但是google了半天也没找到。谁能帮忙?
解决方案
STOMP 不限于 Node.js,或者 Java 等。有
- PHP的STOMP PECL 扩展,
- PHP STOMP 库,例如Stomp-PHP,
- 以及支持 STOMP 的通用消息传递库,
例如Enqueue的STOMP 传输层或Laravel-Queue
关于您的具体情况,如果不了解更多详细信息,很难直接给出答案,但这里有一个来自 stomp-php 库示例帽子的示例,它设置了一个自定义标头:
use Stomp\Client; use Stomp\SimpleStomp; use Stomp\Transport\Map; // make a connection $client = new Client('tcp://localhost:61613'); $stomp = new SimpleStomp($client); // send a message to the queue $body = array('city' => 'Belgrade', 'name' => 'Dejan'); $header = array(); $header['transformation'] = 'jms-map-json'; $mapMessage = new Map($body, $header); $client->send('/queue/test', $mapMessage); echo 'Sending array: '; print_r($body); $stomp->subscribe('/queue/test', 'transform-test', 'client', null, ['transformation' => 'jms-map-json']); /** @var Map $msg */ $msg = $stomp->read(); // extract if ($msg != null) { echo 'Received array: '; print_r($msg->map); // mark the message as received in the queue $stomp->ack($msg); } else { echo "Failed to receive a message\n"; }
推荐阅读
- python - 列表冲突不起作用
- python-3.x - 我得到没有名为“oauth2._compat”的模块
- angular - Angular 5 访问包含 Observable 的字段
- json - 我需要将 JSON 发布到 API 端点,但我找不到任何“请求有效负载”示例
- python - 如何在 Python 中生成异方差模型?
- javascript - 似乎无法在语义 UI 提要摘要中调整图像大小
- css - React Native:模态与根组件重叠
- powershell - PowerShell 浮动到字符串行为
- c++ - 格式化表格显示的最佳方法?
- javascript - 获取文档时出错:TypeError:无法设置未定义的属性“userexist”