Actual combat spool [chat room]


Foreword: after understanding the concept, we should practice our hands, otherwise it is giant baby

If you get something, pleaseAdd a little starIf you don’t get anything, you canopposition It didn’t help reportSan Lian


  • You need to see it firstNew knowledge of spoolTo understand the basic server websocket usage
  • Simple use of JS websocket client


#Command line 1


Official example

$server = new swoole_ websocket_ server("", 9501);


  • swoole_ websocket_ Server inherits from spool_ http_ server

    • With onrequest callback set, websocket server can also act as HTTP server at the same time
    • If onrequest callback is not set, websocket server will return HTTP 400 error page after receiving HTTP request
    • If you want to trigger the push of all WebSockets by receiving HTTP, you need to pay attention to the scope problem. For the process, please use global to push the spool_ websocket_ Server reference, object-oriented can be swoole_ websocket_ Set server as a member property
  • function onOpen(swoole_websocket_server $svr, swoole_http_request $req);

    • When the websocket client establishes a connection with the server and finishes the handshake, it will call back this function.
    • $req is an HTTP request object, which contains the handshake request information sent by the client
    • In the onopen event function, you can call push to send data to the client or call close to close the connection
    • The onopen event callback is optional
  • function onMessage(swoole_websocket_server $server, swoole_websocket_frame $frame)

    • This function is called back when the server receives a data frame from the client.
    • $frame is a spool_ websocket_ The frame object contains the data frame information sent by the client
    • Onmessage callbackMust be set, the server cannot be started if it is not set
    • The ping frame sent by the client will not trigger onmessage, and the bottom layer will automatically reply to the pong packet
  • swoole_ websocket_ Frame property

    • $frame->fd, the socket ID of the client, using$server->pushNeed to use when pushing data
    • $frame->dataData content, which can be text content or binary data, can be judged by the value of opcode
    • $frame->opcodeFor the opcode type of websocket, please refer to the websocket protocol standard document
    • $frame->finishIndicates whether the data frame is complete or not. A websocket request may be divided into multiple data frames for transmission (the bottom layer has implemented automatic data frame merging, so now you don’t have to worry about the incomplete data frame received)

Chat room server example

Directory structure:

  • config
    • socket.php
  • src
    • websocket
      • Config.php
      • run.php
      • Websocketserver.php memory table version
      • Wsredisserver.php redis version

Websocketserver.php memory table version

<? php

Wsredisserver.php redis version

<? php


<? php


<? php


require __DIR__ . '/../bootstrap.php';

$server = new App\WebSocket\WebSocketServer();



Complete example:chat room

After learning, I found that the so-called chat room in my life is just like this. Of course, this is only a simple demo, and many functions have not been realized. If you want to further study, you can go to GitHub to find a complete project for in-depth study

reference resources

This work adoptsCC agreementReprint must indicate the author and the link of this article