【发布时间】:2021-08-09 02:13:24
【问题描述】:
我正在尝试升级我的 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!");
});
【问题讨论】:
标签: php http websocket http-status-code-400 handshake