我有一些简单的PHP代码可以创建SSL连接
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->sslPem);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->passPhrase);
$this->apnsConnection = stream_socket_client('ssl://'.$this->apnsHost.':'.$this->apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
但是知道如何将SO_KEEPALIVE设置为true吗?我也尝试过STREAM_CLIENT_PERSISTENT,这不是一回事。
答案 0 :(得分:1)
您是否已验证(通过网络跟踪)您需要设置套接字选项?
你通过套接字发送了什么? HTTP / HTTPS通过“Connection”标头引入了自己的连接重用功能,因此套接字上的选项不一定是您想要设置的选项。
答案 1 :(得分:1)
SO_KEEPALIVE,如
The SO_KEEPALIVE option causes a packet (called a 'keepalive probe') to be sent to the remote system if a long time (by default, more than 2 hours) passes with no other data being sent or received.?
答案 2 :(得分:0)
尝试一下:
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->sslPem);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->passPhrase);
$this->apnsConnection = stream_socket_client('ssl://'.$this->apnsHost.':'.$this->apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$is_keepalive = 0;
// https://www.php.net/manual/ru/function.socket-import-stream.php
$socket = socket_import_stream($this->apnsConnection);
if (socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1)) {
$is_keepalive = socket_get_option($socket, SOL_SOCKET, SO_KEEPALIVE);
//echo 'SO_KEEPALIVE: ' . $is_keepalive . PHP_EOL;
} else {
echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}
if ($is_keepalive) {
// https://www.php.net/manual/ru/function.socket-export-stream.php
$this->apnsConnection = socket_export_stream($socket);
}
// TEST
//$socket = socket_import_stream($this->apnsConnection);
//echo 'SO_KEEPALIVE: ' . socket_get_option($socket, SOL_SOCKET, SO_KEEPALIVE);