#!c:\perl\bin\perl.exe -w use strict; use Win32::GUI; use threads; use threads::shared; use PerlChat::Chat; use PerlChat::Config; use PerlChat::Utils; #初始化聊天類 my $config = getConfig("conf/config.conf"); my $chat = PerlChat::Chat->spawn($config->{ip},$config->{port}); my $mainWin = new Win32::GUI::Window( -name => 'mainWin', -text => '51GG', -size => [ $s_Main->{w}, $s_Main->{h} ], -onResize => \&mainWinResize, -onMinimize => sub { 1 }, -onMaximize => sub { 1 }, -onTerminate => \&mainWinClose, ); $mainWin->AddTextfield( -name => 'Username', -pos => [ $s_Login->{x} + 55, $s_Login->{y} + 20 ], -size => [ 120, 20 ], -prompt => [ "用戶名:", -45 ], ); $mainWin->AddTextfield( -name => 'Password', -pos => [ $s_Login->{x} + 55, $s_Login->{y} + 40 ], -size => [ 120, 20 ], -password => 1, -prompt => [ "密 碼:", -45 ], ); $mainWin->AddButton( -name => 'LoginServer', -pos => [ $s_Login->{x} + 50, $s_Login->{y} + 70 ], -title => '登錄', -onClick => \&LoginServer, ); $mainWin->AddButton( -name => 'LogoutServer', -pos => [ $s_Login->{x} + 100, $s_Login->{y} + 70 ], -title => '注銷', -disabled => 1, -onClick => \&LogoutServer, ); #主循環(huán) $mainWin->Show(); Win32::GUI::Dialog(); sub LoginServer{ $chat = PerlChat::Chat->spawn($config->{ip},$config->{port}) unless ($chat->{dp}); my ($username,$password) = ($mainWin->{Username}->Text(),$mainWin->{Password}->Text()); my $loginStr = makeLoginStr($username,$password); $mainWin->MessageBox('登錄信息發(fā)送失敗,請重新啟動本程序,點(diǎn)擊"確定"關(guān)閉本程序','錯誤',MB_OK | MB_ICONERROR) and return -1 unless ($chat->send_data($loginStr) == 0); my $ret = $chat->recv_data(1); $mainWin->MessageBox('系統(tǒng)繁忙,登錄失敗,請重新啟動本程序,點(diǎn)擊"確定"關(guān)閉本程序','錯誤',MB_OK | MB_ICONERROR) and return -1 unless ($ret == 1); my $threadRecvMsg = threads->create(sub {$chat->recv_data($mainWin);}); $threadRecvMsg->detach(); } sub LogoutServer { my $logoutStr = "~2~" . $chat->{'uin'} . "~100~200~1~0~0"; $logoutStr = ( length($logoutStr) - 1 ) . $logoutStr; $chat->{'dp'} ->send_data( $chat->{'sock'},$logoutStr); $chat->{'dp'}->close_socket( $chat->{'sock'} ); } __END__ |
sub LoginServer{ $chat = PerlChat::Chat->spawn($config->{ip},$config->{port}) unless ($chat->{dp}); my ($username,$password) = ($mainWin->{Username}->Text(),$mainWin->{Password}->Text()); my $loginStr = makeLoginStr($username,$password); $mainWin->MessageBox('登錄信息發(fā)送失敗,請重新啟動本程序,點(diǎn)擊"確定"關(guān)閉本程序','錯誤',MB_OK | MB_ICONERROR) and return -1 unless ($chat->send_data($loginStr) == 0); my $ret = $chat->recv_data(1); $mainWin->MessageBox('系統(tǒng)繁忙,登錄失敗,請重新啟動本程序,點(diǎn)擊"確定"關(guān)閉本程序','錯誤',MB_OK | MB_ICONERROR) and return -1 unless ($ret == 1); my $threadRecvMsg = threads->create(sub {$chat->recv_data($mainWin);}); $threadRecvMsg->detach(); } |
歡迎光臨 Chinaunix (http://www.72891.cn/) | Powered by Discuz! X3.2 |