Code Monkey home page Code Monkey logo

Comments (14)

Hywan avatar Hywan commented on May 18, 2024

Hello :-),

So let's start by question 2 (the first one, because you have two 2 😉). Each connection is represented by a node. Maybe this Section of the documentation might help you, http://hoa-project.net/En/Literature/Hack/Websocket.html#Customized_node.

Next, question 1. PHP is synchronous by nature. You cannot listen to incoming message while having another execution where you send messages. This is possible if you use pthread for instance or some tricks to get a pseudo-asynchronous execution with generators and co-routines.

Finally, question 3. Answered with the previous paragraph.

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

1) I think you may have misunderstood me, will try to explaine.

  • when page is rendered I create connection with socketServer (send user id to server)
  • SocketServer runs under Supervisord, with while(true){} loop
  • SocketServer iterate each node, and checking message for each user by his id received at the first step
  • I have 2500 clients that working at the same time
    Thats what I need.

2) Yes, I think it will be help, need to try, thanks.

3) I understand, but how you can solved this problem, if you need answer each client every minute?
Run two process, one for read and one for send or will sending message from client each minute, or some another way?

from websocket.

Hywan avatar Hywan commented on May 18, 2024

So you want that:

  1. the server replies to the client when a new message is coming,
  2. but also sending spontaneously messages to every client each x ticks.

Is it correct?

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

Yes and yes.

I think, I can extend the loop circul, and call once in 3 seckonds my method that sending data to clients.

Whay You think abut this?

from websocket.

Hywan avatar Hywan commented on May 18, 2024

You cannot have 2 infinite loops inside the same thread. When calling the famous run method, an infinite loop is starting. If you are starting another infinite loop inside a listener, this will not work.

I recommend 2 solutions: Using pthread, or using generators/co-routines libraries to simulate asynchronous execution. Most people uses pthread for this usages.

Or, you can use 2 servers.

from websocket.

Hywan avatar Hywan commented on May 18, 2024

Did you have your answer or should we keep this issue open?

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

Can we create something like this: https://github.com/Shkarbatov/WebSocketPHPWorkerman/blob/master/worker.php ?

from websocket.

Hywan avatar Hywan commented on May 18, 2024

This is almost the actual API of Hoa\Websocket\Server or Hoa\Websocket\Client. You add listeners, and you run the instance.

What do you mean?

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

Can I send data from server to client with Hoa like I do this with workerman?
Not sending every second request from web through WebSockets to server.

from websocket.

Pierozi avatar Pierozi commented on May 18, 2024

Hello @Shkarbatov, Yes, as long you save the client (fro source bucket) during client established connection to the server.

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

Thanks for answer, do you have any example?

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

Trying next:

Worker:

use Hoa\Websocket\Server as WebsocketServer;
use Hoa\Socket\Server as SocketServer;
use Hoa\Event\Bucket;

$subscribedTopics = array();

// =================================

$websocket_php = new WebsocketServer(
    new SocketServer('ws://127.0.0.1:8009')
);

$websocket_php->on('open', function (Bucket $bucket) { });

$websocket_php->on('message', function (Bucket $bucket) use (&$subscribedTopics) {
    $data = json_decode($bucket->getData()['message'], true);

    if (isset($data['user']) and isset($subscribedTopics[$data['user']])) {
        $subscribedTopics[$data['user']]->getSource()->send($data['command']);
    }
});

// =================================

$websocket_web = new WebsocketServer(
    new SocketServer('ws://127.0.0.1:8008')
);

$websocket_web->on('open', function (Bucket $bucket) use (&$subscribedTopics) {
    $subscribedTopics[substr($bucket->getSource()->getRequest()->getUrl(), 7)] = $bucket;
});

$websocket_web->on('message', function (Bucket $bucket) {
    $bucket->getSource()->send('Socket connected');
});

$websocket_web->on('close', function (Bucket $bucket) { });

// =================================

$group     = new Hoa\Socket\Connection\Group();
$group[]   = $websocket_web;
$group[]   = $websocket_php;

$group->run();

Web Client:

<script>
    var ws = new WebSocket('ws://site.ll:8008/?user=tester01');
    ws.onmessage = function(evt) { console.log(evt); };
    ws.onopen = function() {
        console.log("connect");
        ws.send("hello");
    };
    ws.onerror = function(error) {
        console.log("Error " + error.message);
    };
</script>

PHP Client:

$client = new Hoa\Websocket\Client(new Hoa\Socket\Client('tcp://127.0.0.1:8009'));
$client->setHost('127.0.0.1');
$client->connect();
$client->send(json_encode(['user' => 'tester01', 'command' => '111111']));
$client->close();

Web client connected to worker and save his backet, then I run php client and worker receive message, find backet needed web client and send message, but it not send and I don't have any errors.

Any idea what it can be?

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

Any idea, why it is not working?

from websocket.

Shkarbatov avatar Shkarbatov commented on May 18, 2024

Well, problem next:

I saved bucket next:

$subscribedTopics[substr($bucket->getSource()->getRequest()->getUrl(), 7)] = $bucket;

And then, when I want to send:

$subscribedTopics[$data['user']]->getSource()->send($data['command']);

if I will not put Socket\Node as the second parametr of the send function it will used current node:

if (null === $node) {
    $node = $this->getConnection()->getCurrentNode();
}

This is ok, but if I will used Node from saved backed:

$subscribedTopics[$data['user']]->getSource()->send($data['command'], 
$subscribedTopics[$data['user']]->getSource()->getConnection()->getCurrentNode());

It will return current node, not saved node.

So I need to save separate bucket and node:

$subscribedTopics[substr($bucket->getSource()->getRequest()->getUrl(), 7)] =
    ['bucket' => $bucket, 'node' => $bucket->getSource()->getConnection()->getCurrentNode()];

And when send message add saved node:

$subscribedTopics[$data['user']]['bucket']->getSource()->send(
    $data['command'],
    $subscribedTopics[$data['user']]['node']
);

Is this bug? Can I somehow receive node from saved bucked?
I can receive node id, but there no method receive node by id, only all with method getNodes()

from websocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.