In the Internet era, personal privacy security is getting more and more attention. When chatting with friends, for example, people often avoid typing private content for fear that it will be peeped in the background by unscrupulous companies. Faced with this situation, programmers set up their own chat rooms to ensure their privacy.

Normally, to do this, you need to:

  • Set up a server (software + hardware), or in the cloud era, set up a cloud server.
  • Purchase a domain name from a domain name provider.
  • Configure security regulations such as firewalls to prevent hacker attacks/intrusions.
  • Install code, database running environment required, such as LNMP.
  • Create your personal chat room.

Now we should practice our hands, or we will be the giants of speech and the dwarfs of action

Swoole has webSocket server functionality built in since version 1.7.9. With a few simple lines of PHP code, you can create an asynchronous, non-blocking, multi-process WebSocket server. First, we create a new project called Swoole in the Apache workspace and create a ws-server.php file in it. This file creates a websocket server with the user’s request as follows:

<? $ws_server = new swoole_websocket_server('192.168.1.169', 9502); $ws_server = new swoole_websocket_server('192.168.1.169', 9502); $ws_server->set(array('daemonize' => true)); $ws_server->on('open', function ($ws, $request) {file_put_contents(__DIR__.'/log.txt', $request->fd); //$ws->push($request->fd, "Hello, Welcome\n"); }); $ws_server->on('message', function ($ws,$frame) {pushMessage($ws,$frame); }); $ws_server->on('close', function ($ws, $fd) {echo "client-{$fd} is closed\n"; }); $ws_server->start(); Function pushMessage($ws,$frame){$data = $frame->data; $msg = file_get_contents( __DIR__ .'/log.txt'); for ($i=1 ; $i<= $msg ; $i++) { $ws->push($i, $frame->fd.' : '.$data); }}Copy the code

The above code creates a WebSocket server with an IP address of 192.168.1.169 and a port of 9502. This information can be adjusted according to the actual situation.

Create a chat interaction page

Similarly, in the Swoole directory, we create a chat. HTML file, which is a purely static HTML5 page, mainly interacting with the WebSocket server through the HTML5 WebSocket protocol, and its content is as follows:

<! DOCTYPE html> <html> <head> <title></title> <meta charset="UTF-8"> <script type="text/javascript"> if(window.WebSocket){ Var webSocket = new webSocket ("ws://192.168.1.169:9502"); webSocket.onopen = function (event) { //webSocket.send("Hello,WebSocket!" ); }; webSocket.onmessage = function (event) { var content = document.getElementById('content'); content.innerHTML = content.innerHTML.concat('<p style="margin-left:20px; height:20px; line-height:20px;" > user id - '+ event. The data +' < / p > '); } var sendMessage = function(){ var data = document.getElementById('message').value; webSocket.send(data); }}else{console.log(" Your browser does not support WebSocket"); } </script> </head> <body> <div style="width:600px; margin:0 auto; border:1px solid #ccc;" > <div id="content" style="overflow-y:auto; height:300px;" ></div> <hr/> <div style="height:40px"> <input type="text" id="message" style="margin-left:10px; height:25px; width:450px;" > < button &western nclick = "sendMessage ()" style = "height: 28 px; width:75px;" </button> </div> </div> </body> </ HTML >Copy the code

That’s it. Now that we’ve created the two files we need, let’s test to see if it works as expected. 1 Start the WebSocket server

Switch to the root directory of the project, and run the ws-server. PHP script from the PHP command line to start the WebSocket server.

cd /var/www/html/swoole
php ws-server.php
Copy the code

2 Check whether the WebSocket server is started successfully

Enter the command: netstat tunlp | grep, 9502

3 Start a Conversation

Prepare several browsers and enter 192.168.1.169/swoole/ch in each browser, each representing one user, to simulate a group chat.