Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report