deadpickle has asked for the wisdom of the Perl Monks concerning the following question:
#!/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 | |
by deadpickle (Pilgrim) on Jan 07, 2008 at 02:09 UTC | |
by Snarius (Sexton) on Jan 07, 2008 at 03:19 UTC |