Events.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. /**
  15. * 用于检测业务代码死循环或者长时间阻塞等问题
  16. * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  17. * 然后观察一段时间workerman.log看是否有process_timeout异常
  18. */
  19. //declare(ticks=1);
  20. /**
  21. * 聊天主逻辑
  22. * 主要是处理 onMessage onClose
  23. */
  24. use \GatewayWorker\Lib\Gateway;
  25. //use \GatewayWorker\Lib\DbConnection;
  26. use think\Db;
  27. use think\App;
  28. class Events
  29. {
  30. public static function onWorkerStart(){
  31. App::initCommon();
  32. //$info = Db::name('gateway_test')->insertGetId(['msg'=>'onWorkerStart','datetime'=>date('Y-m-d H:i:s')]);
  33. }
  34. /**
  35. * 有消息时
  36. * @param int $client_id
  37. * @param mixed $message
  38. */
  39. public static function onMessage($client_id, $message)
  40. {
  41. //echo $message.PHP_EOL;
  42. //file_put_contents('1.json',$message.PHP_EOL,FILE_APPEND);
  43. //Gateway::sendToAll($client_id.':'.$message);
  44. //php版本用7.3
  45. try{
  46. $data = self::get_data($message);
  47. if(!empty($data)){
  48. //file_put_contents('1.json',json_encode($data).PHP_EOL,FILE_APPEND);
  49. $info = Db::name('device_sanheyi')->insert($data);
  50. }
  51. }catch (\Exception $e)
  52. {
  53. //echo $e->getMessage();
  54. }
  55. //Gateway::sendToAll($client_id.':'.$message);
  56. return;
  57. /* $message = json_decode($message,true);
  58. if(is_array($message)){
  59. self::changestatus($message);
  60. }else{
  61. //echo 'default';
  62. }*/
  63. //return ;
  64. /*if(is_array($message) && isset($message['action']) && $message['action'] == 'changestatus'){
  65. self::changestatus($message);
  66. }else{
  67. echo 'default';
  68. }
  69. return ;*/
  70. }
  71. /**
  72. * 当客户端连接时触发
  73. * 如果业务不需此回调可以删除onConnect
  74. * @param int $client_id 连接id
  75. */
  76. public static function onConnect($client_id)
  77. {
  78. // 向当前client_id发送数据
  79. // Gateway::sendToClient($client_id, "Hello $client_id");
  80. // 向所有人发送
  81. // Gateway::sendToAll("$client_id login");
  82. }
  83. /*{
  84. 'action':'changestatus',
  85. "uid": "1",
  86. "type": "video",'audio'
  87. "value": "0",或‘1’
  88. "nowtime": "1234567890 ",
  89. "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
  90. }*/
  91. //修改用的通话状态
  92. private static function changestatus($message){
  93. //dump($message);
  94. if(isset($message['uid']) && isset($message['value'])){
  95. $redis = newredis();
  96. $redis->set('gg_matching_uid_'.$message['uid'], $message['value'], null);
  97. }
  98. return;
  99. }
  100. //聊天扣钱
  101. private static function userchat(){
  102. $price = config('site.typing_min_price');
  103. //扣费
  104. Db::startTrans();
  105. if(isset($message['uid'])){
  106. $rs = model('wallet')->lockChangeAccountRemain($message['uid'],'gold',-$price,13,'','',0);
  107. if($rs['status'] === false){
  108. Db::rollback();
  109. }
  110. }
  111. Db::commit();
  112. }
  113. public static function test31(){
  114. //血糖,5秒
  115. $str = ' 7.2mmol/L (GLUC 2023/12/29 06:05 SN:PMA231223000006 )';
  116. //尿酸,16秒
  117. //$str = ' 381umol/L ( UA 2023/12/29 06:21 SN:PMA231223000006 )';
  118. //胆总,26秒
  119. //$str = ' 3.91mmol/L (CHOL 2023/12/29 06:25 SN:PMA231223000006 )';
  120. $result = self::get_data($str);
  121. dump($result);
  122. }
  123. private static function get_data($str = ''){
  124. $str = trim($str);
  125. //判断为空
  126. if(empty($str)){
  127. return [];
  128. }
  129. //找到编号起始点
  130. $SN_start = strpos($str,'SN:');
  131. if($SN_start === false){
  132. return [];
  133. }
  134. //编号
  135. $SN = mb_substr($str,$SN_start+3,15);
  136. if(empty($SN)){
  137. return [];
  138. }
  139. //判断类型
  140. $type_gluc = strpos($str,'GLUC');
  141. $type_ua = strpos($str,'UA');
  142. $type_chol = strpos($str,'CHOL');
  143. if($type_gluc === false && $type_ua === false && $type_chol === false){
  144. return [];
  145. }
  146. //确认类型
  147. $type = '';
  148. $type_text = '';
  149. if($type_gluc){
  150. $type = 'GLUC';
  151. $type_text = '血糖';
  152. }
  153. if($type_ua){
  154. $type = 'UA';
  155. $type_text = '尿酸';
  156. }
  157. if($type_chol){
  158. $type = 'CHOL';
  159. $type_text = '总胆固醇';
  160. }
  161. if(empty($type) || empty($type_text)){
  162. return [];
  163. }
  164. //数值
  165. $value_end = strpos($str,'/L');
  166. if($value_end === false){
  167. return [];
  168. }
  169. $value = mb_substr($str,0,$value_end+2);
  170. if(empty($value)){
  171. return [];
  172. }
  173. //返回
  174. $data = [
  175. 'msg' => $str,
  176. 'datetime' =>date('Y-m-d H:i:s'),
  177. 'createtime' =>time(),
  178. 'sn' => $SN,
  179. 'type' => $type,
  180. 'type_text' => $type_text,
  181. 'value' => $value,
  182. ];
  183. return $data;
  184. }
  185. /**
  186. * 当客户端断开连接时
  187. * @param integer $client_id 客户端id
  188. */
  189. public static function onClose($client_id)
  190. {
  191. // debug
  192. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  193. // 从房间的客户端列表中删除
  194. if(isset($_SESSION['room_id']))
  195. {
  196. $room_id = $_SESSION['room_id'];
  197. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  198. Gateway::sendToGroup($room_id, json_encode($new_message));
  199. }*/
  200. }
  201. public static function onWorkerStop(){
  202. //$info = Db::name('gateway_test')->insertGetId(['msg'=>'onWorkerStop','datetime'=>date('Y-m-d H:i:s')]);
  203. }
  204. }