Just contacted the socket, server has started to listen, but websocket can not connect to the server, what is the reason?

clipboard.png

clipboard.png

clipboard.png

js Code:

var socket = new WebSocket("ws://47.94.80.240:12345");
socket.onopen = function (evt) {
    console.log("socket");
    //
    var mes = {
        type: 1
    }
    socket.send(JSON.stringify(mes));
};

part of php code:

public function startServer()
{
    // $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
   $this->master = socket_create_listen($this->port);
    if (!$this->master) {
        throw new \Exception("listen $this->port fail !");
    }


    $this->runLog("Server Started : " . date("Y-m-d H:i:s"));
    $this->runLog("Listening on   : 47.94.80.240 port " . $this->port);
    $this->runLog("Master socket  : " . $this->master . "\n");

    self::$connectPool[] = $this->master;

    while (true) {
        $readFds = self::$connectPool;
        //
        @socket_select($readFds, $writeFds, $e = ull, $this->timeout);

        foreach ($readFds as $socket) {
            // 
            if ($this->master == $socket) {

                $client = socket_accept($this->master);  //
                $this->handShake = false;

                if ($client < 0) {
                    $this->log("clinet connect false!");
                    continue;
                } else {
                    //
                    if (count(self::$connectPool) > self::$maxConnectNum)
                        continue;

                    //
                    $this->connect($client);
                }

            } else {
                //,
                $bytes = @socket_recv($socket, $buffer, 2048, 0);
                //
                if ($bytes == 0) {
                    $this->disConnect($socket);
                } else {
                    // 
                    if (!$this->handShake) {

                        $this->doHandShake($socket, $buffer);
                    } else {

                        //
                        $buffer = $this->decode($buffer);
                        $this->parseMessage($buffer, $socket);
                    }
                }

            }
        }

    }
}
Apr.19,2021

socket and websocket are not the same thing.
websocket is a higher-level protocol, analogous to the http protocol.
I remember that the first step in a websocket connection is to require an http request.

I forgot the details, too. Just give me an idea. You can study the difference between them first.


A pit of Ali CVM. You need to add a security group

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf335c-31e6b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf335c-31e6b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?