gpt4 book ai didi

perl - 为什么 websocket 连接中断

转载 作者:行者123 更新时间:2023-12-04 15:32:15 25 4
gpt4 key购买 nike

在我对 websocket 回显服务器运行客户端后,它在大约半分钟后与 WebSocket closed with status 1006 断开连接信息。

请建议如何避免这种行为(浏览器客户端似乎没有受到影响)

use 5.20.0;
use Mojo::UserAgent;
use Mojo::IOLoop;

sub ws_connect {

state $ua;

say "Connecting..";
$ua = Mojo::UserAgent->new;
$ua->websocket('ws://127.0.0.1:3000/echo' => \&onConnect);

}

sub onConnect {
my ($ua, $tx) = @_;

if (!$tx->is_websocket) {
say 'WebSocket handshake failed!';
}
say "Connected!";

$tx->on(finish => sub {
my ($tx, $code) = @_;
say "WebSocket closed with status $code";
});

}

ws_connect();
Mojo::IOLoop->start;

回声服务器
use Mojolicious::Lite;
use Mojo::EventEmitter;

helper events => sub { state $events = Mojo::EventEmitter->new };

# get '/' => 'chat';

websocket '/echo' => sub {
my $c = shift;

$c->inactivity_timeout(3600);

# Forward messages from the browser
$c->on(message => sub { shift->events->emit(mojochat => shift) });

# Forward messages to the browser
my $cb = $c->events->on(mojochat => sub { $c->send(pop) });
$c->on(finish => sub { shift->events->unsubscribe(mojochat => $cb) });
};

app->start;

最佳答案

如果客户端和服务器之间没有数据,则可能您已达到不活动超时。

你有没有试过增加 inactivity_timeout ? (或者您可以简单地将其设置为 0 以实现无限不活动)

关于perl - 为什么 websocket 连接中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com