Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

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

Closed
YnievesDotNet opened this issue Sep 24, 2015 · 8 comments
Closed

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

YnievesDotNet opened this issue Sep 24, 2015 · 8 comments

Comments

@YnievesDotNet
Copy link

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
Copy link
Member

Hywan commented Sep 28, 2015

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
Copy link
Author

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
Copy link
Author

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

@YnievesDotNet
Copy link
Author

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
Copy link
Author

It's that possible?

@YnievesDotNet
Copy link
Author

How I can connect at one node specific?

Thanks. YnievesDotNet

@thehawk970
Copy link

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
Copy link
Member

Hywan commented Sep 29, 2015

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.

@Hywan Hywan closed this as completed Sep 29, 2015
@Hywan Hywan removed the in progress label Sep 29, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants