workerman icon indicating copy to clipboard operation
workerman copied to clipboard

Can you please tell me how the client code in php will look like?

Open Artem2710 opened this issue 1 year ago • 5 comments

Can you please tell me how the client code in php will look like? The client must be able to send data to the server and receive data from other clients

my server

require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;

$users = [];

$ws_worker = new Worker("websocket://0.0.0.0:8000");

$ws_worker->onWorkerStart = function() use (&$users)
{
    $inner_tcp_worker = new Worker("tcp://127.0.0.1:1234");

    $inner_tcp_worker->onMessage = function($connection, $data) use (&$users) {
        $data = json_decode($data);
        var_dump($data);
        foreach($users as $user_connection) {
            if($user_connection->id !== $connection->id) {
                $user_connection->send($data->message);
            }
        }
    };

    $inner_tcp_worker->listen();
};


$ws_worker->onMessage = function($connection, $data) use (&$users) {
    $data = json_decode($data);
    var_dump($data);
    foreach($users as $user_connection) {
        if($user_connection->id !== $connection->id) {
            $user_connection->send($data->message);
        }
    }
};


$ws_worker->onConnect = function($connection) use (&$users)
{
    $connection->onWebSocketConnect = function($connection) use (&$users)
    {
        $users[$connection->id] = $connection;
    };
};

$ws_worker->onClose = function($connection) use(&$users)
{
    unset($users[$connection->id]);
};

// Run worker
Worker::runAll();

example of a client that sends data but does not receive it

$localsocket = 'tcp://127.0.0.1:1234';
$user = 'tester01';
$message = 'test';

$instance = stream_socket_client($localsocket);


$stdin = fopen('php://stdin', 'r');
while (true) {
    $message = trim(fgets($stdin));

    fwrite($instance, json_encode(['user' => $user, 'message' => $message])  . "\n");

}

an example on js

ws = new WebSocket("ws://127.0.0.1:8000/?user=tester01");

const bodyElement = document.getElementById('wrapper');

bodyElement.addEventListener('keyup', event => {

    console.log(123123)
    let positionData = {
        user: 'user',
        message: 'message',
    };

    ws.send(JSON.stringify(positionData));
});

ws.addEventListener('message', (event) => {
    console.log(event.data)
});

Artem2710 avatar Mar 19 '23 20:03 Artem2710

Change

$inner_tcp_worker = new Worker("tcp://127.0.0.1:1234");

to

$inner_tcp_worker = new Worker("text://127.0.0.1:1234");

walkor avatar Mar 20 '23 01:03 walkor

I changed the value tcp to text as you suggested. Now I'm trying to get data using the php client below, but I'm getting an error PHP Fatal error: Uncaught Error: Call to a member function add() on null in /var/www/sockets/workerman_chat/vendor/workerman/workerman/Connection/AsyncTcpConnection.php:216 Stack trace: #0 /var/www/sockets/workerman_chat/client1.php(22): Workerman\Connection\AsyncTcpConnection->connect() #1 {main} thrown in /var/www/sockets/workerman_chat/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 216

client code

use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$server_address = 'text://localhost:1234';

$connection = new AsyncTcpConnection($server_address);

$connection->onMessage = function ($connection, $data) {
    echo "from server: $data\n";
};

$connection->onClose = function ($connection) {
    echo "connection closed\n";
};

$connection->connect();

$stdin = fopen('php://stdin', 'r');
while (true) {
    $message = trim(fgets($stdin));

    if (!empty($message)) {
        $connection->send($message);
    }
}

I will be very grateful if you help me with this

Artem2710 avatar Mar 20 '23 09:03 Artem2710

client codes

$localsocket = 'tcp://127.0.0.1:1234';
$user = 'tester01';
$message = 'test';

$instance = stream_socket_client($localsocket);


$stdin = fopen('php://stdin', 'r');
while (true) {
    $message = trim(fgets($stdin));

    fwrite($instance, json_encode(['user' => $user, 'message' => $message])  . "\n");

}

walkor avatar Mar 21 '23 07:03 walkor

Yes, this code works well for sending data, but can you please advise something about receiving data from the server using php?

Artem2710 avatar Mar 21 '23 08:03 Artem2710

If you want to use workerman as client , the codes like this.

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Workerman\Connection\AsyncTcpConnection;
use Workerman\Connection\TcpConnection;
use Workerman\Worker;

$worker = new Worker();
$worker->onWorkerStart = function($worker)
{
    $server_address = 'text://localhost:1234';
    $connection = new AsyncTcpConnection($server_address);
    $connection->onMessage = function ($connection, $data) {
        echo "from server: $data\n";
    };
    $connection->onClose = function ($connection) {
        echo "connection closed\n";
    };
    $connection->connect();

    $stdin = new TcpConnection(STDIN);
    $stdin->onMessage = function($stdin, $message) use ($connection) {
        echo "send $message\n";
        $connection->send($message);
    };
};
Worker::runAll();

walkor avatar Mar 23 '23 04:03 walkor