in reply to XMPP client with Win32::GUI
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XMPP client with Win32::GUI
by BrowserUk (Patriarch) on Jun 17, 2015 at 21:11 UTC |