Guest User

Untitled

a guest
Apr 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.50 KB | None | 0 0
  1. procedure TForm1.ClientSocketRead(Sender: TObject;
  2.   Socket: TCustomWinSocket);
  3. var NET_BUFF        : string;
  4.     NET_CODE        : string;
  5.     TMP_STR         : string;
  6.     TMP_LAST        : string;
  7.     av_key          : word;
  8.     pos_index       : word;
  9.     i,n             : word;
  10.     tmp_view_style  : TViewStyle;
  11. begin
  12.   NET_BUFF := Socket.ReceiveText();
  13.   NET_CODE := copy(NET_BUFF,1,5);
  14.  
  15.   //ShowMessage(NET_BUFF);
  16.  
  17.   if NET_CODE = '^S^N:' then                                                   // N E W   U S E R
  18.   begin
  19.     //Removing code chars:
  20.     delete(NET_BUFF,1,5);
  21.     //Playing notification sound:
  22.     if (allow_snd=true) then PlaySound(PChar(snd_new_user),0,SND_ASYNC);
  23.     userlist_view.Clear;
  24.     //Main cycle:
  25.     cl_userlist.DelimitedText:=NET_BUFF;
  26.     for i:=0 to cl_userlist.Count-1 do
  27.     begin
  28.       //Saving current username to temporary string:
  29.       TMP_STR:=cl_userlist.Strings[i];
  30.       if (i=cl_userlist.Count-1) then TMP_LAST:=cl_userlist.Strings[i];
  31.       //Storing av_key of the new user:
  32.       if (pos('#A:',TMP_STR))<>0 then
  33.       begin
  34.         pos_index:=(pos('#A:',TMP_STR));
  35.         av_key:=StrToInt(copy(TMP_STR,pos_index+3,1));
  36.         //Removing avatar key:
  37.         delete(TMP_STR,pos('#A:',TMP_STR),4);
  38.         //Adding user to the userlist:
  39.         userlist_view.AddItem(TMP_STR,nil);
  40.         userlist_view.Items.Item[userlist_view.Items.count-1].ImageIndex:=av_key;
  41.       end else  //if unknown error:
  42.       begin
  43.         Socket.SendText('^D:'+u_name+cl_avatar_key);
  44.         Socket.Close;
  45.         showMessage('Error receiving a socket. Please contact the support.');
  46.         Break;
  47.       end;
  48.     end;
  49.     //Removing av_key from the last connected user:
  50.     delete(TMP_LAST,pos('#A:',TMP_LAST),4);
  51.     //Adding LAST user notification into the chat window:
  52.     w_out.lines.Add(cl_msg_header+TMP_LAST+' - connected to the server.');
  53.   end else
  54.   if NET_CODE = '^S^M:' then                                                   // M E S S A G E
  55.   begin
  56.     delete(NET_BUFF,1,5);
  57.     if (allow_snd=true) then PlaySound(PChar(snd_msg_in),0,SND_ASYNC);
  58.     if (pos('Server message:',NET_BUFF)>0) then
  59.     begin
  60.       w_out.SelLength:=14;
  61.       w_out.SelAttributes.Color:=clRed;
  62.     end else w_out.SelAttributes.Color:=clDefault;
  63.     if not (Showing) and (tray_message=true) then
  64.     begin
  65.       tray.ShowBalloonHint('-New message-',(cl_msg_header+NET_BUFF),bitInfo,10);
  66.       tray.IconList:=tray_msg;
  67.       tray.CycleIcons:=true;
  68.     end;
  69.     w_out.Lines.Add(cl_msg_header+NET_BUFF);
  70.     //Scroll fix:
  71.     SendMessage(w_out.Handle, EM_SCROLL, SB_LINEDOWN, 0);
  72.   end else
  73.   if (NET_CODE = '^S^D:') then                                                 // D I S C O N N E C T   \ K I C K   \   O F F L I N E
  74.   begin
  75.     delete(NET_BUFF,1,5);
  76.       if (NET_BUFF=u_name+cl_avatar_key) then    //if you was kicked
  77.     begin
  78.       clientsocket.Active:=false;
  79.       w_out.Lines.Add(cl_msg_header+' - You have been kicked from the server by Administrator.');
  80.       //Scroll fix:
  81.       SendMessage(w_out.Handle, EM_SCROLL, SB_LINEDOWN, 0);
  82.     end else
  83.       begin
  84.       //finding avatar_key:
  85.       delete(NET_BUFF,pos('#A:',NET_BUFF),(Length(NET_BUFF)-pos('#A:',NET_BUFF)+1));
  86.       //removing disconnected user from userlist:
  87.           n := userlist_view.Items.IndexOf(userlist_view.FindCaption(0,NET_BUFF,true,true,false));
  88.           if (n <> -1) then userlist_view.items.delete(n);
  89.       //userlist-fix:
  90.       tmp_view_style:=userlist_view.ViewStyle;
  91.       userlist_view.ViewStyle:=vsList;
  92.       userlist_view.ViewStyle:=tmp_view_style;
  93.     end;
  94.     end else
  95.   if (NET_CODE = '^S^T:') then                                                 // T Y P I N G {begin}
  96.   begin                                                                        //-KEY_DOWN-
  97.     delete(NET_BUFF,1,5);
  98.     //Saving target avatar:
  99.     t_indx:=StrToInt(Copy(NET_BUFF,pos('#A:',NET_BUFF)+3,(Length(NET_BUFF)-pos('#A:',NET_BUFF))+3) );
  100.     //Changing target avatar:
  101.     TMP_STR2:=Copy(NET_BUFF,0,pos('#A:',NET_BUFF)-1);
  102.     n := userlist_view.Items.IndexOf(userlist_view.FindCaption(0,TMP_STR2,true,true,false));
  103.         if (n <> -1) then userlist_view.items.Item[n].ImageIndex:=11;
  104.   end else
  105.   if (NET_CODE = '^S^S:') then                                                 // T Y P I N G {end}
  106.   begin                                                                        //-KEY_UP-
  107.     delete(NET_BUFF,1,5);
  108.     timer_type.Enabled:=false;
  109.     timer_type.Enabled:=true;
  110.   end;
  111.   timer_fix_out.Enabled:=true;
  112. end;
Add Comment
Please, Sign In to add comment