Home PHP Framework Workerman workerman+thinkphp creates a simple chat room

workerman+thinkphp creates a simple chat room

Jan 14, 2020 pm 02:39 PM
thinkphp workerman

The following column workerman tutorial will introduce to you how to make a simple chat room. I hope it will be helpful to friends in need!

workerman+thinkphp creates a simple chat room

1: The environment is under the window, thinkphp3.2

2: Download GatewayWork and put it in

workerman+thinkphp creates a simple chat room

Directory

3: Then double-click to open

workerman+thinkphp creates a simple chat room

This file starts the service

4: All logic is in

workerman+thinkphp creates a simple chat room

5 in the Events.php file: When the user connects to the server, trigger

workerman+thinkphp creates a simple chat room

, initialize, send client_id

6: When receiving the message,

workerman+thinkphp creates a simple chat room

starts this method, message is the data sent by the client

7: Attached code

/**
 * 当客户端发来消息时触发
 * @param int $client_id 连接id
 * @param mixed $message 具体消息
 */
public static function onMessage($client_id, $message)
{
     //1:收到消息之后转成array()
     $data = json_decode($message,true);
     if(!$data){
       return;
     }
     //2:判断类型,bind是client_id与用户id绑定 
     //say 发送消息的事件
     switch ($data['type']) {
       //绑定
       case 'bind':
           $from_id = $data['from_id'];
           //把获取的到用户id与client_id进行绑定
           Gateway::bindUid($client_id,$from_id);
           return;
       //发送文字消息
       case 'say':
           //获取到客户端传过来的信息
           $text = $data['data'];
           $from_id = $data['from_id'];
           $to_id = $data['to_id'];
           //封装消息
           $info = array(
             'type'=>'text',
             'data'=>$text,
             'from_id'=>$from_id,
             'to_id'=>$to_id,
             'time'=>date('Y-m-d h:i:s',time()) 
           );
           Gateway::sendToUid($to_id,json_encode($info));
           return;
       //发送图片
       case 'img':
           $from_id = $data['from_id'];
           $to_id = $data['to_id'];
           $img = $data['img'];
           //封装消息
           $info = array(
             'type'=>'img',
             'data'=>$img,
             'from_id'=>$from_id,
             'to_id'=>$to_id,
             'time'=>date('Y-m-d h:i:s',time()) 
           );
           Gateway::sendToUid($to_id,json_encode($info));
           return;
     }
     //推送给指定的uid
     // 向所有人发送 
     // Gateway::sendToAll(json_encode($info));
}
Copy after login

can easily realize point-to-point message exchange.

The above is the detailed content of workerman+thinkphp creates a simple chat room. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

Implement file upload and download in Workerman documents Implement file upload and download in Workerman documents Nov 08, 2023 pm 06:02 PM

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to implement the basic usage of Workerman documents How to implement the basic usage of Workerman documents Nov 08, 2023 am 11:46 AM

Introduction to how to implement the basic usage of Workerman documents: Workerman is a high-performance PHP development framework that can help developers easily build high-concurrency network applications. This article will introduce the basic usage of Workerman, including installation and configuration, creating services and listening ports, handling client requests, etc. And give corresponding code examples. 1. Install and configure Workerman. Enter the following command on the command line to install Workerman: c

Which one is better, swoole or workerman? Which one is better, swoole or workerman? Apr 09, 2024 pm 07:00 PM

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.

See all articles