thinkphp6使用workerman websocket连接 (带源码)
一、首先按照thinkphp文档中的进行安装workerman
地址:https://www.kancloud.cn/manual/thinkphp6_0/1147857
- composer require topthink/think-worker
- php think worker
- php think worker:server
- websocket客户端代码:
<!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://外网地址:2345");
socket.onopen = function() {
$("#msg").html("连接成功...");
};
socket.onmessage = function(e) {
$("#msg").html($("#msg").html() + "<br />" + 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>
<p id="msg"></p>
</body>
</html>
- config/worker.php配置(这里没改过):
return [
'host' => '0.0.0.0',
'port' => 2346,
'root' => '',
'app_path' => '',
'file_monitor' => false,
'file_monitor_interval' => 2,
'file_monitor_path' => [],
'name' => 'thinkphp',
'count' => 4,
'daemonize' => false,
'pidFile' => '',
];
6.config\worker_server.php 主要看worker_class代表:自定义服务类,需要在该位置建立对应文件,如第7项
return [
'protocol' => 'websocket',
'host' => '0.0.0.0',
'port' => 2345,
'socket' => '',
'context' => [],
'worker_class' => 'app\index\controller\Worker',
'name' => 'thinkphp',
'count' => 4,
'daemonize' => false,
'pidFile' => '',
'onWorkerStart' => function ($worker) {
},
'onWorkerReload' => function ($worker) {
},
'onConnect' => function ($connection) {
},
'onMessage' => function ($connection, $data) {
$connection->send('receive success');
},
'onClose' => function ($connection) {
},
'onError' => function ($connection, $code, $msg) {
echo "error [ $code ] $msg\n";
},
];
7.worker_class位置: 在app/index/controller/新增Index.php,文件位置和名称随意,但要和worker_class.php中对应
namespace app\index\controller;
use think\facade\Db;
use think\worker\Server;
use Workerman\Lib\Timer;
define('HEARTBEAT_TIME', 20);
class Worker extends Server
{
protected $socket = 'websocket://0.0.0.0:2345';
public function __construct()
{
parent::__construct();
$this->onMessage();
$this->worker->onWorkerStart = function($worker)
{
echo "Worker starting...\n";
};
}
public function onMessage()
{
$this->worker->onMessage = function($connection, $data)
{
dump('参数:'.$data.'时间:'.date('Y-m-d H:i:s',time()));
$connection->send($data);
};
}
}
注意:protected $socket = ‘websocket://0.0.0.0:2345’; 这里我在刚开始写的时候被thinkphp文档误解了写成http://0.0.0.0:2345
二、连接失败常见问题
- 防火墙未放行2345和2346端口
- 服务端放行地址是0.0.0.0客户端如果写成127.0.0.0会连接不上,应该使用外网地址
三、源码
这里使用的是重新安装thinkphp6之后直接做的workerman代码没有其他代码。 /app/websocket/view/index.html是客户端代码。 下载地址: 链接:https://pan.baidu.com/s/10x7BDIhSMyUvX-we3XgK6Q 提取码:6v4o
欢迎大家评论区留言或私信我~!!!
|