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

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块