search
HomePHP FrameworkWorkermanworkerman+thinkphp creates a simple chat room

workerman+thinkphp creates a simple chat room

Jan 14, 2020 pm 02:39 PM
thinkphpworkerman

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));
}

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
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use