Home  >  Article  >  PHP Framework  >  Think-Swoole's WebSocket client message parsing and using SocketIO to process user UID and fd association

Think-Swoole's WebSocket client message parsing and using SocketIO to process user UID and fd association

藏色散人
藏色散人forward
2020-10-20 13:43:592790browse

Think-Swoole's WebSocket client message parsing and using SocketIO to process user UID and fd association

Parsing of WebSocket client messages

We demonstrated earlier that when the client connects to the server, a connection event will be triggered. In the event, we require Returns the fd of the current client. When the client sends a message to the server, the server will send the message to the client of the specified fd according to our rules:

app/listener/WsConnect.php

 emit('sendfd',$ws -> getSender());
    }
}

app/listener/ WsTest.php

 to(intval($event['to'])) -> emit('testcallback',$event['message']);
    }
}

After the client executes the above two events, the console prints out the following information:

Think-Swooles WebSocket client message parsing and using SocketIO to process user UID and fd association

There are some numbers in front of the return information, 40, What does 42 mean?

Because the extension we use is based on the SocketIO protocol, these numbers can be understood as the protocol code.

Open /vendor/topthink/think-swoole/src/websocket/socketio/Packet.php, there is the following content:

Think-Swooles WebSocket client message parsing and using SocketIO to process user UID and fd association

The above is the Socket type, The following is the engine. The two code names before and after are pieced together to get:

40:”MESSAGE CONNECT”
42:”MESSAGE EVENT”

Combining these codes, you can know the general operation of messages in SocketIO.

Through the messages printed out from the console, we found that these messages cannot be used directly and need to be intercepted and processed:

test.html




    
    Document


消息:
接收者:



Parsed data:

Think-Swooles WebSocket client message parsing and using SocketIO to process user UID and fd association

Use SocketIO to process message business

For related knowledge of SocketIO, you can view the documentation, focusing on client knowledge:

https:/ /www.w3cschool.cn/socket/socket-k49j2eia.html

iotest.html




    
    Document


消息:
接收者:




var socket = io("http://127.0.0.1:9501", {transports: ['websocket']}); The second parameter specifies the protocol to be upgraded.

app/listener/WsConnect.php

 emit('sendfd',$ws -> getSender());
    }
}

app/listener/WsTest.php

 to(intval($event['to'])) -> emit('testcallback',$event['message']);
        $ws -> to(intval($event['to'])) -> emit('testcallback',[
            'form' => [
                'id' => 10,
                'fd' => $ws -> getSender(),
                'nickname' => '张三'
            ],
            'to' => [
                'id' => 11,
                'fd' => intval($event['to']),
                'nickname' => '李四'
            ],
            'massage' => [
                'id' => 888,
                'create_time' => '2020-03-13',
                'content' => $event['message']
            ]
        ]);
    }
}

Open two clients, fd are 5 and 6:

Think-Swooles WebSocket client message parsing and using SocketIO to process user UID and fd association

In WsConnect.php, there is $ws -> emit('sendfd',$ws -> getSender()); The scene value corresponding to the send fd message is "sendfd" , in iotest.html, there is socket.on("sendfd", function (data) {console.log(data)}); this code, which also has the scene value "sendfd", this line of code can directly obtain the corresponding scene value information, so the fd value will be printed on the console.

Use fd 5 to send information to fd 6:

Think-Swooles WebSocket client message parsing and using SocketIO to process user UID and fd association

Both clients will receive the information:

Think-Swooles WebSocket client message parsing and using SocketIO to process user UID and fd association

It can be seen that the message has been parsed, because the message is sent in WsTest.php to specify the scene value testcallback, and in iotest.html, socket.on("testcallback", function (data){console.log(data)}); can be used Get the parsed results directly.

This shows the convenience of SocketIO in receiving client messages.

Binding of user UID and client fd

In the previous examples, messages are sent to the client by specifying fd. In actual scenarios, it is impossible for us to determine the sending object through fd. , because fd is not fixed, so the user's UID needs to be bound to the client's fd, and then the fd can be selected to complete the sending of the message.

You only need to add the UID parameter to the HTTP connection of the front-end page:

test.html

var ws = new WebSocket("ws://127.0.0.1:9501/?uid=1");

iotest.html

var socket = io("http://127.0.0.1:9501?uid=1", {transports: ['websocket']});

The back-end can Bind in the connection event:

app/listener/WsConnect.php

 get('uid');
        //获取 fd
        $fd = $ws -> getSender();
        //获取到 uid 和 fd 后,可以存数据库,内存或者 redis
        $ws -> emit('sendfd',[
            'uid' => $uid,
            'fd' => $fd
        ]);
    }
}

With UID and fd, you can update the database after each successful connection, and then restart after the connection is disconnected. Clear the user's FD. If the server is restarted, the corresponding relationship between the two will be useless, so there is no need to store it in the database. It is best to store it in Redis. It is also a good choice to map the relationship between the two through the Hash of Redis.

The above is the detailed content of Think-Swoole's WebSocket client message parsing and using SocketIO to process user UID and fd association. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:阿dai哥. If there is any infringement, please contact admin@php.cn delete