sockets - PHP: write_socket() giving unexpected warning -
i'm trying set websocket application, i'm stuck @ following. after accepting socket received client's headers expected, when try send upgrade socket_write()
throws warning:
warning: socket_write(): unable write socket [10038]: operation attempted on not socket
this happens in following piece of code:
var_dump($this->socket); //output: resource(2) of type (socket) socket_write($this->socket, $upgrade);
this happens inside pthreads context.
possible reasons php throwing me warning?
the full code:
public function handshake($headers) { main::console($headers); main::console("getting client websocket version..."); main::console("headers: \r\n\r\n".$headers); if(preg_match("/sec-websocket-version: (.*)\r\n/", $headers, $match)) $version = $match[1]; else { main::console("the client doesn't support websocket"); return false; } main::console("client websocket version {$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: (.*)\r\n/", $headers, $match)) $host = $match[1]; if(preg_match("/origin: (.*)\r\n/", $headers, $match)) $origin = $match[1]; if(preg_match("/sec-websocket-key: (.*)\r\n/", $headers, $match)) $key = $match[1]; main::console("client headers are:\r\n\r\n". "- root: ".$root."\r\n". "- host: ".$host."\r\n". "- origin: ".$origin."\r\n". "- sec-websocket-key: ".$key."\n"); 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\r\n". "upgrade: websocket\r\n". "connection: upgrade\r\n". "sec-websocket-accept: $acceptkey". "\r\n\r\n"; main::console("sending response client #{$this->getid()}:\r\n\r\n".$upgrade); var_dump($this->socket); socket_write($this->socket, $upgrade, strlen($upgrade)); $this->sethandshake(true); main::console("handshake 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}"); } } }
}
try socket_last_error()
, socket_strerror()
more information. if doesn't help, show how socket created. source: http://www.php.net/manual/en/function.socket-last-error.php
Comments
Post a Comment