dronthpi05943 2013-05-21 14:49
浏览 311
已采纳

PHP:write_socket()发出意外警告

I'm trying to set up a WebSocket application, but I'm stuck at the following. After accepting the socket I received the client's headers as expected, but when I try to send the upgrade back socket_write() throws a warning:

Warning: socket_write(): unable to write to socket [10038]: An operation was attempted on something that is not a socket

This happens in the following piece of code:

var_dump($this->socket); //output: resource(2) of type (Socket)
socket_write($this->socket, $upgrade);

This happens inside a pthreads context.
What are possible reasons PHP is throwing me this warning?

The full code:

public function handshake($headers)
{
    Main::console($headers);
    Main::console("Getting client WebSocket version...");
    Main::console("Headers: 

".$headers);
    if(preg_match("/Sec-WebSocket-Version: (.*)
/", $headers, $match))
        $version = $match[1];
    else {
        Main::console("The client doesn't support WebSocket");
        return false;
    }

    Main::console("Client WebSocket version is {$version}, (required: 13)");
    if($version == 13) {
        // Extract header variables
        Main::console("Getting headers...");
        if(preg_match("/GET (.*) HTTP/", $headers, $match))
            $root = $match[1];
        if(preg_match("/Host: (.*)
/", $headers, $match))
            $host = $match[1];
        if(preg_match("/Origin: (.*)
/", $headers, $match))
            $origin = $match[1];
        if(preg_match("/Sec-WebSocket-Key: (.*)
/", $headers, $match))
            $key = $match[1];

        Main::console("Client headers are:

".
                        "- Root: ".$root."
".
                        "- Host: ".$host."
".
                        "- Origin: ".$origin."
".
                        "- Sec-WebSocket-Key: ".$key."
");

        Main::console("Generating Sec-WebSocket-Accept key...");
        $acceptKey = $key.'258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
        $acceptKey = base64_encode(sha1($acceptKey, true));

        $upgrade = "HTTP/1.1 101 Switching Protocols
".
                   "Upgrade: websocket
".
                   "Connection: Upgrade
".
                   "Sec-WebSocket-Accept: $acceptKey".
                   "

";

        Main::console("Sending this response to the client #{$this->getId()}:

".$upgrade);
        var_dump($this->socket);
        socket_write($this->socket, $upgrade, strlen($upgrade));
        $this->setHandshake(true);
        Main::console("Handshake is successfully done!");
        return true;
    }
    else {
        Main::console("WebSocket version 13 required (the client supports version {$version})");
        return false;
    }
}

public function run()
{
while($this->alive)
{
    $bytes = @socket_recv($this->socket, $buffer, 4096, MSG_WAITALL);
    if ($buffer)
        {
        if(!$this->handshake)
        {
            $this->handshake($buffer);
        } else {
            Main::console("Client {$this->getID()} says {$buffer}");
        }
    }
}

}

  • 写回答

1条回答 默认 最新

  • doujingjiao0015 2013-05-23 09:24
    关注

    Try socket_last_error() and socket_strerror() for more information. If that doesn't help, show us how the socket is created. Source: http://www.php.net/manual/en/function.socket-last-error.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch