1 thinkphp6.0 中开发一个websocket 聊天 前端用uniapp

2024-08-23 09:28

本文主要是介绍1 thinkphp6.0 中开发一个websocket 聊天 前端用uniapp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

开发环境

thinkphp 8.0
php 8.2 创建项目:composer create-project topthink/think chatphp
安装composer require workerman/workerman
composer require topthink/think-worker:* -W

注意将composer.json进行以下修改:

{"name": "topthink/think","description": "the new thinkphp framework","type": "project","keywords": ["framework","thinkphp","ORM"],"homepage": "https://www.thinkphp.cn/","license": "Apache-2.0","authors": [{"name": "liu21st","email": "liu21st@gmail.com"},{"name": "yunwuxin","email": "448901948@qq.com"}],"require": {"php": ">=7.2.5","topthink/framework": "^6.1.0","topthink/think-orm": "^2.0","topthink/think-filesystem": "^1.0","workerman/workerman": "^3.5","topthink/think-worker": "*"},"require-dev": {"symfony/var-dumper": "^4.2","topthink/think-trace": "^1.0"},"autoload": {"psr-4": {"app\\": "app"},"psr-0": {"": "extend/"}},"config": {"preferred-install": "dist"},"scripts": {"post-autoload-dump": ["@php think service:discover","@php think vendor:publish"]}
}

composer update 进行升级

在app 中controller 创建Chat.php

端口号为 2346

<?php
namespace app\controller;use Workerman\Worker;
use think\worker\Server;class Chat extends Server
{protected $socket = 'websocket://0.0.0.0:2346';protected $groups = [];       // 存储用户与群组的映射protected $kickedUsers = [];  // 存储被踢出用户的信息protected $groupUsers = [];   // 存储每个群组中的用户列表public function onMessage($connection, $data){$data = json_decode($data, true);switch ($data['type']) {case 'joinGroup':$this->joinGroup($connection, $data);break;case 'sendMessage':$this->sendMessage($connection, $data);break;case 'kickUser':$this->kickUser($data);break;default:break;}}public function onConnect($connection){echo "New connection: {$connection->id}\n";}public function onClose($connection){if (isset($this->groups[$connection->id])) {$groupName = $this->groups[$connection->id]['groupName'];$userName = $this->groups[$connection->id]['userName'];$this->groupUsers[$groupName] = array_filter($this->groupUsers[$groupName], function ($user) use ($userName) {return $user !== $userName;});foreach ($this->worker->connections as $client) {$client->send(json_encode(['type' => 'userList','users' => $this->groupUsers[$groupName]]));$client->send(json_encode(['type' => 'left', 'content' => "$userName 已离开群组 $groupName"]));}unset($this->groups[$connection->id]);}echo "Connection {$connection->id} has disconnected\n";}protected function joinGroup($connection, $data){$groupName = $data['groupName'];$userName = $data['userName'];if (!isset($this->kickedUsers[$groupName]) || !in_array($userName, $this->kickedUsers[$groupName])) {$this->groups[$connection->id] = ['groupName' => $groupName, 'userName' => $userName];$this->groupUsers[$groupName][] = $userName;foreach ($this->worker->connections as $client) {$client->send(json_encode(['type' => 'userList','users' => $this->groupUsers[$groupName]]));$client->send(json_encode(['type' => 'join', 'content' => "$userName 加入"]));}echo "$userName has joined the group $groupName\n";} else {$connection->send(json_encode(['type' => 'kick', 'content' => "$userName 被踢"]));}}protected function sendMessage($connection, $data){$groupName = $data['group_name'];$userName = $data['user_name'];if (!isset($this->kickedUsers[$groupName]) || !in_array($userName, $this->kickedUsers[$groupName])) {foreach ($this->worker->connections as $client) {if (isset($this->groups[$client->id]) && $this->groups[$client->id]['groupName'] === $groupName) {$client->send(json_encode($data));}}echo "$userName sent a message to $groupName\n";} else {$connection->send(json_encode(['type' => 'kick', 'content' => "$userName 被踢"]));}}protected function kickUser($data){$groupName = $data['groupName'];$userName = $data['userName'];foreach ($this->worker->connections as $client) {if (isset($this->groups[$client->id]) && $this->groups[$client->id]['groupName'] === $groupName && $this->groups[$client->id]['userName'] === $userName) {$client->send(json_encode(['type' => 'kick', 'content' => "$userName 被踢"]));$client->close();$this->groupUsers[$groupName] = array_filter($this->groupUsers[$groupName], function ($user) use ($userName) {return $user !== $userName;});foreach ($this->worker->connections as $otherClient) {$otherClient->send(json_encode(['type' => 'userList','users' => $this->groupUsers[$groupName]]));}$this->kickedUsers[$groupName][] = $userName;break;}}echo "$userName 被踢出群组 $groupName\n";}
}

这篇关于1 thinkphp6.0 中开发一个websocket 聊天 前端用uniapp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098987

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.