PHP + swoole implement simple multi-person online chat group _ PHP

Source: Internet
Author: User
This article mainly introduces the PHP + swoole implementation of simple multi-person online chat group-related information. For more information, see this article because of the limited capabilities, there are a lot of chat logic details not implemented, the code is as follows:

Php code:

$ Serv = new swoole_websocket_server ("127.0.0.1", 3999); // Basic service settings $ serv-> set (array ('worker _ num' => 2, 'reactor _ num' => 8, 'task _ worker_num '=> 1, 'Dispatch _ mode' => 2, 'debug _ mode' => 1, 'daemonize '=> true, 'log _ file' => _ DIR __. '/log/webs_swoole.log', 'Heartbeat _ check_interval '=> 60, 'Heartbeat _ idle_time' => 600,); $ serv-> on ('connect ', function ($ serv, $ fd) {// echo "client: $ fd Connect. ". PHP_EOL;}); // Test receive $ se Rv-> on ("receive", function (swoole_server $ serv, $ fd, $ from_id, $ data) {// echo "receive # {$ from_id }: receive $ data ". PHP_EOL;}); $ serv-> on ('open', function ($ server, $ req) {// echo "server # {$ server-> worker_pid }: handshake success with fd # {$ req-> fd }". PHP_EOL; // echo PHP_EOL;}); $ serv-> on ('message', function ($ server, $ frame) {// echo "message :". $ frame-> data. PHP_EOL; $ msg = json_decode ($ frame-> data, true); switch ($ Msg ['type']) {case 'login': $ server-> push ($ frame-> fd, "welcome ~ "); Break; default: break;} $ msg ['fd '] = $ frame-> fd; $ server-> task ($ msg );}); $ serv-> on ("workerstart", function ($ server, $ workerid) {// echo "workerstart :". $ workerid. PHP_EOL; // echo PHP_EOL;}); $ serv-> on ("task", "on_task"); $ serv-> on ("finish", function ($ serv, $ task_id, $ data) {return;}); $ serv-> on ('close', function ($ server, $ fd, $ from_id) {// echo "connection close :". $ fd. PHP_EOL; // echo PHP_EOL;}); $ serv-> start (); func Tion on_task ($ serv, $ task_id, $ from_id, $ data) {switch ($ data ['type']) {case 'login': $ send_msg = "said: I am coming ~ "; Break; default: $ send_msg =" Description: {$ data ['MSG '] ['speak']} "; break ;} foreach ($ serv-> connections as $ conn) {if ($ conn! = $ Data ['fd ']) {if (strpos ($ data ['MSG'] ['name'], "tourist") = 0) {$ name = $ data ['MSG '] ['name']. "_". $ data ['fd '];} else {$ name = $ data ['MSG'] ['name'] ;}} else {$ name = "I ";} $ serv-> push ($ conn, $ name. $ send_msg);} return;} function on_finish ($ serv, $ task_id, $ data) {return true ;}

Front-end code:

 WebSocket test 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.