我正在使用 Perl 模块 Net::WebSocket::Server 为访问域的用户构建消息传递系统。我正在使用 CPAN 模块页面中的示例脚本,这是我正在运行的代码:
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::SSL;
use Net::WebSocket::Server;
my $ssl_server = IO::Socket::SSL->new(
Listen => 5,
LocalPort => 4000,
Proto => 'tcp',
SSL_cert_file => '/ssl/domain.com.crt',
SSL_key_file => '/ssl/domain.com.key',
) or die "failed to listen: $!";
my $port = 4000;
#print "Starting WebSocket chat server on port $port, press Ctr-C to disconnect\n$
Net::WebSocket::Server->new(
listen => $ssl_server,
on_connect => sub {
my ($serv, $conn) = @_;
$conn->on(
utf8 => sub {
my ($conn, $msg) = @_;
# print "got: $msg\n";
print $_->ip() for( $serv->connections() );
print $_->port() for( $serv->connections() );
$_->send_utf8($msg) for( $serv->connections() );
},
);
},
)->start;
我想提取某种类型的信息,这些信息将为我提供在线用户,例如 IP 或连接 ID,并将它们全部放入一个数组中。您可以看到我评论过的尝试抓取连接数据并使用 datadumper 打印它,但这只会导致错误或散列。谁能弄清楚如何提取 IP 或连接 ID 并将它们放入一个数组中,以便我知道有多少个唯一连接?
解决方案:
Net::WebSocket::Server->new(
listen => $ssl_server,
on_connect => sub {
my ($serv, $conn) = @_;
$conn->on(
utf8 => sub {
my ($conn, $msg) = @_;
$IP = $conn->ip();
$PORT = $conn->port();
# print "got: $msg\n";
$_->send_utf8('IP: ' . $IP . ' PORT: ' . $PORT . ' '. $msg) for( $serv->connections() );
},
);
},
)->start;