使用SSL的PHP​​ WebSocket服务器

时间:2017-11-24 11:19:25

标签: php ssl websocket

我正在尝试在我的应用程序中使用在此环境中运行的websockets: - Debian 8 - Apache 2.4.10 - PHP 5.6.30 我使用devristo / phpws包,只是试图使他们的例子工作(时间)。 在HTTP上下文网站和ws://协议连接上一切正常,但是当我尝试将其放在HTTPS网站上并使用wss://协议时,它会失败。

我通过“php -q server.php”启动服务器,文件内容:

<?php
require_once("vendor/autoload.php");
use Devristo\Phpws\Server\WebSocketServer;

$loop = \React\EventLoop\Factory::create();

// Create a logger which writes everything to the STDOUT
$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);

// Create a WebSocket server using SSL
$server = new WebSocketServer("wss://X.X.X.X:9300", $loop, $logger);

$loop->addPeriodicTimer(0.5, function() use($server, $logger){
    $time = new DateTime();
    $string = $time->format("Y-m-d H:i:s");
    $logger->notice("Broadcasting time to all clients: $string");
    foreach($server->getConnections() as $client)
        $client->sendString($string);
});

// Bind the server
$server->bind();

// Start the event loop
$loop->run();

另一方面,我有一个time.html,其中包含:

<html>
<head>
    <title>WebSocket TEST</title>
</head>
<body>
<h1>Server Time</h1>
<strong id="time"></strong>

<script>
    var socket = new WebSocket("wss://X.X.X.X:9300/");
    socket.onmessage = function(msg) {
        document.getElementById("time").innerText = msg.data;
    };
</script>
</body>
</html>

服务器开始运行没有问题:

2017-11-24T11:47:57+01:00 NOTICE (5): phpws listening on ssl://X.X.X.X:9300
2017-11-24T11:47:57+01:00 NOTICE (5): Broadcasting time to all clients: 2017-11-24 11:47:57
2017-11-24T11:47:58+01:00 NOTICE (5): Broadcasting time to all clients: 2017-11-24 11:47:58
....

当我在浏览器中访问我的页面time.html时,Chrome中出现以下JavaScript错误:

VM725:164 WebSocket connection to 'wss://X.X.X.X:9300/' failed: Error in connection establishment: net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH

同时,服务器产生此错误输出:

PHP Warning:  stream_socket_accept(): SSL_R_NO_SHARED_CIPHER: no suitable shared cipher could be used.  This could be because the server is missing an SSL certificate (local_cert context option) in /var/www/web/sofive/k-zam/web/vendor/devristo/phpws/src/Devristo/Phpws/Server/WebSocketServer.php on line 141

Warning: stream_socket_accept(): SSL_R_NO_SHARED_CIPHER: no suitable shared cipher could be used.  This could be because the server is missing an SSL certificate (local_cert context option) in /var/www/web/sofive/k-zam/web/vendor/devristo/phpws/src/Devristo/Phpws/Server/WebSocketServer.php on line 141
PHP Warning:  stream_socket_accept(): Failed to enable crypto in /var/www/web/wstest/vendor/devristo/phpws/src/Devristo/Phpws/Server/WebSocketServer.php on line 141

Warning: stream_socket_accept(): Failed to enable crypto in /var/www/web/wstest/vendor/devristo/phpws/src/Devristo/Phpws/Server/WebSocketServer.php on line 141
PHP Warning:  stream_socket_accept(): accept failed: Success in /var/www/web/wstest/vendor/devristo/phpws/src/Devristo/Phpws/Server/WebSocketServer.php on line 141

Warning: stream_socket_accept(): accept failed: Success in /var/www/web/wstest/vendor/devristo/phpws/src/Devristo/Phpws/Server/WebSocketServer.php on line 141

所以我想我错过了传递给socket_context_create的证书?如果是,如何创建此证书,是否有规格(ssl版本?等),或者我是否需要传递与用于https网站的相同(使用let的加密certbot生成)?

0 个答案:

没有答案