Maison > Article > développement back-end > 客户端websocket 无法连接上PHP socket问题
我客户端是用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
<?phperror_reporting(E_ALL);set_time_limit(0);//ob_implicit_flush(); $address = '127.0.0.1';$port = 100322;//创建端口if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";} //绑定if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";} //监听if (socket_listen($sock, 5) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";} do { //得到一个链接 if (($msgsock = socket_accept($sock)) === false) { echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n"; break; } //welcome 发送到客户端 $msg = "<font color='red'>server send:welcome</font><br/>"; socket_write($msgsock, $msg, strlen($msg)); echo 'read client message\n'; $buf = socket_read($msgsock, 8192); $talkback = "received message:$buf\n"; echo $talkback; if (false === socket_write($msgsock, $talkback, strlen($talkback))) { echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n"; } else { echo 'send success'; } socket_close($msgsock);} while(true);//关闭socketsocket_close($sock); ?>
您的 php 代码中并没有看到与 websocket 协议相关的内容
看看服务端的端口有没有正常开启或已被占用
你的php socket服务器没有完成websocket握手,建议你看下websocket 协议相关文档,或者参考下别人怎么写的,比如下面的代码包含了websocket握手及发送websocket消息
https://github.com/walkor/workerman-chat/blob/master/applications/Chat/Event.php
你的php socket服务器没有完成websocket握手,建议你看下websocket 协议相关文档,或者参考下别人怎么写的,比如下面的代码包含了websocket握手及发送websocket消息
https://github.com/walkor/workerman-chat/blob/master/applications/Chat/Event.php
您的 php 代码中并没有看到与 websocket 协议相关的内容
有没有教程。。没教程没教程没教程没教程没教程没教程