Develop simple chat rooms with swoole and websocket

Source: Internet
Author: User
Tags sessionstorage

First of all, I want to say some of the habits of code, first, any configurable parameters or variables are written to a config file. Second, the code must have a log record and perfect error and record the error. Anyway, swoole should be what every phper must know, and it claims to have redefined PHP. This chat room improves program performance by leveraging the Swoole high concurrency and asynchronous non-blocking features.

First, define a swoole_lock and swoole_websocket_server , and configure the parameters, details of the specific parameters can go to Swoole official website to view.

 Publicfunction Start ()
{ $ This-Lock=NewSwoole_lock (Swoole_mutex); Lock operation on a file or array has reached synchronization $ This->server =NewSwoole_websocket_server ($ This->ADDR, $ This-port); Swoole provided by WebSocket Server $ This->server->Set(Array ('daemonize'=0, 'Worker_num'=4, 'Task_worker_num'=Ten, 'max_request'= +, 'Log_file'= = Root_path.'Storage\\logs\\swoole.log'//swoole log path, must be an absolute path )); $ This->server->on ('Open', Array ($ This,'OnOpen')); $ This->server->on ('message', Array ($ This,'OnMessage')); $ This->server->on ('Task', Array ($ This,'Ontask')); $ This->server->on ('Finish', Array ($ This,'onfinish')); $ This->server->on ('Close', Array ($ This,'OnClose'));
Start Service $ This->server->start ();}

When there is a client link, simply record the client's information.

         Publicfunction OnOpen ($server, $request) {$message=Array ('REMOTE_ADDR'= $request->server['REMOTE_ADDR'],                'Request_time'= = Date ('y-m-d h:i:s', $request->server['Request_time'])            );        Write_log ($message); }

When a client sends a message, the information is processed.

         Publicfunction OnMessage ($server, $frame) {$data= Json_decode ($framedata); Switch($datatype) {                 Case 'Init':                 Case 'INIT':                    $ This->users[$frame->FD] = $datamessage; Record the information for each link, and don't try to print it out, because you can only see your own link information $message='Welcome'. $data->message.'joined the chat room'; $response=Array ('type'=1,//1 for system messages, 2 for user chat                        'message'=$message);  Break;  Case 'Chat':                 Case 'CHAT': $message= $datamessage; $response=Array ('type'=2,//1 for system messages, 2 for user chat                        'username'= = $ This->users[$frameFD],'message'=$message);  Break; default:                    return false; }
Handing information to task processing $ This->server->task ($response); } Publicfunction Ontask ($server, $task _id, $from _id, $message) {
Iterate through all the client links and push the message over. (If you try to print the $ this->server->connections out, then you will find that he is empty.) But when we used foreach to Loop, it really worked. )foreach($ This->server->connections as$fd) { $ This->server->push ($FD, Json_encode ($message)); } $server->finish ('Task'. $task _id.'finished' . PHP_EOL); }

Finally, when the client disconnects, the client information is deleted synchronously, and the log is logged, using the lock mechanism.

         Publicfunction OnClose ($server, $fd) {$username= $ This-users[$FD]; //release the client and synchronize with the lock$ This-Lock-Lock(); Unset ($ This-users[$FD]); $ This-Lock-unlock (); if($username) {$response=Array ('type'=1,//1 for system messages, 2 for user chat                    'message'= $username.'left the chat room.'                ); $ This->server->task ($response); } write_log ($FD.'Disconnected'); }

Service end, the following is the client, very simple, just need to use WebSocket link ok!

        //WebSocketLet address ='ws://<?php echo client_connect_addr.':' . Client_connect_port?>'; Let WebSocket=NewWebSocket (address); Websocket.onerror= function (Event) {alert ('Server connection error, please retry later');        }; Websocket.onopen= function (Event) {            if(!sessionstorage.getitem ('username') ) {setName (); }Else{username= Sessionstorage.getitem ('username') Websocket.send (json.stringify ({'message': Username,'type':'Init'                }));        }        }; Websocket.onmessage= function (Event) {Console.log (Event); Let Data= Json.parse (Event. Data); if(Data.type = =1) {                $('#chat-list2'). Append ('<li class= "UI-BORDER-TB" ><span class= "username" > System messages: </span><span class= "Message" > '+ Data.message +'</span></li>'); } Else if(Data.type = =2) {                $('#chat-list2'). Append ('<li class= "UI-BORDER-TB" ><span class= "username" >'+ Data.username +': </span><span class= "message" >'+ Data.message +'</span></li>');        }        }; Websocket.onclose= function (Event) {alert ('let's go, the servers are off.'); };

Detailed code can go to my github download

Develop simple chat rooms with swoole and websocket

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.