deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

Currently I have been trying to build a chat client using the Perl Object Environment and Gtk2. I have been talking on the POE mailing list and have made progress but currently have ran into a few snags lately. One problem that I have encountered is that when you close the client, the GUI closes but the program is still running. I suspect that the Component::Client session is still running. Another problem is that when the client connects to the server, nothing is displayed in the textview but when it encounters the client error event, it is displayed in the text box. I would appreciate any help.
#!/usr/bin/perl use warnings; use strict; use Gtk2 -init; use Glib qw/TRUE FALSE/; use POE::Kernel { loop => "Glib" }; use POE::Session; use POE::Component::Client::TCP; my $buffer = Gtk2::TextBuffer->new; ######################################################## # Create the session that will drive the user interface POE::Session->create( inline_states => { _start => \&ui_start} ); #start the UI interface (Main windowed) sub ui_start { my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ]; $heap->{main_window} = Gtk2::Window->new("toplevel"); $kernel->signal_ui_destroy( $heap->{main_window} ); my $table = Gtk2::Table->new(3, 2, FALSE); my $label = Gtk2::Label->new("Chat Client Test"); my $button = Gtk2::Button->new("Send"); my $textview = Gtk2::TextView->new_with_buffer($buffer); $textview->set_cursor_visible (FALSE); my $swindow = Gtk2::ScrolledWindow->new( undef, undef); $swindow->set_policy( 'automatic', 'automatic'); $swindow->set_shadow_type( 'etched-out'); $swindow->add( $textview); my $entry = Gtk2::Entry->new(); $table->attach_defaults($label, 0, 1, 0, 1); $table->attach_defaults($swindow, 0, 2, 1, 2); $table->attach_defaults($entry, 0, 1, 2, 3); $table->attach_defaults($button, 1, 2, 2, 3); $heap->{main_window}->add($table); $heap->{main_window}->show_all(); spawn_client(); Gtk2->main; } #################################################### #################################################### #start the client session sub spawn_client { my $host = "Deadpickle-hobo"; my $port = 11211; POE::Component::Client::TCP->new( RemoteAddress => $host, RemotePort => $port, Connected => sub { $buffer->set_text("connected to $host:$port ...\n"); }, ConnectError => sub { $buffer->set_text("could not connect to $host:$port ...\n" +); $_[KERNEL]->delay( reconnect => 60 ); }, ServerInput => sub { my ( $kernel, $heap, $input ) = @_[ KERNEL, HEAP, ARG0 ]; $buffer->set_text($input); }, Disconnected => sub { $buffer->set_text("Disconnected\n"); $_[KERNEL]->delay( reconnect => 60 ); } ); } ################################################### $poe_kernel->run(); exit 0;

Replies are listed 'Best First'.
Re: Building a Chat client using POE
by Snarius (Sexton) on Jan 06, 2008 at 22:31 UTC
    I believe that you should take away this line: Gtk2->main; It is called while the POE loop is running.

    I think that might lock you into the main Gtk2 loop, where $poe_kernel->run(); handles both Gtk2 and POE events.
      I removed the Gtk2->main; section of code from the loop. The program still runs but will not post the "Connected" text. Can POE run threads?
        I remember reading about this: No threads. Supposedly POE supports concurrency, but only in the form of processes.