U
U
UntitledNikname2020-05-08 01:30:47
PHP
UntitledNikname, 2020-05-08 01:30:47

How to send a message to all clients (tcp://workerman)?

To create a TCP connection, I use the WORKERMAN library. For her, let's look at an example.

use Workerman\Worker;
require_once './Workerman/Autoloader.php';

$tcp_worker = new Worker("tcp://127.0.0.1:8000");
$tcp_worker->count = 4;

$tcp_worker->onMessage = function($connection, $data) use ($tcp_worker)
{
   // №1 Отправляем сообщение обратно отправителю
    $connection->send($data);

  // №2 Рассылаем сообщение всем клиентам
   foreach ($tcp_worker->connections as $conn) {
     $conn->send($data);
  }
};
Worker::runAll();


If the 1st example from the onMessage event works flawlessly, then the 2nd example works only locally (localhost) for some reason, and on the server it returns a message to the sender and that's it, the rest do not receive anything. Because of which ? can add some header or tweak the settings? both locally and on the server everything is identical. there is not even an ssl connection and the same port is used.

PS. testing with telnet command on 2 different devices

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dimonov1, 2020-05-08
@UntitledNikname

and have you tried doing this? create an array and add all connections to it by username, for example, instead of calling $tcp_worker->connections https://ru.stackoverflow.com/questions/850695/%D0%...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question