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:
Thanks to all the other discussions that got me going.
Here is the code for the form:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebClient._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
- </asp:ScriptManager>
- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
- <ContentTemplate>
- <asp:TextBox ID="txtConversation" runat="server" Height="108px" TextMode="MultiLine" Width="474px"></asp:TextBox>
- <br />
- <br />
- <asp:TextBox ID="txtNewText" runat="server" Height="81px" TextMode="MultiLine"
- Width="472px"></asp:TextBox>
- <asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
- </asp:Timer>
- <br />
- <br />
- <asp:Button ID="btnSend" runat="server"
- onclientclick="javascript:SendText();return false;" Text="Send" />
- </ContentTemplate>
- </asp:UpdatePanel>
- <br />
- <br />
- <br />
- <asp:Button ID="cmdLogout" runat="server" onclick="cmdLogout_Click"
- Text="Logout" />
- </div>
- <script type="text/javascript">
- function SendText()
- {
- var str = document.getElementById('txtNewText').value;
- document.getElementById('txtConversation').value = document.getElementById('txtConversation').value + "\n" + "me: " + str;
- PageMethods.SendMessage(str);
- document.getElementById('txtNewText').value = "";
- }
- </script>
- </form>
- </body>
- </html>
- AND NOW THE CODE BEHIND CODE ***
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using agsXMPP;
- using agsXMPP.protocol.client;
- namespace WebClient
- {
- public partial class _Default : System.Web.UI.Page
- {
- private XmppClientConnection xmpp;
- private ArrayList messages;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack && ! IsCallback)
- {
- xmpp = (XmppClientConnection)Cache["xmpp"];
- if (xmpp == null)
- {
- Cache["xmpp"] = xmpp;
- }
- xmpp.AutoPresence = true;
- xmpp.AutoResolveConnectServer = true;
- xmpp.Port = 5222;
- xmpp.UseSSL = false;
- xmpp.Server = "ta-test";
- xmpp.Username = "chrisp";
- xmpp.Password = "chris";
- xmpp.Open();
- Cache["messages"] = messages;
- }
- }
- void xmpp_OnMessage(object sender, Message msg)
- {
- ArrayList messages = Cache["messages"] as ArrayList;
- messages.Add(msg.Body);
- }
- void xmpp_OnLogin(object sender)
- {
- }
- protected void cmdLogout_Click(object sender, EventArgs e)
- {
- xmpp = (XmppClientConnection)Cache["xmpp"];
- xmpp.Close();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- txtConversation.Text = txtConversation.Text + "\n" + "me: " + txtNewText.Text;
- }
- protected void Timer1_Tick(object sender, EventArgs e)
- {
- ArrayList messages = Cache["messages"] as ArrayList;
- if (messages != null)
- {
- if (messages.Count > 0)
- {
- foreach (string message in messages)
- {
- txtConversation.Text = txtConversation.Text + "\n" + "chrisp: " + message;
- }
- }
- messages.Clear();
- }
- }
- [System.Web.Services.WebMethod]
- public static void SendMessage(string uniqueID, string text)
- {
- XmppClientConnection xmpp = (XmppClientConnection)HttpRuntime.Cache["xmpp"];
- }
- }
- }