hprose / hprose-html5

Hprose is a cross-language RPC. This project is Hprose 2.0 Client for HTML5
MIT License
238 stars 90 forks source link

client.subscribe() keeps creating new connections #7

Open Fishdrowned opened 7 years ago

Fishdrowned commented 7 years ago

I see this in Chome devtool >> Network: image

Client script:

<script src="../hprose/hprose-html5-2.0.34.js"></script>
<script type="text/javascript">
    !function () {
        var url = 'ws://127.0.0.1:2080/hello',
            subscriber = new hprose.WebSocketClient(url);
        subscriber.subscribe('push', function () {
            console.log(arguments);
        });
    }();
</script>

Server script:

use Hprose\Swoole\Server as Server;

$listen = 'ws://0.0.0.0:2080/hello';
$server = new Server($listen);
$server->publish('push');
$server->start();
Fishdrowned commented 7 years ago

It keeps creating new connections about every 30 seconds.

I can see the call stack by hanging mouse over the Initiator.

The first connection, looks OK, it was initiated by subscribe(): image

The second connection, no good: image

The third connection, wow, look at the scroll bar, it gets longer: image

andot commented 7 years ago

because the client default timeout is 30s, and the server default timeout is 120s. you can set the client timeout more than 120s to fixed problem.

Fishdrowned commented 7 years ago

Thank you for your reply, I've extended the client timeout to 1 day, it stops creating new connections.

But look at the 1st screenshot, it indicates that the old connections are not closed (status = 101 and time = Pending means the connection is still alive), but new connections are created.

PS. The term heartbeat is misleading, I expected it to send some ping frames to the client to keep connection open.

andot commented 7 years ago

When timeout on client, the connection will not to be closed, because maybe some other requests on this connection too.

Fishdrowned commented 7 years ago

The problem is, by default settings, client.subscribe() will keep creating connections as time grows, keep them open, not reusable, never closed, just like memory leak.

I guess this should be a architecture issue, but I don't have enough time to investigate and prove this.

Sets the client timeout to 1 day or some big number is just a workaround.

I implemented a manual but real heartbeat in my application, by pushing an empty string to the subscribers every minute.

andot commented 7 years ago

Yes, you are right. I will try to fix this problem in next version.