当前位置:   article > 正文

Thinkphp 使用Workerman 聊天室_thinkphp workerman

thinkphp workerman

1、下载Workerman插件

composer require topthink/think-worker

2、示例代码 后端

     2.1 下载之后会得到这俩个文件 

2.2 这个文件对应上面的worker_class 的地址 

   2.3.worker.php示例代码

  1. <?php
  2. namespace app\http;
  3. use think\facade\Db;
  4. use think\worker\Server;
  5. use Workerman\Lib\Timer;
  6. use app\model\IndexModel;
  7. /**
  8. * websockect
  9. * Class Worker
  10. * @package app\http
  11. */
  12. class Worker extends Server
  13. {
  14. protected $socket = 'http://0.0.0.0:2348';
  15. /**
  16. * 心跳检测
  17. * @var int
  18. */
  19. protected static $heartbeat_time = 55;
  20. public function onConnect($connection){
  21. var_dump('connect success');
  22. $connection->send(json_encode('connect success'));
  23. }
  24. ## 接收消息,接收心跳消息 ,接收用户id 信息等等
  25. public function onMessage($connection,$data)
  26. {
  27. $connection->lastMessageTime = time();
  28. #var_dump($data);
  29. $connection->send(json_encode('receive success'));
  30. }
  31. /**
  32. * 消息回调
  33. * @param $worker
  34. * @author
  35. * @date 2022/5/5 17:07
  36. */
  37. public function onWorkerStart($worker)
  38. {
  39. //查看是否有新的下面的3是几秒推送一次
  40. Timer::add(3, function () use ($worker) {
  41. $time_now = time();
  42. var_dump(date('Y-m-d H:i:s',$time_now) );
  43. ## 推送的数据
  44. $infoObj = IndexModel::getTips();
  45. $infoArr = json_decode(json_encode($infoObj),true);
  46. if (true) {
  47. foreach ($worker->connections as $connection) {
  48. if (empty($connection->lastMessageTime)) {
  49. $connection->lastMessageTime = $time_now;
  50. }
  51. var_dump(date('Y-m-d H:i:s',$connection->lastMessageTime) );
  52. if ($time_now - $connection->lastMessageTime > self::$heartbeat_time) {
  53. $connection->close();
  54. }
  55. $connection->send(json_encode($infoArr));
  56. }
  57. } else {
  58. var_dump(22);
  59. foreach ($worker->connections as $connection) {
  60. if (empty($connection->lastMessageTime)) {
  61. $connection->lastMessageTime = $time_now;
  62. continue;
  63. }
  64. if ($time_now - $connection->lastMessageTime > self::$heartbeat_time) { //连接超时
  65. $connection->close();
  66. }
  67. }
  68. }
  69. });
  70. }
  71. public function onClose($connection){
  72. var_dump('close');
  73. }
  74. }

     2.4 前端示例代码

  1. <script>
  2. let ws = new WebSocket("ws://127.0.0.1:2348")
  3. ws.onopen = function() { //绑定连接事件
  4. console.log("连接成功");
  5. //每30秒发送一次心跳
  6. setInterval(function(){
  7. ws.send(JSON.stringify({'type':"peng"}));
  8. console.log('发送心跳...');
  9. },30000)
  10. };
  11. ws.onmessage = function(evt) {//绑定收到消息事件
  12. data = JSON.parse(evt.data)
  13. console.log(data);
  14. //这里处理收到的消息, type类型有两种: connectin、deposit如果有deposit要提示
  15. };
  16. ws.onclose = function(evt) { //绑定关闭或断开连接事件
  17.   console.log("连接已关闭");
  18. };
  19. </script>

3、开启服务 php think worker:server

4、打开前端页面查看是否链接成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/309451
推荐阅读
相关标签
  

闽ICP备14008679号