canyin-project/ybcy/websocket.php

28 lines
942 B
PHP
Raw Normal View History

2024-11-01 16:07:54 +08:00
<?php
$info=file_get_contents('./web/cacheSet.log');
$port=json_decode($info,true)['swoole'];
$ws =new swoole_websocket_server("0.0.0.0","$port",SWOOLE_BASE, SWOOLE_SOCK_TCP);
$ssl_certificate=json_decode($info,true)['ssl_certificate'];
$ssl_certificate_key=json_decode($info,true)['ssl_certificate_key'];
//配置参数
$ws ->set([
'daemonize' => false, //守护进程化。
//配置SSL证书和密钥路径
'ssl_cert_file'=>$ssl_certificate,//https证书位置
'ssl_key_file'=>$ssl_certificate_key,//https证书位置
]);
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
echo "client-{$request->fd} is open\n";
});
//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
echo "Message: {$frame->data}\n";
$ws->push($frame->fd, "server: {$frame->data}");
});
//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();