WebSocketHandle.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Company: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/5/9
  8. * Time: 15:51
  9. * Author: Merlin
  10. */
  11. namespace app\process;
  12. use app\common\helpers\YunSession;
  13. use app\common\helpers\YunSessionMemcache;
  14. use app\common\helpers\YunSessionRedis;
  15. use app\common\services\Session;
  16. use Illuminate\Support\Facades\Redis;
  17. use Workerman\Connection\TcpConnection;
  18. class WebSocketHandle
  19. {
  20. protected $service;
  21. public function __construct(WebSocket $service)
  22. {
  23. $this->service = $service;
  24. }
  25. public function login(TcpConnection $connection, array $res)
  26. {
  27. $session_id = $res['session_id'];
  28. if (empty($session_id)) {
  29. return $connection->send(json_encode(['msg' => '授权失败', 'result' => 0]));
  30. }
  31. $session_data = Redis::hgetall('PHPSESSID:' . $session_id);
  32. $member_id = $this->getMemberId($session_data['data']);
  33. if (empty($member_id)) {
  34. return $connection->send(json_encode(['msg' => '授权失败', 'result' => 0]));
  35. }
  36. if (empty($res['group'])) {
  37. return $connection->send(json_encode(['msg' => '授权失败,没有分组', 'result' => 0]));
  38. }
  39. $connection->member_id = $member_id;
  40. $connection->is_login = 1;
  41. $this->service->setMember($member_id, $res['group'] , $connection);
  42. return $connection->send(json_encode(['msg' => '登陆成功', 'result' => 1]));
  43. }
  44. public function register(TcpConnection $connection, $res)
  45. {
  46. $connection->group = $res['group'];
  47. $connection->is_login = 0;
  48. $this->service->setGroup($res['group'],$connection);
  49. return $connection->send(json_encode(['msg' => '注册成功', 'result' => 1]));
  50. }
  51. public function ping(TcpConnection $connection, $res)
  52. {
  53. return $connection->send('pong');
  54. }
  55. private function getMemberId($read_data)
  56. {
  57. $member_data = '';
  58. if (!empty($read_data)) {
  59. preg_match_all('/yunzshop_([\w]+[^|]*|)/', $read_data, $name_matches);
  60. preg_match_all('/(a:[\w]+[^}]*})/', $read_data, $value_matches);
  61. if (!empty($name_matches)) {
  62. foreach ($name_matches[0] as $key => $val) {
  63. if ($val == 'yunzshop_member_id') {
  64. $member_data = $val . '|' . $value_matches[0][$key];
  65. }
  66. }
  67. }
  68. }
  69. $member_id = unserialize(explode('|', $member_data)[1])['data'];
  70. return $member_id;
  71. }
  72. }