artem78 has asked for the wisdom of the Perl Monks concerning the following question:
I`m writing simple xmpp client using Net::XMPP and Win32::GUI. I want to know how to make recieving loop assynchronous.
First I try to moved loop to another thread. But it fails with many errors:
Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in numeric eq (==) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value within %status in numeric eq (==) at C:/str +awberry/perl/site/lib/XML/Stream.pm line 1506. Use of uninitialized value in subtraction (-) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1507. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1669. Use of uninitialized value in numeric eq (==) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1673. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. ....
So I use Win32::GUI::Timer, which call $client->Process(1) every 3 seconds. But function need some time to execute, so window freezes for a moment. How can I avoid these and call Process() asynchronously?
use Win32::GUI(); use Net::XMPP; use feature 'say'; my $wnd = Win32::GUI::Window->new( -size => [320, 240] ); $wnd->AddTextfield( -name => 'messages', -size => [$wnd->ScaleWidth, 130], -pos => [0, 0], -multiline => 1 ); $wnd->AddTextfield( -name => 'new_message', -size => [$wnd->ScaleWidth, 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 $client = new Net::XMPP::Client; $client->SetCallBacks( message => sub { my ($mid, $message) = @_; say 'message recieved'; $wnd->messages->Text($wnd->messages->Text() . '[' . $message-> +GetFrom('jid')->GetUserID . '] ' . $message->GetBody . "\r\n") } ); my $result = $client->Connect( hostname => 'xmpphostname', port => '5222', 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); my $timer = $wnd->AddTimer('timer', 3000); Win32::GUI::Dialog(); sub timer_Timer() { if ($client->Connected) { $client->Connect() unless defined $client->Process(1); } else { $client->Connect() or die('cant connect'); } } sub send_message() { my $msg = $wnd->new_message->Text(); $client->MessageSend( to => 'user2@xmpphostname', type => 'chat', body => $msg ); $wnd->messages->Text($wnd->messages->Text() . "[me] $msg\r\n"); $wnd->new_message->Text(''); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XMPP client with Win32::GUI
by BrowserUk (Patriarch) on Jun 17, 2015 at 20:55 UTC | |
by BrowserUk (Patriarch) on Jun 17, 2015 at 21:11 UTC |