如何通过php客户端发送websocket升级头



我正在尝试升级我的php客户端以使用websocket,但是,在发送标头时收到错误: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.1rn";
$headers .= "Upgrade: websocketrn";
$headers .= "Connection: Upgradern";
$headers .= "Sec-WebSocket-Version: 13rn";
$headers .= "Sec-WebSocket-Key: i9riAfOgSsKwUlmLjIkGA==rnrn";
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!");
});

可能"主持人:。。。。\\r\n";以及";来源:。。。\\r\n";在$Headers 中要求

相关内容

  • 没有找到相关文章

最新更新