【问题标题】:How to send websocket upgrade headers via php client如何通过 php 客户端发送 websocket 升级标头
【发布时间】: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


    【解决方案1】:

    $Headers 中可能需要 "Host:....\r\n" 和 "Origin:...\r\n"

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 2016-02-15
      • 2015-08-30
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 2017-09-30
      相关资源
      最近更新 更多