下面的客户端程序从WebSocket服务器接收消息。
它不会发送任何消息。
客户端
use v6;
use Cro::WebSocket::Client;
constant WS-URL = 'ws://localhost:20000/status';
constant TIMEOUT-TO-CONNECT = 5; # seconds
my $timeout;
my $connection-attempt;
await Promise.anyof(
$connection-attempt = Cro::WebSocket::Client.connect(WS-URL),
$timeout = Promise.in(TIMEOUT-TO-CONNECT));
if $timeout.status == Kept
{
say "* could not connect to server in ', TIMEOUT-TO-CONNECT, ' seconds";
exit 1;
}
if $connection-attempt.status != Kept
{
my $cause = $connection-attempt.cause;
say '"* error when trying to connect to server';
say '"* --------------------------------------';
# $cause is a long string, how do we get a simple numeric code ?
say $cause;
say '"* ======================================';
exit 1;
}
my $connection = $connection-attempt.result;
my $peer = 'localhost:20000';
say '* connected with ', $peer;
react
{
whenever $connection.messages -> $message
{
my $body = await $message.body;
say '* received message=[' ~ $body ~ '] from server';
LAST { say '* LAST'; done; }
QUIT { default { say '* QUIT'; done; }}
}
CLOSE { say '* CLOSE: leaving react block';}
} # react服务器
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use Cro::HTTP::Router::WebSocket;
my $application =
route
{
get -> 'status'
{
web-socket -> $incoming
{
my $counter = 0;
my $timer = Supply.interval(1);
supply
{
whenever $incoming -> $thing
{
LAST { note '* LAST: client connection was closed'; done; }
QUIT { default { note '* QUIT: error in client connection'; done; } }
}
whenever $timer
{
$counter++;
say '* sending message ', $counter;
emit $counter.Str;
}
CLOSE { say '* CLOSE: leaving supply block'; }
} # supply
} #incoming
} # get -> status
}
my $server = Cro::HTTP::Server.new: :port(20000), :$application;
$server.start;
say '* serving on port 20000';
react whenever signal(SIGINT)
{
$server.stop;
exit;
}现在,当服务器停止工作(比如通过Ctrl+C)时,客户端什么也看不到。
在客户端中设置CRO_TRACE=1可以实现以下功能:
TRACE(anon 2)] Cro::WebSocket::MessageParser EMIT WebSocket Message - Text
* received message=[4] from server
[TRACE(anon 1)] Cro::TCP::Connector DONE
[TRACE(anon 2)] Cro::WebSocket::FrameParser DONE
[TRACE(anon 2)] Cro::WebSocket::MessageParser DONE
[TRACE(anon 1)] Cro::HTTP::ResponseParser DONE
^C 客户没有展示更多(然后我取消了它)。
因此,问题是:客户端应该如何处理这种情况?
更新
编辑了问题,现在显示了服务器代码
还有,我在Fedora 28号当我第一次取消服务器时,netstat显示
$ netstat -ant | grep 20000
tcp6 0 0 ::1:20000 ::1:56652 TIME_WAIT
$Tcpdump显示
IP6 ::1.20000 > ::1.56652: Flags [F.], seq 145, ack 194, win 350, options [nop,nop,TS val 1476681452 ecr 1476680552], length 0
IP6 ::1.56652 > ::1.20000: Flags [F.], seq 194, ack 146, win 350, options [nop,nop,TS val 1476681453 ecr 1476681452], length 0
IP6 ::1.20000 > ::1.56652: Flags [.], ack 195, win 350, options [nop,nop,TS val 1476681453 ecr 1476681453], length 0从客户端到服务器的最后一个ACK似乎丢失了,我猜客户端没有关闭连接。
此外,我很好奇为什么Cro默认情况下选择使用IPv6。
发布于 2020-02-23 16:58:23
另外,我很好奇为什么
默认情况下会选择使用IPv6。
如果IPv6地址是hosts文件中localhost的第一个地址,则localhost将首先解析为该地址。在撰写本文时,IO::Socket::Async ( Cro在内部使用)只允许将PF_UNSPEC指定为一个系列,并且从主机名解析结果中使用的唯一地址是收到的地址列表中的第一个地址。这将在将来的某个时候作为我的IP6NS grant和problem solving issue to improve how DNS is handled工作的一部分进行更改,但是现在,如果您只想使用IPv4/IPv6,您应该指定127.0.0.1/::1而不是使用localhost (或者如果它们不同,您的机器将其解析为哪个地址)。
https://stackoverflow.com/questions/51432724
复制相似问题