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;

In reply to Building a Chat client using POE by deadpickle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.