php websocket连接失败,PHP中的Websocket握手连接错误

在控制台中显示错误:WebSocket连接到

'ws://localhost/socket/php-socket.php/wss'失败:期间出错

$newSocket = socket_accept($socketResource);

$clientSocketArray[] = $newSocket;

$header = socket_read($newSocket, 1024);

$chatHandler->doHandshake($header, $newSocket, HOST_NAME, PORT);

enter code here

function doHandshake($received_header,$client_socket_resource, $host_name, $port) {

$headers = array();

$lines = preg_split("/\r\n/", $received_header);

foreach($lines as $line)

{

$line = chop($line);

if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))

{

$headers[$matches[1]] = $matches[2];

}

}

$secKey = $headers['Sec-WebSocket-Key'];

$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));

$buffer = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .

"Upgrade: websocket\r\n" .

"Connection: Upgrade\r\n" .

"WebSocket-Origin: $host_name\r\n" .

"WebSocket-Location: ws://$host_name:$port/demo/shout.php\r\n".

"Sec-WebSocket-Accept:$secAccept\r\n\r\n";

socket_write($client_socket_resource,$buffer,strlen($buffer));

}

Javascript代码:

var websocket = new WebSocket("ws://localhost/socket/php-socket.php");

websocket.onopen = function(event) {

showMessage("

Connection is established!
");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值