我使用stream_socket_server创建一个安全的websocket服务器。 我希望有一个函数接受顺序获取连接并将握手作为阻塞访问。使用chrome,它连接和断开连接,并且它告诉我,fread有错误。 摘录如下:
$write = null;
$except = null;
$sockets = $this->socket;
@stream_select( $sockets, $write, $except, 0 );
foreach( $sockets as $socket ) {
$resource = @stream_socket_accept( $socket );
if (!$resource) {
return false;
}
$accepts = array( $resource );
@stream_select( $accepts, $write, $except, 0, 1000 );
foreach( $accepts as $accepted ) {
$buffer = '';
$bytes_to_read = 8192;
while ( $chunk = fread( $accepted, $bytes_to_read ) ) {
$buffer .= $chunk;
$status = stream_get_meta_data( $accepted );
$bytes_to_read = $status[ "unread_bytes" ];
if ( strlen( $buffer ) === 1 )
$bytes_to_read = 8192;
}
$response = HandShake::perform( $buffer );
$responseLength = strlen( $response );
for( $written = 0; $written < $responseLength; $written += $fwrite ) {
$fwrite = fwrite( $accepted, substr( $response, $written ) );
if ( ( $fwrite === false ) || ( $fwrite === 0 ) ) {
stream_socket_shutdown( $accepted, STREAM_SHUT_RDWR );
return false;
}
}
}
}
你能告诉我为什么它不起作用并转换我的片段吗?