php - 如何通过 php 客户端发送 websocket 升级标头
问题描述
我正在尝试升级我的 php 客户端以使用 websockets,但是,我收到错误:400 Bad Request
发送标头时。(我的 ws 服务器在 node.js 中)
消息是输出的,$buf
因为这是服务器发回的内容......(我认为)
我已经缩小了代码,所以它很容易阅读,我的真实代码有多个错误检查;)
完整错误:
HTTP/1.1 400 Bad Request
Connection: close
Content-Type: text/html
Content-Length: 11
客户
<?php
$host = "localhost";
$port = 8000;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, $host, $port));
$headers = "GET / HTTP/1.1\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Key: i9riAfOgSsKwUlmLjIkGA==\r\n\r\n";
socket_write($sock, $headers, strlen($headers));
socket_recv($sock, $buf, 2045, MSG_WAITALL);
echo $buf;
简单的服务器
import WebSocket from "ws";
const wss = new WebSocket.Server({ port: 8000 });
wss.on("connection", (ws) => {
console.log("connection recieved!");
});
解决方案
$Headers 中可能需要 "Host:....\r\n" 和 "Origin:...\r\n"
推荐阅读
- python - 如何创建excel文件,然后通过数据框读取文件?
- python - 如何在python timerotatingfilehandler中将翻转时间作为积分点
- yaml-cpp - Jaegerw 与 yaml-cpp 链接错误
- sql-server - SQL Server 2012 批量加载权限已设置,但仍然出现错误
- c# - 为什么我的带有标签的图片框在保存时被移动
- r - 将我自己的函数添加到现有包中
- python - autograder 应该如何传递自己的文本文件?
- rust - 迭代 Rust Vec 的首选方法?
- python - flattened dictionary into nested dictionary of dictionaries of lists
- javascript - 如何将全栈应用程序部署到 heroku 或 netlify?哪些文件是必需的?