thinkphp6 使用Workerman测试

1.首先通过 composer 安装 think-worker、think-view

composer require topthink/think-worker
composer require topthink/think-view

安装成功后在项目的config目录会新增3个配置文件

 2.修改woker_server.php中监听的端口

// 扩展自身需要的配置
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host' => '127.0.0.1', // 监听地址
'port' => 8000, // 监听端口
'socket' => 'http://127.0.0.1:8000', // 完整监听地址
'context' => [], // socket 上下文选项
'worker_class' => 'app\controller\Worker', // 自定义Workerman服务类名 支持数组定义多个服务

3.在项目controller下新建文件 Worker.php。

<?php

namespace app\controller;

use think\worker\Server;

class Worker extends Server
{
    protected $socket = 'websocket://127.0.0.1:8000';

    public $con;

    public function onConnect($connection)
    {
        $connection->send('链接成功!');
    }

    public function onMessage($connection,$data)
    {

        $connection->send("\n".'服务器接收成功!'."\n");
        $this->con = $connection;
        $this->qianwen($data);
    }

    public function onClose($connection)
    {

    }

    public function onError($connection,$code,$msg)
    {
        echo 'error' . $code  . $msg;

    }

    public function onWorkerStart($worker)
    {

    }

    public function qianwen($question='长恨歌')
    {
        header('Content-Type: text/event-stream');
        header('Cache-Control: no-cache');
        header('X-Accel-Buffering: no');
        header('Connection: keep-alive');
        set_time_limit(0);

        $openai_api_key = 'sk-6666666666666666666666666666';//子空间key
        $url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
        $headers = [
            'Authorization: Bearer ' . $openai_api_key,
            'Content-Type: application/json',
            'X-DashScope-SSE: enable'
        ];
        $params =  [
            'model' => 'farui-plus',
            'input' => ['messages' => []],
            'parameters' => [
                'result_format'=>'message',
                'incremental_output'=>true
            ],
        ];
        $params['input']['messages'][] = [
            'role' => 'user',
            'content' => $question
        ];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//有时候希望返回的内容作为变量储存,而不是直接输出。这个时候就必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true。
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//设置这个选项为一个非零值(象 “Location: “)的头,服务器会把它当做HTTP头的一部分发送(注意这是递归的,PHP将发送形如 “Location: “的头)。
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//curl_exec()获取的信息以文件流的形式返回,而不是直接输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
            $res = substr(explode("\n",$data)[3],5);
            $res = json_decode($res,true);
            if($res['output']){
                $this->dayin($res['output']['choices'][0]['message']['content']);
            }else{
                echo $res['message'];
            }
            return strlen($data);
        });
        curl_exec($ch);
        curl_close($ch);

    }

    public function dayin($msg){
        $this->con->send($msg);
    }

}

 

在cmd窗口项目跟目录执行:

php think worker:server

 

 4.新建一个路由测试前端连接

Route::get('wokerman', 'index/wokerman');

在Index控制器编写测试方法

public function wokerman()
    {

        return View::fetch();
    }

5.创建前端模板文件,目录为

 模板文件代码如下;

<!DOCTYPE html>
<html>
<head>
<title>HTML5</title>
<meta charset="utf-8" />
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function() {
var socket;
var readyState = ["connecting", "connected", "closing", "closed"];
/* 打开连接事件 */
$("button:eq(0)").click(function() {
try {
/* 连接 */
socket = new WebSocket("ws://127.0.0.1:8000");
/* 绑定事件 */
socket.onopen = function() {
$("#msg").html("连接成功...");
};

socket.onmessage = function(e) {
// $("#msg").html($("#msg").html() + "<br />" + e.data);
$("#return").html($("#return").html() + e.data);
};

socket.onclose = function() {
$("#msg").html($("#msg").html() + "<br />关闭连接...");
};
} catch(exception) {
$("#msg").html($("#msg").html() + "<br />有错误发生");
}
});

/* 发送数据事件 */
$("button:eq(1)").click(function() {
/* 检查文本框是否为空 */
if($("#data").val() == "") {
alert("请输入数据!");
return;
}

try {
socket.send($("#data").val());
$("#msg").html($("#msg").html() + "<br />发送数据:" + $("#data").val());
} catch (exception) {
$("#msg").html($("#msg").html() + "<br />发送数据出错");
}

/* 清空文本框 */
$("#data").val("");
});

/* 断开连接 */
$("button:eq(2)").click(function() {
socket.close();
});
});
</script>
</head>

<body>
<h1>WebSocket示例</h1>
<input type="text" id="data" />
<button>打开连接</button>
<button>发送数据</button>
<button>关闭连接</button>
<div style="width: auto" id="msg"></div>
<textarea style="width:1200px;min-height:800px;max-height: max-content" id="return"></textarea>
</body>
</html>

6.测试前端连接wokerman

前端:

 

 后端:

 

posted @ 2024-07-08 11:14  流浪2024  阅读(322)  评论(0编辑  收藏  举报