hoaproject / Websocket

The Hoa\Websocket library.
https://hoa-project.net/
423 stars 75 forks source link

Injecting data at the WebSocket/Node object... #53

Closed YnievesDotNet closed 9 years ago

YnievesDotNet commented 9 years ago

Injecting data at the WebSocket/Node object

Hello, I try including this library at my project in Laravel. In this moment I can manage the running of server from Laravel using Artisan command, but I need vinculate one or some nodes for each client, when the client connect at the system.

Other things what I need is configure the listeners on one abstract class, but inside the server connection, while it is running. For example, I need create one class calling HoaWebSocket, and I can configure for example, this:

class HoaWebSocket extend [hoa way point class] {
    ...
    public function onOpen($node, $user) {
        //Assing one node at one user.
    }

    public function send($message, $user) {
        //Send one message at one node.
    }
    ...
}

This is a fictitious idea, is only for comment the flow with I need.

In this moment, I can send the system message at our user using this method

use Hoa\Websocket\Client as WsClient;
use Hoa\Socket\Client as SClient;
...
    public function getLogin()
    {
        $client = new WsClient(
            new SClient('tcp://127.0.0.1:8080')
        );
        $client->connect();
        $client->send('Login rendered');
        $client->close();

        return view('auth.login');
    }
...

And the idea final is it:

use HoaWebSocket;
...
    public function getLogin()
    {
        HoaWebSocket::send('Login rendered', User::findById(1));
        return view('auth.login');
    }
...

But, doing this action without Hoa\Websocket\Client and/or Hoa\Socket\Client.

Thanks

YnievesDotNet

Hywan commented 9 years ago

Hello :-),

To be frank, I didn't understand your questions.

I understand there is 2 questions.

In this moment I can manage the running of server from Laravel using Artisan command…

Good \o/!

… but I need vinculate one or some nodes for each client, when the client connect at the system.

What “vinculate” means?

what I need is configure the listeners on one abstract class, but inside the server connection, while it is running

This is a PHP related question, and no, it's not possible to change the parent class of an instance. So maybe it is better to expose/explain your workflow instead.

doing this action without Hoa\Websocket\Client and/or Hoa\Socket\Client

You want to write a helper? Just write a function or a trait to connect a client, send a data and close the client.

YnievesDotNet commented 9 years ago

Hello, thanks.

Vinculate means assign one system user a one or more node in the servers.

For example, if I need send one message at one specific client connected at the system, ex, admin user logged at the laravel system, know wath is yours nodes in the server.

Thanks.

YnievesDotNet commented 9 years ago

User::id (1) = Node::id (asdasdqwe)

YnievesDotNet commented 9 years ago

I have try other things, and in this moment, I have one database table with this relations, and I can get all nodes ID for one client.

Now, I want send one message at one node, from my client in php, using websocket/client php library, for example, doing this:

        $fstck = FSTocken::where('user_id', 1)->first();

        $client = new WsClient(
            new SClient('tcp://127.0.0.1:8080')
        );
        $client->connect();
        $client->send('Login rendered', *************);
        $client->close();

How I can define this line for correct function. The node ID is in $fstck->websocket_id .

Thanks.

YnievesDotNet commented 9 years ago

It's that possible?

YnievesDotNet commented 9 years ago

How I can connect at one node specific?

Thanks. YnievesDotNet

thehawk970 commented 9 years ago

Hello,

I try to understand on IRC but i really need to go to sleep :D (Paris timezone power) You want to send an data to an specific node $data = 'hello world';

So you need to have an identifier in your custom node i assume its the method getMyIdentifier. You need to do an loop on all your node for find the specific node you will get something like that :

$server->on('message', function (Hoa\Core\Event\Bucket $bucket) {
        $nodes = $bucket->getSource()->getConnection()->getNodes();
$data = 'hello world';

foreach($nodes as $node) {
        if($node->getID() === 'myID') {
                $this->send($data, $node);
        }
}

    return;
});

You need to test this i write that just for exemple its not probably the solution but maybe a clue

Hywan commented 9 years ago

We also have the broadcastIf method (https://github.com/hoaproject/Socket/blob/8c57600d21582b7b4256590de6830839ac4555f1/Connection/Handler.php#L313-L341). But we opened an issue to document it (see #38), as @camael24 noticed.