Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use workerman+thinkphp to make a simple chat room

Shulou Source: shulou.com Published: 2022-06-03 05:11:21 09月21日 Update

In this issue, the editor will bring you about how to use workerman+thinkphp to make a simple chat room. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

1: the environment is under window, thinkphp3.2

2: download GatewayWork and put it in

Under the catalog

3: then double-click to open

This file starts the service.

4: all the logic is in

In the Events.php file,

5: triggered when the user connects to the server

, initialize, send client_id

6: when you receive the message

Set out this method, message, which is the data sent by the client.

7: attach the code

/ * trigger * @ param int $client_id connection id * @ param mixed $message specific message * / public static function onMessage ($client_id, $message) {/ / 1: transfer to array () $data = json_decode ($message,true) after receiving the message; if (! $data) {return } / / 2: judge the type. Bind is the event switch ($data ['type']) where client_id binds with user id / / say sends messages {/ / binds case' bind': $from_id = $data ['from_id']; / / binds the acquired to user id and client_id Gateway::bindUid ($client_id,$from_id) Return; / / send a text message case 'say': / / get the information sent from the client $text = $data [' data']; $from_id = $data ['from_id']; $to_id = $data [' to_id'] / / encapsulate the message $info = array ('type'= >' text', 'data'= > $text,' from_id'= > $from_id, 'to_id'= > $to_id,' time'= > date ('Y-m-d hpuri swatches time (() Gateway::sendToUid ($to_id,json_encode ($info)); return; / / send pictures case 'img': $from_id = $data [' from_id']; $to_id = $data ['to_id']; $img = $data [' img'] / / encapsulate the message $info = array ('type'= >' img', 'data'= > $img,' from_id'= > $from_id, 'to_id'= > $to_id,' time'= > date ('Y-m-d hpuri swatches time (() Gateway::sendToUid ($to_id,json_encode ($info)); return;} / / push to the specified uid / / send / / Gateway::sendToAll to all (json_encode ($info));}

Point-to-point message exchange can be easily realized.

The above is the editor for you to share how to use workerman+thinkphp to make a simple chat room, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Message customer client user simple chat room production content that is file analysis encapsulation service professional small and medium event code information rich content picture Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info OPPO Reno MariaDB Microsoft