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

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

Open
Artem2710 opened this issue Mar 19, 2023 · 5 comments
Open

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

Artem2710 opened this issue Mar 19, 2023 · 5 comments

Comments

@Artem2710
Copy link

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)
});
@walkor
Copy link
Owner

walkor commented Mar 20, 2023

Change

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

to

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

@Artem2710
Copy link
Author

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

@walkor
Copy link
Owner

walkor commented Mar 21, 2023

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");

}

@Artem2710
Copy link
Author

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

@walkor
Copy link
Owner

walkor commented Mar 23, 2023

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();

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

No branches or pull requests

2 participants