4

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;
BryanD
  • 41
  • 3
  • 1
    Please `use strict` your code. Apart from that there is no concept of a *connection id* or *user* in websockets so it is unclear what you are trying to do. If you only want to count the number of current connections just look at the size of the array returned by `$serv->connections()`. If you want to get information about each of these connections like the peers IP address just call the appropriate methods [as documented](https://metacpan.org/pod/Net::WebSocket::Server::Connection#ip()). – Steffen Ullrich Jan 14 '17 at 07:54
  • I can't seem to get the information as you pointed out in the link, which is why I posted the question. Using data dumped does not show the port or ip. Can you provide a working example of how this would be accomplished? – BryanD Jan 14 '17 at 16:23
  • I've even linked to the exact function, named `ip`. I can neither see that you've modified your code to `use strict` nor can I see how you've attempted to use the function `ip` so it's not possible to see what you are doing wrong. – Steffen Ullrich Jan 14 '17 at 18:40
  • Yes I see that. Thank you for your attempt to assist with my problem. I have already reviewed the documentation after a Google search turned up the cpan module outage with the exact information. I was hoping I could get more than a Google link answer. My attempts roo extract the information are already included and commented out in the code I provided as I explained in the initial post. The link you provided, and also available at cpan simply state calling ip() yields results. But I do not see a proper syntax for making this call or where to incorporate such call in my example. Thanks. – BryanD Jan 14 '17 at 19:55
  • To show all IP try `print $_->ip() for $serv->connections()`. At least that's what I would read from the documentation. And again, no attempt to use `ip` is shown in your code and the code is still not using `use strict`. – Steffen Ullrich Jan 14 '17 at 20:03
  • You are correct I did not specify the ip specifically. I tried dumping all contents of $Serv->connections() into the hash %users and printing all contents. I will try your method and report back. Thank you for the syntax example. – BryanD Jan 14 '17 at 21:03
  • I have updated the code to reflect the suggested changes. The code still runs but the IP and PORT sections do not display anything in the console. I am still able to connect from the web and operate as it did before. – BryanD Jan 14 '17 at 23:42
  • I have updated the code and tested with: `use warnings;` as recommended. I am still unable to obtain the IP or PORT of the client connections. – BryanD Jan 15 '17 at 03:16
  • 2
    After messing with variations of the `$_->ip()` syntax I was able to get things working! here is the section that needed altering. I updated the original post to include the solution. – BryanD Jan 15 '17 at 04:12
  • 1
    Thanks @SteffenUllrich for solution with syntax – BryanD Jan 15 '17 at 04:20

0 Answers0