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


Today, I’m going to make a simple chat room, which supports changing rooms and supporting private letters. The code I wrote is a bit messy, and there are many bad places in it. After all, I’m 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 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’s a video of a demonstration (there’s no advertisement for BiliBili). You can have a look at it,
You can also download the source code directly to GitHub. It’s absolutely useful. Find me if it’s not easy
Change my IP address to your IP address
I have made three rooms here. The first time I log in is in the public chat room. I can switch to the literati area at will. Of course, I can choose to go to the dead fat house
The process is almost like this:

  1. Log in to 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, save the users in the session
       The information is sent to the swoole server and registered. Then it returns the successful or failed registration and the current online user list
       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 cannot send a message to the current group. If you register, go to the website background to verify the session verification code and the user name to be used

    And timestamp and salt for sha256 information summary. Then request registration from the swoolewebsocket server. At this time, the server will determine if the first six letters of user information
       It is tokenr and is not registered at present. If the token verification is successful, it will be inserted into the redis database, and the customer will be notified of the successful registration, and then all users in the customer's group will be notified
       Add users (that is, the current online column)
       The code is almost like this

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

  2. At this time, the login is successful, and you can switch groups at will. The number of users who switch groups will be reset every time. Because according to the rule of swoole, as long as the connection is disconnected,
    The next connection is a new connection. For example, if the previous socket number is 1, then the next connection after disconnection is 2. But according to the session, the user name can be saved,
    The user name after refreshing is the same as before

    People in the public chat room can only be seen by people in the public chat room, and only literati in the literati area can be seen by literati


    Only the other party can see the private letter chat, but no one else can see it. This is mainly to write the first six letters of the sent message as SendTo
       The code flow is almost like this

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

Oh, I found that the process is so few. Let’s see the backstage

In the background, I created a new class called webs. This class inherits swoolewebsocketserver
And add some logical methods
Mainly as follows:

1. Checkregistername() to check 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
   Prototypes 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 stick GitHub. The source code is annotated
 5. Checktoken () to check whether the token is correct
 6. Judge the message sent by the user and analyze what he wants

No. isn’t it super simple? It’s so simple with swoole. And it’s developed by Chinese people! You can learn from swoole official website. There are many documents, and swoole has powerful functions,
I only read the chapter of websocket, but I didn’t read the rest, because I can’t understand it! Besides, the common use of redis is very simple. I just went to mooc.com and watched the video for 20 minutes. > http://www.imooc.com/video/14315 I use redis as MySQL here
Then I only use the hash type and two or three functions. I can’t do anything else. I haven’t done anything like lock and read-only
Phpredis extended Baidu installation
This is a one-time completion. I started to do it last night. In the morning, if there is no inspection, I can see that it can operate normally. If there is no abnormal information on the console, it will be sent
If you have problems, I can solve them. In addition, please support domestic excellent works swoole! Http://swoole.com
Swoole official QQ communication group 193772828 I belong to the “food driven” type in it. If you have problems learning swoole, you can go to the group to communicate
Say the video address again (7-minute demo)

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