Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
assassin #1
Member since Sep 2009 · 1 post
Group memberships: Members
Show profile · Link to this post
Subject: ASP.net example with AJAX
I culled the previous conversations in the forum and modified the WebClient sample that comes with the install. Great library by the way. I finally got what I think is a pretty good simple example of a web chat client. It is not ready to ship but it should get other ASP.net / C# developers up and running in no time.

Thanks to all the other discussions that got me going.

Here is the code for the form:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebClient._Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9.     <form id="form1" runat="server">
  10.     <div>
  11.    
  12.         <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
  13.         </asp:ScriptManager>
  14.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  15.             <ContentTemplate>
  16.                 <asp:TextBox ID="txtConversation" runat="server" Height="108px"  TextMode="MultiLine" Width="474px"></asp:TextBox>
  17.                 <br />
  18.                 <br />
  19.                 <asp:TextBox ID="txtNewText" runat="server" Height="81px" TextMode="MultiLine"
  20.                    Width="472px"></asp:TextBox>
  21.                 <asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
  22.                 </asp:Timer>
  23.                 <br />
  24.                 <br />
  25.                 <asp:Button ID="btnSend" runat="server"
  26.                    onclientclick="javascript:SendText();return false;" Text="Send" />
  27.             </ContentTemplate>
  28.         </asp:UpdatePanel>
  29.         <br />
  30.         <br />
  31.         <br />
  32.    
  33.         <asp:Button ID="cmdLogout" runat="server" onclick="cmdLogout_Click"
  34.            Text="Logout" />
  35.    
  36.     </div>
  37.     <script type="text/javascript">
  38.  
  39.         function SendText()
  40.         {
  41.             var str = document.getElementById('txtNewText').value;
  42.  
  43.             document.getElementById('txtConversation').value = document.getElementById('txtConversation').value + "\n" + "me: " + str;
  44.  
  45.             PageMethods.SendMessage(str);
  46.  
  47.             document.getElementById('txtNewText').value = "";
  48.         }
  49.        
  50.     </script>
  51.     </form>
  52. </body>
  53. </html>

      • AND NOW THE CODE BEHIND CODE ***

  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11.  
  12. using agsXMPP;
  13. using agsXMPP.protocol.client;
  14.  
  15. namespace WebClient
  16. {
  17.     public partial class _Default : System.Web.UI.Page
  18.     {
  19.         private XmppClientConnection xmpp;
  20.         private ArrayList messages;
  21.  
  22.         protected void Page_Load(object sender, EventArgs e)
  23.         {
  24.             if (!IsPostBack && ! IsCallback)
  25.             {
  26.                 xmpp = (XmppClientConnection)Cache["xmpp"];
  27.                 if (xmpp == null)
  28.                 {
  29.                     xmpp = new XmppClientConnection();
  30.                     Cache["xmpp"] = xmpp;
  31.                 }
  32.                 xmpp.OnLogin += new ObjectHandler(xmpp_OnLogin);
  33.  
  34.                 xmpp.AutoPresence = true;
  35.  
  36.                 xmpp.AutoResolveConnectServer = true;
  37.                 xmpp.Port = 5222;
  38.                 xmpp.UseSSL = false;
  39.                 xmpp.Server = "ta-test";
  40.                 xmpp.Username = "chrisp";
  41.                 xmpp.Password = "chris";
  42.  
  43.                 xmpp.Open();
  44.  
  45.                 xmpp.OnMessage += new MessageHandler(xmpp_OnMessage);
  46.  
  47.                 messages = new ArrayList();
  48.                 Cache["messages"] = messages;
  49.             }
  50.         }
  51.  
  52.         void xmpp_OnMessage(object sender, Message msg)
  53.         {
  54.             ArrayList messages = Cache["messages"] as ArrayList;
  55.             messages.Add(msg.Body);
  56.         }
  57.  
  58.         void xmpp_OnLogin(object sender)
  59.         {
  60.             xmpp.Send(new Message(new Jid("matth@ta-test"), MessageType.chat, "Hello, how are you?"));
  61.         }
  62.  
  63.         protected void cmdLogout_Click(object sender, EventArgs e)
  64.         {
  65.             xmpp = (XmppClientConnection)Cache["xmpp"];
  66.             xmpp.Close();
  67.         }
  68.  
  69.         protected void Button1_Click(object sender, EventArgs e)
  70.         {
  71.             txtConversation.Text = txtConversation.Text + "\n" + "me: " + txtNewText.Text;
  72.             xmpp.Send(new Message(new Jid("matth@ta-test"), MessageType.chat, txtNewText.Text));
  73.         }
  74.  
  75.         protected void Timer1_Tick(object sender, EventArgs e)
  76.         {
  77.             ArrayList messages = Cache["messages"] as ArrayList;
  78.             if (messages != null)
  79.             {
  80.                 if (messages.Count > 0)
  81.                 {
  82.                     foreach (string message in messages)
  83.                     {
  84.                         txtConversation.Text = txtConversation.Text + "\n" + "chrisp: " + message;
  85.                     }
  86.                 }
  87.                 messages.Clear();
  88.             }
  89.         }
  90.  
  91.         [System.Web.Services.WebMethod]
  92.         public static void SendMessage(string uniqueID, string text)
  93.         {
  94.             XmppClientConnection xmpp = (XmppClientConnection)HttpRuntime.Cache["xmpp"];
  95.             xmpp.Send(new Message(new Jid("matth@ta-test"), MessageType.chat, text));
  96.         }
  97.     }
  98. }
This post was edited on 2014-04-10, 21:54 by Alex.
Avatar
iceball12 #2
Member since Nov 2009 · 3 posts
Group memberships: Members
Show profile · Link to this post
do you think that it is possible to update the panels on message event and not using a timer?
Its gonna be pretty difficult to create a workable web client when everyevent from the XmppClientConnection  cannot be directly processed but has to be queed and processed on  timer event.

I want to show a popup right after i get a chat room invite etc.
Avatar
Alex #3
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
yes it is possible. The technology which does this is called Comet.
http://en.wikipedia.org/wiki/Comet_%28programming%29

using comet you can pass events back and forth in realtime without timers,

Is Silverlight an option for you? Then you should take a look at our MatriX library.

Alex
Avatar
iceball12 #4
Member since Nov 2009 · 3 posts
Group memberships: Members
Show profile · Link to this post
awesome!
I am trying to do i manually but i cant seem to ever find the right controls to update.

So building an chat client using asp.net ajax control without timers should be possible using the xmpp api events !
Avatar
iceball12 #5
Member since Nov 2009 · 3 posts
Group memberships: Members
Show profile · Link to this post
Do you have any example or more information about how to implement this using asp.net ajax controls?
Avatar
Alex #6
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
there are some good articles with code examples on CodeProject:
http://www.codeproject.com
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: agsXMPP RSS