Home >Backend Development >PHP Tutorial >客户端websocket 无法连接上PHP socket有关问题

客户端websocket 无法连接上PHP socket有关问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:02:171169browse

客户端websocket 无法连接上PHP socket问题

本帖最后由 d6965921d 于 2014-07-16 10:25:53 编辑 我客户端是用c++写得websocket

客户端我请求的地址是ws://127.0.0.1:100322 (我请求ws://echo.websocket.org这个是可以的说明客户端代码是没问题的 问题就在服务端)
输出
[1405476678:7805] NOTICE: Initial logging level 7
[1405476678:7806] NOTICE: Library version: 1.3 1544a2a
[1405476678:7806] NOTICE:  Started with daemon pid 0
[1405476678:7806] NOTICE:  static allocation: 4436 + (12 x 256 fds) = 7508 bytes
[1405476678:8329] WARN: problems parsing header


PHP 写的socket



<br /><?php<br />error_reporting(E_ALL);<br />set_time_limit(0);<br />//ob_implicit_flush();<br /> <br />$address = '127.0.0.1';<br />$port = 100322;<br />//创建端口<br />if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {<br />    echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";<br />}<br /> <br />//绑定<br />if (socket_bind($sock, $address, $port) === false) {<br />    echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";<br />}<br /> <br />//监听<br />if (socket_listen($sock, 5) === false) {<br />    echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";<br />}<br /> <br />do {<br />    //得到一个链接<br />    if (($msgsock = socket_accept($sock)) === false) {<br />        echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";<br />        break;<br />    }<br />    //welcome  发送到客户端<br />    $msg = "<font color='red'>server send:welcome</font><br/>";<br />    socket_write($msgsock, $msg, strlen($msg));<br />    echo 'read client message\n';<br />    $buf = socket_read($msgsock, 8192);<br />    $talkback = "received message:$buf\n";<br />    echo $talkback;<br />    if (false === socket_write($msgsock, $talkback, strlen($talkback))) {<br />        echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n";<br />    } else {<br />        echo 'send success';<br />    }<br />    socket_close($msgsock);<br />} while(true);<br />//关闭socket<br />socket_close($sock);<br /> <br /> <br />?><br />

以下是客户端发到服务端 PHP socket的输出
192:socket apple$ php index.php
read client message\nreceived message:GET / HTTP/1.1
Pragma: no-cache
Cache-Control: no-cache
Host: 127.0.0.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: nfvpsG0kAZkYrfZQkptpUA==
Origin: 127.0.0.1
Sec-WebSocket-Protocol: default-protocol
Sec-WebSocket-Extensions: deflate-frame
Sec-WebSocket-Version: 13


send success
------解决方案--------------------
你的php socket服务器没有完成websocket握手,建议你看下websocket 协议相关文档,或者参考下别人怎么写的,比如下面的代码包含了websocket握手及发送websocket消息
https://github.com/walkor/workerman-chat/blob/master/applications/Chat/Event.php
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn