Ver Mensaje Individual
  #2  
Antiguo 17-07-2011
Avatar de fide_uci
fide_uci fide_uci is offline
Miembro
 
Registrado: ene 2009
Ubicación: Cuba - La Habana
Posts: 226
Reputación: 16
fide_uci Va por buen camino
Por ejemplo esta seria la clase para implementar los mensajes XMPP.

Código Delphi [-]
unit xmppMessageClass;

interface

uses xmldom, XMLIntf, msxmldom, XMLDoc;

//Los diferentes tipos de mensajes que existen
type TMessageType = (mtChat, mtNormal, mtError, mtGroupChat, mtHeadline);

type TXMPPMessage = class(TObject)
  private
    fXMLDocument: string;
    
    fType: TMessageType;
    fFrom: string;
    fTo: string;
    fSubject: string;
    fBody: string;
    fThread: string;
    fId: string;

  protected
    procedure SetType( pType: TMessageType );
    procedure SetFrom( pFrom: string );
    procedure SetTo( pTo: string );
    procedure SetSubject( pSubject: string );
    procedure SetBody( pBody: string );
    procedure SetThread( pThread: string );
    procedure SetId( pId: string );
  public
    property MessageType: TMessageType read fType write SetType;
    property From: string read fFrom write SetFrom;
    property To_: string read fTo write SetTo;
    property Subject: string read fSubject write SetSubject;
    property Body: string read fBody write SetBody;
    property Thread: string read fThread write SetThread;
    property Id: string read fId write SetId;

    function GetXmlText(): string; 

  constructor Create();
end;

implementation

uses Dialogs, unMain;

{ TXMPPMessage }

constructor TXMPPMessage.Create;
begin

  fXMLDocument := '';

end;

function TXMPPMessage.GetXmlText: string;
begin
  Result := fXMLDocument;
end;

procedure TXMPPMessage.SetBody(pBody: string);
var
  vXMLDoc: TXMLDocument;
begin

  vXMLDoc := TXMLDocument.Create(Form1);

  try
      fBody := pBody;
      vXMLDoc.XML.Text := fXMLDocument;
      vXMLDoc.Active := True;
      vXMLDoc.DocumentElement.ChildNodes['body'].Text := pBody;
      fXMLDocument := vXMLDoc.XML.Text;
   finally
     vXMLDoc.Free;
   end;
end;

procedure TXMPPMessage.SetFrom(pFrom: string);
var
  vXMLDoc: TXMLDocument;
begin

  vXMLDoc := TXMLDocument.Create(Form1);

  try
      fFrom := pFrom;
      vXMLDoc.XML.Text := fXMLDocument;
      vXMLDoc.Active := True;
      vXMLDoc.DocumentElement.Attributes['from'] := pFrom;
      fXMLDocument := vXMLDoc.XML.Text;
   finally
     vXMLDoc.Free;
   end;
end;

procedure TXMPPMessage.SetId(pId: string);
var
  vXMLDoc: TXMLDocument;
begin

  vXMLDoc := TXMLDocument.Create(Form1);

  try
      fId := pId;
      vXMLDoc.XML.Text := fXMLDocument;
      vXMLDoc.Active := True;
      vXMLDoc.DocumentElement.Attributes['id'] := pId;
      fXMLDocument := vXMLDoc.XML.Text;
   finally
     vXMLDoc.Free;
   end;
end;

procedure TXMPPMessage.SetSubject(pSubject: string);
var
  vXMLDoc: TXMLDocument;
begin

  vXMLDoc := TXMLDocument.Create(Form1);

  try
      fSubject := pSubject;
      vXMLDoc.XML.Text := fXMLDocument;
      vXMLDoc.Active := True;
      vXMLDoc.DocumentElement.ChildNodes['subject'].Text := pSubject;
      fXMLDocument := vXMLDoc.XML.Text;
   finally
     vXMLDoc.Free;
   end;
end;

procedure TXMPPMessage.SetThread(pThread: string);
var
  vXMLDoc: TXMLDocument;
begin

  vXMLDoc := TXMLDocument.Create(Form1);

  try
      fThread := pThread;
      vXMLDoc.XML.Text := fXMLDocument;
      vXMLDoc.Active := True;
      vXMLDoc.DocumentElement.ChildNodes['thread'].Text := pThread;
      fXMLDocument := vXMLDoc.XML.Text;
   finally
     vXMLDoc.Free;
   end;
end;

procedure TXMPPMessage.SetTo(pTo: string);
var
  vXMLDoc: TXMLDocument;
begin

  vXMLDoc := TXMLDocument.Create(Form1);

  try
    fTo := pTo;
    vXMLDoc.XML.Text := fXMLDocument;
    vXMLDoc.Active := True;
    vXMLDoc.DocumentElement.Attributes['to'] := pTo;
    fXMLDocument := vXMLDoc.XML.Text;
  finally
    vXMLDoc.Free;
  end;

end;

procedure TXMPPMessage.SetType(pType: TMessageType);
var
  vXMLDoc: TXMLDocument;
begin

  vXMLDoc := TXMLDocument.Create(Form1);

  try

      fType := pType;
      vXMLDoc.XML.Text := fXMLDocument;
      vXMLDoc.Active := True;

      case pType of
        mtChat: vXMLDoc.DocumentElement.Attributes['type'] := 'chat';
        mtNormal: vXMLDoc.DocumentElement.Attributes['type'] := 'normal';
        mtError: vXMLDoc.DocumentElement.Attributes['type'] := 'error';
        mtGroupChat: vXMLDoc.DocumentElement.Attributes['type'] := 'groupchat';
        mtHeadline: vXMLDoc.DocumentElement.Attributes['type'] := 'headline';
      end;

      fXMLDocument := vXMLDoc.XML.Text;
  finally
    vXMLDoc.Free;
  end;
  
end;

end.

Un ejemplo de uso de la clase podria ser.

Código Delphi [-]
procedure (...)
var
  vMessage: TXMPPMessage;
begin

  vMessage := TXMPPMessage.Create;
  vMessage.MessageType := mtChat;
  vMessage.To_ := 'nquesada@jabber.uci.cu';
  vMessage.Subject := 'Esto es un mensaje de prueba';
  vMessage.Thread := '243GG235543G45G-34G-34345VV99';
  vMessage.Id := '32aa4';
  vMessage.From := 'fhernandez@jabber.uci.cu/UCi Messenger';
  vMessage.Body := 'Caballero diganme como va quedando esto por que a mi me parece que esta super chulo !!!';

  ShowMessage(vMessage.GetXmlText());

end;

La llamada a

Código Delphi [-]
vMessage.GetXmlText();

nos devolveria algo como esto, que es lo que el servidor XMPP entiende.

Código:
<message type="chat" to="nquesada@jabber.uci.cu" id="32aa4" from="fhernandez@jabber.uci.cu/UCi Messenger"><subject>Esto es un mensaje de prueba</subject><thread>243GG235543G45G-34G-34345VV99</thread><body>Caballero diganme como va quedando esto por que a mi me parece que esta super chulo !!!</body></message>

Última edición por fide_uci fecha: 17-07-2011 a las 14:46:02.
Responder Con Cita