A brand new POE::Component::Server::TCP has a single session that listens for new connections. It spawns off a new session for each connection that arrives.

The server session uses the given Alias. In your code, that means "TOF" addresses the server itself. The individual connections do not have aliases. Even if they did, it would be difficult to address a particular connection without some arbitration: Which connection does "Moe" refer to?

The best pattern devised so far is to have each connection register itself with a data structure to keep track of them all. The chat server recipe in the POE Cookbook shows a relatively simple way to do this.

On a stylistic note, it is a bad idea to mix event handlers from multiple sessions in one file. It becomes difficult to tell which session a particular function belongs to.

What follows is an untested version of your original program using concepts lifted from the Chat Server recipe.

#!/usr/bin/perl -w use strict; use YAML; sub POE::Kernel::ASSERT_DEFAULT () { 1 } use POE; use POE::Component::Server::TCP; use POE::Filter::Reference; # This hash will keep track of every connected client. It is used by # the updater session and each connection, so it's scoped where # everything can see it. my %connected_clients; { POE::Component::Server::TCP->new( Alias => 'TOF', Port => 2222, ClientFilter => ['POE::Filter::Reference'], ClientInput => \&client_input, # Register handlers for client connection and disconnection. They # will manage the contents of %connected_clients. ClientConnected => \&add_new_client, ClientDisconnected => \&remove_old_client, # InlineStates are for each client, not for the server as a whole. InlineStates => { update_client => \&update_client, }, ); sub client_input { my $input = $_[ARG0]; print 'my input = ' . YAML::Dump($input); } # Each client session has its own "update" method, scoped to the # single client. sub update_client { my $update = $_[ARG0]; my $client = $_[HEAP]->{client}; if ($client) { print 'my update is ' . YAML::Dump($update); $client->put($update); } } sub add_new_client { $connected_clients{$_[SESSION]->ID} = { # Store information about the client here. # Remote user ID? Address? Port? Whatever. # The code to send updates to clients can examine this data to # decide whether a client needs an update. For now, it's empty, # and all clients receive all updates. }; } sub remove_old_client { delete $connected_clients{$_[SESSION]->ID}; } } { POE::Session->create( inline_states => { _start => \&_start, Send_updates => \&send_update, _stop => \&_stop, }, ); sub _start { my $kernel = $_[KERNEL]; print "session started\n"; $kernel->yield('Send_update'); } sub send_update { my $kernel = $_[KERNEL]; my $update = { Header => 'update', Msg => 'Hello Server', }; # At update time, send an update to every currently connected # client. As previously mentioned, this code can examine the # contents of $connected_clients{$client_id} and skip clients that # don't need or deserve updates. foreach my $client_id (keys %connected_clients) { $kernel->post($client_id, 'update_client', $update); } $kernel->delay('Send_update', 10); } sub _stop { print "session end\n"; } } POE::Kernel->run(); exit;

In reply to Re: POE: post an event on tcp server from a session by rcaputo
in thread POE: post an event on tcp server from a session by tsvikt

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.