Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket and Perl CGI

I am bit new in CGI programming, and I trying to make an online chat API but face not few troubles:

I was looking online for solution and found Websocket for client (js) and HTTP::Daemon for perl, but I have no idea where to start to make the server listen for the connections from the browser.

Here is my JavaScript code:

ws = new WebSocket('ws://www.crazygao.com:3000'); // test
ws.onopen = function() {
    alert('Connection is established!'); // test
};
ws.onclose = function() {
    alert('Connection is closed'); 
};
ws.onmessage = function(e) {
    var message = e.data;
    //alert('Got new message: ' + message);
};
ws.onerror = function(e) {
    //var message = e.data;
    alert('Error: ' + e);
};

Here is my Perl script test code:

use HTTP::Daemon;
use HTTP::Status;

my $d = HTTP::Daemon->new(
    LocalAddr => 'www.crazygao.com',
    LocalPort => 3000
) || die; print "Please contact me at: <URL:", $d->url, ">\n";
while(my $c = $d->accept) {
    $c->send_response("1"); # test
    while (my $r = $c->get_request) {
        if ($r->method eq 'GET') {
            $c->send_response("...");
        }
    }
    $c->close;
    undef($c);
}

When the page loads, the connection closing immediately, and in Chrome console window I see the following error: WebSocket connection to 'ws://198.38.89.14:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I run the perl script manually (using simple call to http://example.com/cgi-bin/xxx.cgi) and then when I refresh the page I get: WebSocket connection to 'ws://198.38.89.14:3000/' failed: Error during WebSocket handshake: Unexpected response code: 200

I understand that the server normally returns 200 when OK, but Websocket is waiting for 101 code as "OK".

My question is, if so, how can I achieve this?

like image 230
Xerix Avatar asked Sep 11 '25 04:09

Xerix


1 Answers

I know this is old and I got here because I was looking for an answer myself. I ended up finding the answer myself by using Net::WebSocket::Server.

http://search.cpan.org/~topaz/Net-WebSocket-Server-0.003004/lib/Net/WebSocket/Server.pm for more details on how to use the module and example.

Basically, you'll have this perl code to match your javascript (copied and modified from the CPAN page of Net::WebSocket::Server):

use Net::WebSocket::Server;

my $origin = 'http://www.crazygao.com';

Net::WebSocket::Server->new(
    listen => 3000,
    on_connect => sub {
        my ($serv, $conn) = @_;
        $conn->on(
            handshake => sub {
                my ($conn, $handshake) = @_;
                $conn->disconnect() unless $handshake->req->origin eq $origin;
            },
            utf8 => sub {
                my ($conn, $msg) = @_;
                $_->send_utf8($msg) for $conn->server->connections;
            },
            binary => sub {
                my ($conn, $msg) = @_;
                $_->send_binary($msg) for $conn->server->connections;
            },
        );
    },
)->start;
like image 193
Sophia Avatar answered Sep 12 '25 17:09

Sophia