Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making $loop protected in Socket\Server and custom Connection classes? #12

Closed
attozk opened this issue Oct 27, 2014 · 1 comment
Closed
Labels

Comments

@attozk
Copy link

attozk commented Oct 27, 2014

The issue/scenario is that I would like to create a custom Connection and I am wondering what is the best way of doing it.

There are two possible ways that I can think of:

  • Allow extending Server by changing scope of $loop to protected.
  • Add a factory (like) connection class property in Server

Details for both approaches below, but first is there any reason for keeping $loop private in React\Socket\Server?

Approach 1 - changing scope of $loop to protected:
Changing scope to protected, then extending it via MyCustomServer in which I can override createConnection.

This approach is demonstrated below.

/** @event connection */
class Server extends EventEmitter implements ServerInterface
{
    public $master;
    protected $loop;    // changing scope from private to protected
    ....
}

/** MY Custom Server Class */
class MyCustomServer extends Server 
{
    public function createConnection($socket)
    {
        return new CustomConnection($socket, $this->loop);
    }
}

Approach 2 - Connection class property
Have a factory (sort of) way of creating a connection, in which a Server's property can be overwritten to create a connection e.g.

/** @event connection */
class Server extends EventEmitter implements ServerInterface
{
    public $master;
    private $loop;
    public $connectionClass = 'Connection';
    ....

    public function createConnection($socket)
    {
        return new $connectionClass($socket, $this->loop);
    }
}
@attozk
Copy link
Author

attozk commented Oct 27, 2014

Answered via IRC:

[14:17] <cboden> attozk: You could extend Server now, intercepting the __construct function and storing $loop in your own variable space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants