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

How to reopen a closed connection? #39

Closed
DavidM974 opened this issue Dec 15, 2016 · 4 comments
Closed

How to reopen a closed connection? #39

DavidM974 opened this issue Dec 15, 2016 · 4 comments
Labels

Comments

@DavidM974
Copy link

DavidM974 commented Dec 15, 2016

Hello,
I'm using your lib to do some test on an api and i would like to know how to reopen a connection closed using your exemple on the event close

<?php

    require __DIR__ . '/vendor/autoload.php';

    $loop = React\EventLoop\Factory::create();
    $connector = new Ratchet\Client\Connector($loop);

    $connector('ws://127.0.0.1:9000', ['protocol1', 'subprotocol2'], ['Origin' => 'http://localhost'])
    ->then(function(Ratchet\Client\WebSocket $conn) {
        $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
            echo "Received: {$msg}\n";
            $conn->close();
        });

        $conn->on('close', function($code = null, $reason = null) {
            echo "Connection closed ({$code} - {$reason})\n";
        });

        $conn->send('Hello World!');
    }, function(\Exception $e) use ($loop) {
        echo "Could not connect: {$e->getMessage()}\n";
        $loop->stop();
    });

    $loop->run();

How to reopen a connection after this line echo "Connection closed ({$code} - {$reason})\n";

Regards

@cboden
Copy link
Member

cboden commented Jan 1, 2017

I would recommend creating a new connection when the connection is closed. You'll have to abstract some of your code into re-usable functions and maintain a connection variable. I would also recommend using RxPHP as it makes this kind of behaviour trivial.

@cboden cboden added the question label Jan 2, 2017
@DavidM974
Copy link
Author

Is it possible to have a small example? i can't find the good way. i tried to put in a function but it's not working.

@suhuaguo
Copy link

@DavidM974 。hello 。Haveyou solved 【How to reopen a closed connection】 this problem or give me some idea 。

@mbonneau
Copy link
Member

@suhuaguo @DavidM974 The connection is only meant to be used once, you have to reestablish the connection.

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

4 participants