Swoole super simple to build chat room. Group chat, group chat, single chat


Today to do a simple chat room, support change room, support private letter. Write the code a bit slag, there are many not very good place. After all, I am just a wild program ape

Environmental Science:
 apache 2.4
 swoole 1.9
 redis  4.0

bilibili : https://www.bilibili.com/vide…
GitHub address:https://github.com/buffge/buf…

It looks like this:

Swoole super simple to build chat room. Group chat, group chat, single chat

I don’t want to send the code to my server, because this project is too small. It’s rubbish. And I’m afraid of being attacked
Here is a demo video (there is no advertisement in BiliBili). You can have a look at it,
You can also download the source code directly to GitHub, which is absolutely useful
Change my IP address to your IP address
I’ve made three rooms here. The first time I log in is in the public chat room. I can switch to the literati section at will. Of course, I can also choose to go to the dead fat house section
The process is almost like this:

  1. Log into a chat room

    1.1 trigger the open event in the background. At this time, judge whether it is the first login or the refresh after registration. If it is the registration refresh, the user saved in the session will be saved
       The information is sent to the spool server and registered. Then the success or failure of registration and the current online user list are returned
       If it is a new user, it will directly send the list of all users in the current chat room to the client. Both will insert new data into the redis database

    1.2 at this time, if you do not register, you will not be able to send messages to the current group. If you register, go to the website background to verify the session verification code and the user name you will use

    Then, the server will request registration from the swoolewebsocket server. At this time, the server will determine if the first six letters of the user information are invalid
       It is a token R and is not registered at present. If the token is verified successfully, it will be inserted into the redis database, and the customer will be notified that the registration is successful, and then all users in the customer's group will be notified
       Add user (the current online column)
       The code is pretty much like this

    Swoole super simple to build chat room. Group chat, group chat, single chat

  2. At this time, you have successfully logged in. You can switch groups at will. Each time you switch groups, the number of the user will be reset. Because this is the rule of spool, just disconnect the connection,
    The next connection is a new connection. For example, if the previous socket number is 1, then the next connection after disconnection is 2. However, according to the session judgment, the user name can be saved,
    After refreshing, the user name will be the same as before

    People in public chat room can only see people in public chat room, and only literati can see people in literati area


    Only the other party can see the private message, but no one can see it. The main thing is to write the first six letters of the message as SendTo
       The code flow is almost like this

    Swoole super simple to build chat room. Group chat, group chat, single chat

Oh, I found that the process is very little. Let’s watch the backstage

The backstage is mainly a new class called webs, which inherits swoolewebsocketserver
And add some logic methods
The main contents are as follows:

1. Checkregistername() checks whether the user name can be used   
   Prototype checkregistername (\ redis $redis, string $user)_ name, \buff\string  $reload) : boolean
2. Sendtoperson() sends a message to a single user
   Prototype sendtoperson (integer $FD, string $MES, integer $type, string $SendTo = ', string $user_ name = "", \Redis  $redis = null)
3. Sendtogroup() sends a message to a single group
   Prototype sendtogroup (int $framefd, redis $redis, string $user)_ name, int $type, string $mes = "", string $group = "public")
 4. Getonlineuserslist() gets all the current registered user list prototypes, so I won't write them. I'll post GitHub. Source code comments below
 5. Checktoken() check whether the token is correct
 6. Judgemeseventtype () judges the message sent by the user and analyzes what he wants

No. isn’t it super simple. With spool, it’s so simple. And this is developed by Chinese people! You can learn from the official website of spoole. There are many documents, and the function of spoole is powerful,
I only read the chapter of websocket, but I didn’t read the rest, because I didn’t understand it! And the common usage of redis is very simplehttp://www.imooc.com/video/14315Here I use redis as mysql
Then only the hash type is used, two or three functions are used, and the others are not available. There is no such thing as locking read-only
Phpredis extension Baidu installation
This is a one-time completion, starting from last night, to do the morning, no inspection, I can see the normal operation, no abnormal information on the console sent up
If you encounter problems, I can solve it. In addition, please support the excellent domestic works spool!http://swoole.com
Swoole official QQ communication group 193772828, I belong to the kind of dish force. If you have problems in learning swoole, you can go to the group to communicate
Video address again (7 minutes demo)

bilibili : https://www.bilibili.com/vide…
GitHub address:https://github.com/buffge/buf…