<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
// 证书最好是申请的证书
$context = array(
// 更多ssl选项请参考手册 http://php.net/manual/zh/context.ssl.php
'ssl' => array(
// 请使用绝对路径
'local_cert' => '/www/wwwroot/www.fpm.com/ssl.crt', // crt或pem文件
'local_pk' => '/www/wwwroot/www.fpm.com/ssl.key',
'verify_peer' => false,
'allow_self_signed' => true, //如果是自签名证书需要开启此选项
)
);
// 这里设置的是websocket协议(端口任意,但是需要保证没被其它程序占用)
$worker = new Worker('websocket://0.0.0.0:443', $context);
// 设置transport开启ssl,websocket+ssl即wss
$worker->transport = 'ssl';
$worker->onMessage = function(TcpConnection $con, $msg) {
$con->send('ok');
};
Worker::runAll();
實例運行多次並於前端有交互時如:
[root@localhost www.fpm.com]# /usr/local/php734/bin/php ssl.php start
Workerman[ssl.php] start in DEBUG mode
------------------------------------------- WORKERMAN -------------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
-------------------------------------------- WORKERS --------------------------------------------
proto user worker listen processes status
ssl root none websocket://0.0.0.0:443 1 [OK]
-------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
^CWorkerman[ssl.php] stopping ...
Workerman[ssl.php] has been stopped
[root@localhost www.fpm.com]# /usr/local/php734/bin/php ssl.php start
Workerman[ssl.php] start in DEBUG mode
------------------------------------------- WORKERMAN -------------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
-------------------------------------------- WORKERS --------------------------------------------
proto user worker listen processes status
ssl root none websocket://0.0.0.0:443 1 [OK]
-------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
SSL handshake error: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown
可以看到這裏運行了兩次實例,且都有報錯,那麽第一次報錯是由websocket直接訪問引起的
前端websocket代碼如:
<!doctype html>
<html>
<head>
<script>
ws2 = new WebSocket("wss://192.168.43.170");
ws2.onopen=function(evt){
console.log("ws2連接成功!",evt);
var order={
order_id:220807,
uid:2022
}
ws2.send(JSON.stringify(order));
};
ws2.onmessage = function(e) {
console.log("ws2收到服务端的消息:" + e.data);
};
</script>
</head>
<body>
<div class="container">
</div>
</body>
</html>
那麽第一次websocket訪問時chrome在控制台就直接報錯連接失敗,其實這個時候我們將wss鏈接換成https鏈接並訪問于瀏覽器中,此時chrome會有該網站不安全的提示,你點擊高級裏面的繼續訪問就能出現如:
的界面,此時,你可以中斷最開始的服務端脚本並重新啓動那個服務端脚本,再次刷新前端websocket所在頁面就能看到正常連接了
當然這裏第二次訪問websocket時候還是出現了一點小錯誤,但是好在前端還是正常接收到消息了。
此時前端websocket每次刷新後端那個脚本還是每次會有報錯。
不過消息還是正常發送,這裏有個地方就是第二次訪問websocket時候或許都不用重啓服務端脚本,不過這裏沒有實驗,如果有你想的話可以試試。