Perl, websocket in infinite loop

960 views Asked by At

I have a perl script that worked as a “middle men” between a local program and an external interactive website.

The problem is that the external website migrated from plain tcp connection to a websocket connection.

When the server was using tcp, after initial connection, the client (the script) and the server (external website) will go thru a handshake, then the script will send the username and password and server will finally respond with some encryption keys, afterwards the script will go into an infinite loop and waited for data from both connections and then process that data and "print" back to the connections as needed.

I had been able to establish the websocket connection with the server using the Mojo::UserAgent as well as with protocol::websocket, go thru the handshake and the other information exchange (username, password, etc), but I have not been able (or better said: I do not know how) to "throw" the websocket connection into the infinite loop via IO::Select ( The reason I want to use IO::Select is because doing so will require minimal changes to the script, but other suggestions are definitely welcome).

The relevant parts of the script are as follows:

# Creating connection for local program
$lsn=new IO::Socket::INET(
    Proto => 'tcp',
    LocalPort => 6000,
    Listen => 1,
);
unless(defined $lsn){
    print"$0: $!\n";
    exit 1;
}
print"Waiting for local program connection on port 6000\n";
$server=$lsn->accept;
$lsn->close;
unless(defined $server){
    print "$0: Unable to accept incoming connection: $!\n";
    exit 1;
    }
# At this point, the script is waiting for a connection from
# the local program on port 6000
printf"Connection accepted from %s\n",$server->peerhost;
select $server;
binmode $server;
$stdin=$server;
(select)->autoflush(1);
# Creating connection for external website
$net=new IO::Socket::INET(
    Proto => 'tcp',
    PeerAddr => $yog,
    PeerPort => $yserverport,
);

unless(defined($net)){
    print "Can't connect!\n";
    exit 1;
}
$net->autoflush(1);
####################################
# Here the script and server will  #
# exchange information few times   #
####################################
my $sel=new IO::Select($stdin,$net);
$net->autoflush(0);
while (1){
    foreach my $i($sel->can_read(0.05)){
        if($i==$net){
            &dosomething;
            $net->flush;
        }
        else{
            &dosomething2;
            $net->flush;
        }
    }   
}

The infinite loop examples that I have found, are not suitable in this case because I need to use an infinite loop that can check for incoming data on both connections.

1

There are 1 answers

1
Bryan On

WebSockets require a lot than asimple IO Socket. They require handshakes and data framing. I would review the W3C WebSocket API and then look into using a perl module (Net::WebSocket::Server) to do the heavy lifting. Also, webSockets will only work with the chrome browser using SSL so if are interested in cross compatibility, use Net::WebSocket::Server with IO::Socket::SSL instead and here is a working sample of SSL:

#!/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 => '/var/ssl/cert.crt',
      SSL_key_file  => '/var/ssl/cert.key',
    ) or die "failed to listen: $!";

my $port = 6000;
my $origin = 'https://YOURDOMAIN.com';

Net::WebSocket::Server->new(
    listen => $ssl_server,

    on_connect => sub {
            our ($serv, $conn) = @_;
            $conn->on(

            handshake => sub {
                        my ($conn, $handshake) = @_;
                        $conn->disconnect() unless $handshake->req->origin eq $origin;
                    },
                    utf8 => sub {

            my ($conn, $msg) = @_;
            my $MyIP = $conn->ip();
            my $MyPORT = $conn->port();
             $_->send_utf8($msg) for( $serv->connections() );
                },
    );
    },
)->start;

If you are not concerned with Chrome or SSL here is a working non-SSL example, (It needs use strict and use warnings):

#!/usr/bin/perl

use Net::WebSocket::Server;

my $port = 6000;

Net::WebSocket::Server->new(
    listen => $port,
    on_connect => sub {
        my ($serv, $conn) = @_;
        $conn->on(
            utf8 => sub {
        my ($conn, $msg) = @_;
                $_->send_utf8($msg) for( $serv->connections() );
            },
        );
    },
)->start;

Also, if you decide to use the SSL version, make sure to update your client side from ws:// to wss://