Warning: there is a lot of guess work in this untested code. I don't have Net::XMPP (or even know what XMPP is), and don't have an XMPP server to connect to, even if I installed it.

Here's my modified untested version of your program:

use threads; ## provides async. use Thread::Queue; ## provides inter-thread communicatio +n. use Win32::GUI(); use Net::XMPP; use feature 'say'; my $Qrecv = new Thread::Queue; ## For passing received mesgs from the +client (main) thread to the gui thread. my $Qsend = new Thread::Queue; ## For passing msgs to send from the gu +i thread to the client thread sub timer_Timer() { while( $Qrecv->pending ) { + ## if/while there are inbound messages my $text = $Qrecv->dequeue; + ## dequeue them $wnd->messages->Text( $wnd->messages->Text() . $text . "\r\n" +); ## add them to the gui. } } sub send_message() { my $msg = $wnd->new_message->Text(); + ## get the user input $Qsend->enqueue( $msg ); + ## queue it to the client $wnd->messages->Text($wnd->messages->Text() . "[me] $msg\r\n"); + ## add it to the window $wnd->new_message->Text(''); + ## clear the input field } ## All the gui code lives in here and runs asynchronously to the main +thread. my $thread = async { my $wnd = Win32::GUI::Window->new( -size => [320, 240] ); $wnd->AddTextfield( -name => 'messages', -size => [$wnd->ScaleWidt +h, 130], -pos => [0, 0], -multiline => 1 ); $wnd->AddTextfield( -name => 'new_message', -size => [$wnd->ScaleW +idth, 20], -pos => [0, 140], -multiline => 1 ); $wnd->AddButton( -text => 'Send', -size => [80, 24], -pos => [$wnd +->ScaleWidth / 2 - 40, 170], -onClick => \&send_message ); $wnd->Center; $wnd->Show; my $timer = $wnd->AddTimer('timer', 100); ## Check for message eve +ry 1/10th of a second Win32::GUI::Dialog(); ## process the dialog until it closes. }; my $client = new Net::XMPP::Client; $client->SetCallBacks( message => sub { my ($mid, $message) = @_; say 'message recieved'; $Qrecv->enqueue( + ## queue the formated input msg to the gui th +read '[' . $message->GetFrom('jid')->GetUserID . '] ' . $mes +sage->GetBody ); } ); my $result = $client->Connect( hostname => 'xmpphostname', port => '52 +22', timeout => 7, ssl => 0, tsl => 0 ); if ($result) { $client->AuthSend( username => 'user1', password => '123' ); } if ($client->Connected) { say 'connected to server'; } else { say 'error connecting to server: ' . $!; exit 0; } my $pres = Net::XMPP::Presence->new(); $pres->SetType('available'); $client->Send($pres); while( my $msg = $Qsend->dequeue ) { $client->MessageSend( to => 'user2@xmpphostname', type => 'chat', +body => $msg ); } =comment Don't know what to do with this bit??? Is it necessary? if ($client->Connected) { $client->Connect() unless defined $client->Process(1); } else { $client->Connect() or die('cant connect'); } =cut

Once again; that is untested. You will almost certainly have to correct some of my errors. Yell if you need help.

Please don't be upset by my brutal reformatting of your very nicely formatted code; I just needed to see everything on the screen at once in order to make a guess at what changes were required.

Some explanation: essentially, I've just decoupled the two parts of your application using a pair of queues to pass messages between them.

At the moment, the only way to terminate the program will be ^C or ^break. That can be addressed, but I didn't want to complicate it with more guesswork.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

In reply to Re: XMPP client with Win32::GUI by BrowserUk
in thread XMPP client with Win32::GUI by artem78

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.