Asynchronous Chat Server using Mojolicious

Viewed 471

Hello Ladies and Gentleman! I am currently writing a minimalistic chat server that will somewhat resemble IRC. I am writing it in perl using the Mojolicious, but unfortunately have run into an issue. I have the following code:

#!/usr/bin/perl

use warnings;
use strict;

use Mojo::IOLoop::Server;

my $server = Mojo::IOLoop::Server->new;

$server->on(accept => sub {
    my ($server, $handle) = @_;
    my $data;

    print $handle "Connected!\n";

    while(1) {
        $handle->recv($data, 4096);

        if($data) {
            print $server "$data";
        }
    }
});

$server->listen(port => $ARGV[0]);
$server->start;
$server->reactor->start unless $server->reactor->is_running;

Unfortunately, the print $server "$data"; line does not actually work. It gives off the error:

Mojo::Reactor::Poll: I/O watcher failed: Not a GLOB reference at ./server.pl line 20.

I have looked through the documentation for Mojolicious, but cannot find how to send the line I get from client A to the rest of the clients connected.

1 Answers
Related