Perl Net::WebSocket::Server

1.1k views Asked by At

I am using the Perl Module Net::WebSocket::Server to build a message deliver system for users that are visiting a domain. I amd using the example scripts from the CPAN module page, and this is the code I have running:

#!/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;

I would like to extract some type of information that will supply me with the users online, for example IP or connection ID and put them all into an array. You can see my commented attempts grab the conection data and print it using datadumper, but this has only resulted in errors or hashes. Can anyone figure out how to extract IPs or connection IDs and put them into an array so I know howmany unique connections there are?

Solution:

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;
0

There are 0 answers