search
HomePHP FrameworkThinkPHPThink-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

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

<?php
declare (strict_types = 1);
namespace app\listener;
class WsConnect
{
    /**
     * 事件监听处理
     *
     * @return mixed
     * 受用 WebSocket 客户端连接入口
     */
    public function handle($event)
{
        //实例化 Websocket 类
        $ws = app(&#39;\think\swoole\Websocket&#39;);
        //
        $ws -> emit(&#39;sendfd&#39;,$ws -> getSender());
    }
}

app/listener/ WsTest.php

<?php
declare (strict_types = 1);
namespace app\listener;
use \think\swoole\Websocket;
class WsTest
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event,Websocket $ws)
{
        $ws -> to(intval($event[&#39;to&#39;])) -> emit(&#39;testcallback&#39;,$event[&#39;message&#39;]);
    }
}

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

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
消息:<input type="text" id="message">
接收者:<input type="text" id="to">
<button onclick="send()">发送</button>
<script>
    var ws = new WebSocket("ws://127.0.0.1:9501/");
    ws.onopen = function(){
        console.log(&#39;连接成功&#39;);
    }
    //数据返回的解析
    function mycallback(data){
        var start = data.indexOf(&#39;[&#39;) // 第一次出现的位置
        var start1 = data.indexOf(&#39;{&#39;)
        if(start < 0){
            start = start1;
        }
        if(start >= 0 && start1 >= 0){
            start = Math.min(start,start1);
        }
        if(start >= 0){
            console.log(data);
            var json = data.substr(start); //截取
            var json = JSON.parse(json);
            console.log(json);
        }
    }
    ws.onmessage = function(data){
        // console.log(data.data);
        mycallback(data.data);
    }
    ws.onclose = function(){
        console.log(&#39;连接断开&#39;);
    }
    function send()
{
        var message = document.getElementById(&#39;message&#39;).value;
        var to = document.getElementById(&#39;to&#39;).value;
        console.log("准备给" + to + "发送数据:" + message);
        ws.send(JSON.stringify([&#39;test&#39;,{
            to:to,
            message:message
        }])); //发送的数据必须是 [&#39;test&#39;,数据] 这种格式
    }
</script>
</body>
</html>

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

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
消息:<input type="text" id="message">
接收者:<input type="text" id="to">
<button onclick="send()">发送</button>
<script src="./socketio.js"></script>
<script>
    //http 协议
    var socket = io("http://127.0.0.1:9501", {transports: [&#39;websocket&#39;]});
    socket.on(&#39;connect&#39;, function(){
        console.log(&#39;connect success&#39;);
    });
    socket.on(&#39;close&#39;,function(){
       console.log(&#39;connect close&#39;)
    });
    //send_fd 为自定义的场景值,和后端对应
    socket.on("sendfd", function (data) {
        console.log(data)
    });
    //testcallback 为自定义的场景值,和后端对应
    socket.on("testcallback", function (data) {
        console.log(data)
    });
    function send() {
        var message = document.getElementById(&#39;message&#39;).value;
        var to = document.getElementById(&#39;to&#39;).value;
        socket.emit(&#39;test&#39;, {
            //属性可自行添加
            to:to,
            message:message
        })
    }
</script>
</body>
</html>

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

app/listener/WsConnect.php

<?php
declare (strict_types = 1);
namespace app\listener;
class WsConnect
{
    /**
     * 事件监听处理
     *
     * @return mixed
     * 受用 WebSocket 客户端连接入口
     */
    public function handle($event)
{
        //实例化 Websocket 类
        $ws = app(&#39;\think\swoole\Websocket&#39;);
        //
        $ws -> emit(&#39;sendfd&#39;,$ws -> getSender());
    }
}

app/listener/WsTest.php

<?php
declare (strict_types = 1);
namespace app\listener;
use \think\swoole\Websocket;
class WsTest
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event,Websocket $ws)
{
//        $ws -> to(intval($event[&#39;to&#39;])) -> emit(&#39;testcallback&#39;,$event[&#39;message&#39;]);
        $ws -> to(intval($event[&#39;to&#39;])) -> emit(&#39;testcallback&#39;,[
            &#39;form&#39; => [
                &#39;id&#39; => 10,
                &#39;fd&#39; => $ws -> getSender(),
                &#39;nickname&#39; => &#39;张三&#39;
            ],
            &#39;to&#39; => [
                &#39;id&#39; => 11,
                &#39;fd&#39; => intval($event[&#39;to&#39;]),
                &#39;nickname&#39; => &#39;李四&#39;
            ],
            &#39;massage&#39; => [
                &#39;id&#39; => 888,
                &#39;create_time&#39; => &#39;2020-03-13&#39;,
                &#39;content&#39; => $event[&#39;message&#39;]
            ]
        ]);
    }
}

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: [&#39;websocket&#39;]});

The back-end can Bind in the connection event:

app/listener/WsConnect.php

<?php
declare (strict_types = 1);
namespace app\listener;
class WsConnect
{
    /**
     * 事件监听处理
     *
     * @return mixed
     * 受用 WebSocket 客户端连接入口
     */
    public function handle($event)
{
        // $event 为请求对象
        //实例化 Websocket 类
        $ws = app(&#39;\think\swoole\Websocket&#39;);
        //获取 uid
        $uid = $event -> get(&#39;uid&#39;);
        //获取 fd
        $fd = $ws -> getSender();
        //获取到 uid 和 fd 后,可以存数据库,内存或者 redis
        $ws -> emit(&#39;sendfd&#39;,[
            &#39;uid&#39; => $uid,
            &#39;fd&#39; => $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
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor