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
Evergrande Chairman Xu Jiayin spent nearly HK$7 b...
Author: The Nutcracker Studio Reviewer: Tang Yich...
WeChat marketing promotion focuses on thinking an...
Nowadays, due to increasing stress on people, the ...
On a hot summer night, nothing is more relaxing t...
With the popularity of the live streaming industr...
On September 9, 2024, BYD's 2025 Han DM-i and...
How to accurately attract traffic in private doma...
After working in self-media operation for a long ...
High-power fast charging is gaining popularity ve...
Produced by: Science Popularization China Produce...
The storm is coming, New Jersey has a curfew~ Wri...
The main charging standard for server hosting is ...
Brief introduction of Bianfeng Weiweiweidaolai + ...
Event operation is a job that most operations per...