I was originally planning to continue writing my multi-process tutorial, but today I suddenly wanted to take a look at swoole's websocket. swoole-1.7.9 adds built-in websocket server support. With just a few lines of PHP code, you can write an asynchronous non-blocking multi-process WebSocket server. swoole_websocket_server inherits from swoole_http_server. If the onRequest callback is set, the websocket server can also be used as an http server.
I looked at the demo on the official website and it seemed pretty simple.
I just like this kind of simple and easy to understand demo. The meaning of each line of code can be understood at a glance. The server is ready, I'll find some client js code MDN for Firefox
***The command line runs the php file, and then the browser opens the html file. F12 opens the debugging interface and looks at the console. OK, no problem. At this time, I suddenly thought of something, because in the tutorial on multi-process, the handles of all child processes were stored in the main process for later inter-process communication. Then I will store all the client links into an array. Every time a client sends a message, I will iterate through the client array and send the message to the group. Has the chat room been realized soon? Then, the server code looks like this
Haha, I thought I was done, but I found out I was just being naive. You can try it yourself. After running PHP, open two pages in the browser and see what the contents of console.log are. It worked fine, but it didn't achieve the chat effect we were talking about. Let’s find out the reason. I first wanted to see what was in $map, so I outputted it and found that there was only one element in this map. Oh, that's not right, this is a global variable. Shouldn't there be as many elements as there are client connections? What's going on? Why aren't all client IDs saved? At this point, I couldn't solve the problem of the map variable, so I wanted to see what the fd was. As usual, var_dump output shows that fd is an int type number, and it is self-increasing. This is easy, it's just numbers. So, I do this. I can't save the variable, I can't handle it, so I save it in text. Final version of websocket.php
Open the HTML file again and observe the input on multiple pages. OK, it's OK. Of course, as a chat room, what I wrote is too simple, you can write a better interface yourself (because I am too lazy to write the interface) Also, each chat record should be saved, so that when a new connection comes, the previous chat record will be sent first. In this way, I think the experience will be better. Then, everyone can have a pleasant chat. Haha |
<<: Cocos Play: The best solution for mobile web games
>>: Apple releases iOS 9 fifth test, third public beta
Tofu is a traditional food in my country. It is a...
Author: Global Science At the beginning, we would...
What are the methods of blog promotion ? What are...
The Chinese Valentine's Day in 2020 is coming...
Every marketing plan with soul must not be a pile...
On December 22, Ubuntu Kylin 15.04, a domestic op...
How did the second-tier e-commerce industry perfo...
New Year Greetings Wan Gang, Chairman of the Chin...
In June 2019, long-distance swimmer Ben Lecomte e...
The astronauts of Shenzhou-15 stayed in the space...
On July 5, 1996, on a soft summer night in Roslin...
Zero-cost, zero-threshold money-making project: D...
2016 was a year of great changes for the entire m...
Recently, according to foreign media, some of Chi...
Source: Sina Technology Original title: Tencent i...