Sunday 16 June 2013

Using Jabber-net to talk to OpenFire

After Installing Ignite’s OpenFire and Spark (IM server and client),  using Jabber-Net, I wrote a O2 Platform C# script to access OpenFire, which will:
  • Connect to a local OpenFire server
  • Login as a user
  • Find the HuBot room
  • Join that room
  • Post public messages to it
(see end of post for the source code)
image

The script can be compiled as a stand-alone exe:

image

....which when executed will show a list of current messages

image

... from the HuBot chat room:

image

To see the interaction between both tools, we can write a message on the Spark client (on the bottom-left):

image

... which shows in the Jabber Test tool

image

... and a message from the Jabber Test tool (on the top-right)

image

... will show in Spark

image

Troubleshoot: If you try this script and get a pink treeview, it means the connection or login failed:

image

Source Code used to create the Jabber Test tool
//var credentialsFile = @"..\_UserData\TestAccounts.xml".inTempDir();
//var account = credentialsFile.credential("OpenFileClient");
var userName = "o2_user"; 
var password = "aaaaa"; 

var firstMessage     = "Hello room";
var serverName      = "win-fgnq5aarj8o";
var roomName         = "hubot";

var jabberClient = new JabberClient()
    {
        Server = "localhost",
        Port = 5222,
        User = userName,
        Password = password,
        AutoStartTLS = false
    };
    
var conferenceManager = new ConferenceManager
        {
            Stream = jabberClient
        };

var jid     = new JID(roomName, "conference."+serverName,userName);
var room     = conferenceManager.GetRoom(jid);               


//var topPanel = panel.add_Panel(true);
var topPanel = "Jabber Test".popupWindow()
                            .insert_LogViewer();
var treeView = topPanel.add_TreeView();

Action<string,string> showMessage = 
    (from,text) =>{
                     var nodeText = "{0}: {1}: {2}".format(DateTime.Now.ToShortTimeString(), from,text);
                     treeView.add_Node(nodeText).selected();                     
                  };
                 
Action<string> sendPublicMessage =
    (text)=>{
                room.PublicMessage(text);    
                showMessage(userName, text);
            };    
    
room.OnJoin += (r)=>
        {                            
            treeView.white();
            sendPublicMessage(firstMessage);
        };
room.OnRoomMessage+= (sender,message)=>
        {
            "[OnRoomMessage] {0}".info(message);            
            showMessage(message.From.User, message.Body);                    
        };
                        
Action onAuthenticate = 
    ()=>{    
            "[OnAuthenticate]".info();                         
            room.Join();
        };
Action onConnect = 
    ()=>{
            "[OnConnect]".info();             
            //O2Thread.mtaThread(()=>jabberClient.Login());  // this wasn't working all the time            
        };
        
jabberClient.OnConnect         += (sender,stream)=> onConnect(); 
jabberClient.OnAuthenticate += (sender)=> onAuthenticate(); 

topPanel.insert_Above(20).add_LabelAndComboBoxAndButton("Message","","Send",sendPublicMessage);

treeView.pink();

jabberClient.Connect();

200.wait();
jabberClient.Login();                                     // have to trigger this from here


//var state =  (BaseState)jabberClient.property("State");
return "done";

//using jabber;
//using jabber.client
//using jabber.connection
//Installer:JabberNet_Installer.cs!JabberNet/jabber-net.dll
//O2Ref:JabberNet/jabber-net.dll