Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Ping Frame" is recieved by "Swoole WebSocket Server" from Client Code BUT "Pong Frame" is not going back from Swoole Server to the Swoole Client #5471

Closed
fakharksa opened this issue Sep 8, 2024 · 1 comment

Comments

@fakharksa
Copy link

fakharksa commented Sep 8, 2024

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.

// WebSocket Server-side Code

if ($frame->opcode === WEBSOCKET_OPCODE_PING) { // WEBSOCKET_OPCODE_PING is 0x09
                echo "Ping frame received: Code {$frame->opcode}\n"; // Ref.: See Output-1 after code snippet
                // Reply with Pong frame
                $pongFrame = new Swoole\WebSocket\Frame;
                $pongFrame->opcode = WEBSOCKET_OPCODE_PONG;
                var_export($pongFrame); // Ref.: See Output-2
                $webSocketServer->push($frame->fd, $pongFrame);
            } 

Output-1:
Ping frame received: Code 9

Output-2:

 \Swoole\WebSocket\Frame::__set_state(array(
   'fd' => 0,
   'data' => '',
   'opcode' => 10,
   'flags' => 1,
   'finish' => NULL,
))

// Client-side Code

include 'wbsocketclient.php';
$ip = '127.0.0.1';
$w = new WebSocketClient($ip, 9501);

if ($x = $w->connect()) {
    
    $w->send('end', 'ping', 1); // This is working, as ping is recieved by the server, so no issue here
    
    while(true) {
        $data = $w->recv();
        
        echo PHP_EOL;
        var_export($data); // Output: Gives false
        echo PHP_EOL;

        if ($data) {
            echo "PHP_EOL";
            var_export($data->opcode);
            sleep(1);
        } else {
            break;
        }
        
    }
}   

I am using WebSocket Server with following swoole's configuration (as configuration array's keys) defined :

        'open_websocket_ping_frame' => true, // added from v4.5.4
        'open_websocket_pong_frame' => true,
  1. What did you expect to see?

var_export of the Pong Frame on Client Terminal

  1. What did you see instead?

null value

  1. What version of Swoole are you using (show your php --ri swoole)?
swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 5.1.3
Built => Aug 17 2024 18:55:47
coroutine => enabled with thread context
debug => enabled
trace_log => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 3.0.2 15 Mar 2022
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
c-ares => 1.18.1
zlib => 1.2.11
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled
coroutine_pgsql => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => On => On
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?

uname -a

Linux fakhar-HP-Laptop 5.17.5-76051705-generic #202204271406~1651504840~20.04~63e51bd-Ubuntu SMP PREEMPT Wed Ma x86_64 x86_64 x86_64 GNU/Linux

php -v

PHP 8.3.11 (cli) (built: Aug 30 2024 09:27:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.11, Copyright (c), by Zend Technologies

gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
@matyhtf
Copy link
Member

matyhtf commented Sep 24, 2024

Please provide your WebSocket client code, or switch to using the one provided by Swoole.

My test was correct.

Server

<?php
$server = new Swoole\Websocket\Server('127.0.0.1', 9502);

$server->set([
    'open_websocket_ping_frame' => true,
    'open_websocket_pong_frame' => true,
]);

$server->on('start', function ($server) {
    echo "Websocket Server is started at ws://127.0.0.1:9502\n";
});

$server->on('open', function ($server, $req) {
    echo "connection open: {$req->fd}\n";
});

$server->on('message', function ($server, $frame) {
    echo "received message: {$frame->data}\n";
    if ($frame->opcode === WEBSOCKET_OPCODE_PING) { // WEBSOCKET_OPCODE_PING is 0x09
        echo "Ping frame received: Code {$frame->opcode}\n"; // Ref.: See Output-1 after code snippet
        // Reply with Pong frame
        $pongFrame = new Swoole\WebSocket\Frame;
        $pongFrame->opcode = WEBSOCKET_OPCODE_PONG;
        $pongFrame->finish = true;
        $pongFrame->data = 'hello';
        var_export($pongFrame); // Ref.: See Output-2
        $server->push($frame->fd, $pongFrame);
    } else {
        $server->push($frame->fd, json_encode(['hello', 'world']));
    }
});

$server->on('close', function ($server, $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();

Client

<?php

use Swoole\Coroutine\Http\Client;

Co\run(function () {
    $cli = new Client('127.0.0.1', 9502);
    $cli->upgrade('/');
    $cli->push('hello', WEBSOCKET_OPCODE_PING);
    $frame = $cli->recv();
    var_dump($frame->opcode == WEBSOCKET_OPCODE_PONG);
    var_dump($frame);
});

Output

Server

php ws.php 
Websocket Server is started at ws://127.0.0.1:9502

connection open: 1
received message: hello
Ping frame received: Code 9
\Swoole\WebSocket\Frame::__set_state(array(
   'fd' => 0,
   'data' => 'hello',
   'opcode' => 10,
   'flags' => 1,
   'finish' => true,
))connection close: 1

Client

php client_ws.php 
bool(true)
object(Swoole\WebSocket\Frame)#5 (5) {
  ["fd"]=>
  int(5)
  ["data"]=>
  string(5) "hello"
  ["opcode"]=>
  int(10)
  ["flags"]=>
  int(1)
  ["finish"]=>
  bool(true)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants