workerman
workerman copied to clipboard
Can you please tell me how the client code in php will look like?
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)
});
Change
$inner_tcp_worker = new Worker("tcp://127.0.0.1:1234");
to
$inner_tcp_worker = new Worker("text://127.0.0.1:1234");
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
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");
}
Yes, this code works well for sending data, but can you please advise something about receiving data from the server using php?
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();