Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Page:  1  2  next
Avatar
owen #1
Member since Nov 2006 · 21 posts · Location: UK
Group memberships: Members
Show profile · Link to this post
Subject: ASP.NET and agsXMPP - please help!
Hello again.

I have been successfully writing test applications for the desktop, using VB.NET, which react to events (incoming messages) and display them on the screen.  That works great for desktop applications, but what about web pages?

When it comes to ASP.NET, things are understandaby, more difficult.  We have the whole "posting back" of pages to contend with.  Yes, I can use the same VB.NET code to connect to my Wildfire server (login), but then, my web page has loaded, and that's that.  The ".MessageGrabber.Add" statements, set at connection time, are pointless, since the page has loaded and isn't grabbing any messages or indeed doing anything else!

It may sound strange wanting to display messages "live" on a web page, but what I'm trying to provide for users is something they can access from their browser, to see IMs that people send in, "as it happens".

Is there *any* way of setting up a web page (in .net) which can handle agsXMPP events?  A simple example would be displaying user messages on the web page as they come in.  (And I want this part of the web page - not a separate Java or ActiveX control).

Or is ActiveX / Java applets the only way?

Thanks!
Owen
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello owen

you can do it with post backs. But this means you would have a timer and reload the page in a specific interval.
Sure this will work, but i don't think this is a very good solution. For me the only way to go with web applications is AJAX only.

Some days ago Microsoft released the ATLAS Framework. I think this is something you should take a look at.

I'm sorry that we have no web example yet. It's on our TODO list, but there are also other items with higher priority.

Regards,
Alex
Avatar
owen #3
Member since Nov 2006 · 21 posts · Location: UK
Group memberships: Members
Show profile · Link to this post
Thanks Alex.  I do have some 3rd party AJAX derivations (by telerik) but I'm still struggling to get anything to "react" to incoming messages.

OK, let me ask a different question: Has anybody out there actually managed to receive messages and display them "live" on a web page *without the use of a timer*?   (And if anyone's done it, erm, can I have a look at your web page please?)

Actually at this point in time it seems far more productive to ignore the web and go down the desktop route.  But still, I'd like to know if anybody on this forum has managed it.


Owen
Avatar
Alex #4
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by owen:
OK, let me ask a different question: Has anybody out there actually managed to receive messages and display them "live" on a web page *without the use of a timer*?   (And if anyone's done it, erm, can I have a look at your web page please?)

Actually at this point in time it seems far more productive to ignore the web and go down the desktop route.  But still, I'd like to know if anybody on this forum has managed it.

yes, there are many existing XMPP AJAX applications and messengers which are doing this.
Like Meebo, go to http://www.meebo.com, and enter your XMPP account and password.

But i know none for .NET which is open source.

Alex
Avatar
Alex #5
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
there are technologies where you need no timers. Look for reverse AJAX.
You can so run a timer that polls the server with AJAX. Both is better than reloading the page.

Alex
Avatar
tradle #6
Member since Jan 2007 · 10 posts
Group memberships: Members
Show profile · Link to this post
Owen/Alex -

It seems that I'm having a similar issue - but my problem is that I can see text coming from the OnMessage event coming into my application, but I cannot for the life of me get the message text onto the screen.  I've forced postbacks to no avail.  Is there some reason why the inbound text seems cached, and the outbound text is handeled appropriately?  My code - many, many times time revised - is below.

ASPX...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="Microsoft.Web.UI.Controls" TagPrefix="asp" %>
<%@ Register Assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="Microsoft.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test!</title>
     <script type="text/javascript" language="javascript">
          function postBack()
          {
            alert("hi");
            __doPostBack('LinkButton1', '');
          }
         
        </script>
</head>
<body>
    <form id="form1" runat="server">
   
        <asp:Panel ID="pnlChat" runat="server" Height="500px" Width="350px" BorderStyle="Solid">
     <table runat="server" id="tblChat">
            <tr style="width: 350px; height: 150">
                <td style="width: 75px; height: 81px">
                    <asp:Image ID="imgTheirAvatar" runat="server" Height="75px" Width="75px" /></td>
                <td style="width: 350px;" rowspan="2">
                           <asp:TextBox ID="tbChat" runat="server" Height="264px" Width="275px" TextMode="MultiLine"></asp:TextBox>
                                       
                </td>                   
            </tr>
            <tr style="width: 350px; height: 150px">
                <td style="width: 75px; height: 115px">
                    <asp:ListBox ID="lbContacts" runat="server" Height="175px"></asp:ListBox></td>                   
            </tr>
            <tr>
                <td style="width: 350px; height: 75px" colspan="2">
                    <table runat="server" id="tblImgButtons">
                        <tr style="width: 350px; height:75px" align="center">
                             <td style="width: 116px; height: 75px" align="center">
                                <asp:ImageButton runat="server" ID="imgbtnAddToContacts" ImageUrl="~/Images/SrAm File Add a Contact Icon.jpg" OnClick="imgbtnAddToContacts_Click" />
                             </td>
                             <td style="width: 116px; height: 75px" align="center">
                                <asp:ImageButton runat="server" ID="imgbtnInviteOthers" ImageUrl="~/Images/SrAm Group Chat Icon.jpg" />
                             </td>
                             <td style="width: 116px; height: 75px;" align="center">
                                <asp:ImageButton runat="server" ID="imgbtnBlockUser" ImageUrl="~/Images/SrAm Block User Icon.jpg" />
                             </td>
                        </tr>
                    </table> &nbsp;&nbsp; &nbsp;&nbsp;
                </td>
            </tr>
            <tr>               
                <td style="width: 350px; height: 100px" colspan="2">
                    <asp:TextBox ID="tbCompose" runat="server" Font-Names="Tahoma" Height="91px" TextMode="SingleLine" Wrap="true"
                        Width="350px"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 350px; height: 60px" colspan="2">
                    <table id="tblButtons" runat="server">
                        <tr style="width: 350px; height:15px">
                            <td style="width: 175px; height: 15px; vertical-align: middle" align="center">
                                <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />
                            </td>
                            <td style="width: 175px; height: 15px; vertical-align: middle" align="center">
                                <asp:Button ID="btnExit" runat="server" Text="Exit" OnClick="btnExit_Click" />
                            </td>
                        </tr>
                    </table>
                </td>               
            </tr>           
        </table>
        <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>      
        </asp:Panel>
    </form>
</body>
</html>


C#...
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using agsXMPP;
using agsXMPP.Collections;
using agsXMPP.protocol;
using agsXMPP.protocol.client;

public partial class _Default : System.Web.UI.Page
{
    private static XmppClientConnection _con;
    private Jid _jid;
    private static string _type = "add";
    private static string _secret = "pAssw0rd";
    private static string _username = "six";
    private static string _password = "Abcd7890";
    private string _server = "tradlelap";
    private static string _port = "9090";
    public string _testText;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //ScriptManager1.RegisterAsyncPostBackControl(tbChat);

            Login();
        }
        //updpnlMain.Update();      
    }   

    protected void Login()
    {
        ASCIIEncoding _encoding = new ASCIIEncoding();

        string _queryData = string.Format("type={0}&secret={1}&username={2}&password={3}", _type, _secret, _username, _password);

        byte[] _buffer = _encoding.GetBytes(_queryData);

        // Prepare web request...
        WebRequest _request = WebRequest.Create("http://" + _server + ":" + _port + "/plugins/userService/userservice&type=" + _type + "&secret=" + _secret + "&username=" + _username + "&password=" + _password);
        _request.Method = "POST";
        _request.ContentType = "application/x-www-form-urlencoded";
        // Get length of content
        _request.ContentLength = _buffer.Length;
        // Get request stream
        System.IO.Stream _stream = _request.GetRequestStream();
        // Send the data.
        _stream.Write(_buffer, 0, _buffer.Length);
        // Close stream
        _stream.Close();
        // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
        WebResponse _response = (WebResponse)_request.GetResponse();
        // Display the contents of the page to the console.
        System.IO.Stream _streamResponse = _response.GetResponseStream();
        // Get stream object
        StreamReader _streamRead = new StreamReader(_streamResponse);
        Char[] _readBuffer = new Char[256];
        // Read from buffer
        int _count = _streamRead.Read(_readBuffer, 0, 256);
        String _resultData = string.Empty;
        while (_count > 0)
        {
            // get string
            _resultData = new String(_readBuffer, 0, _count);
            // Read from buffer
            _count = _streamRead.Read(_readBuffer, 0, 256);
        }

        if (_resultData.ToString() == "<error>UserAlreadyExistsException</error>\r\n" || _resultData.ToString() == "<result>ok</result>\r\n")
        {
            //some action
        }
        else
        {
            //error
            btnSend.Enabled = false;
        }
        // Release the response object resources.
        _streamRead.Close();
        _streamResponse.Close();
        // Close response
        _response.Close();

        GetConnection(_username, _server, _password);
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        this._jid = new Jid("two", _server, "Exodus");

        if ((XmppClientConnection)Cache.Get("XMPP_con") != null)
        {
            _con = (XmppClientConnection)Cache.Get("XMPP_con");
        }
        else
        {
            GetConnection(_username, _server, _password);
        }

        Message _msg = new Message(_jid, MessageType.chat, tbCompose.Text);

        _con.Send(_msg);

        PushMessage(_msg);
        tbCompose.Text = "";               
    }   

    private void GetConnection(string _user, string _server, string _password)
    {
        _con = new XmppClientConnection();
        _con.SocketConnectionType = agsXMPP.net.SocketConnectionType.Direct;
        _con.Server = _server;
        _con.Resource = "WebMessenger";
        _con.Username = _user;
        _con.Password = _password;
        _con.Port = 5222;
        _con.AutoResolveConnectServer = true;
        _con.AutoRoster = true;
        _con.Priority = 10;
        _con.UseStartTLS = true;

        _con.OnLogin += new ObjectHandler(OnLogin);
        _con.OnMessage += new XmppClientConnection.MessageHandler(OnMessage);

        _con.Open();

        Cache.Insert("XMPP_con", _con);
    }

    private void PushMessage(Message _message)
    {
        StringBuilder _sb = new StringBuilder(tbChat.Text);
        _sb.AppendLine();
        _sb.Append(_username + ": " + _message.Body);

        tbChat.ForeColor = Color.Black;      

        _testText = _sb.ToString();
        tbChat.Text = _testText;

......
Avatar
Alex #7
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello,

agsXMPP is running on the server, so all events are received by the server.
In HTTP all requests are initiated by a client. This is the same with AJAX. But there are ways to notify the client of new events. Look at reverse Ajax technologies, or poll the server in a specific interval from your client.
If you poll the server then you need a queue for all events. In your polls you have to check queue and process it.

Alex
Avatar
tradle #8
Member since Jan 2007 · 10 posts
Group memberships: Members
Show profile · Link to this post
Alex -

I don't disagree with you - but my issue is not with event recognition.  I see the events fire without issue from the server - but this may be due to the manner in which I am testing.  I'm using an Exodus client, connected to my localhost server.  I can send messages out just fine, and do recognize when a message is coming in.  I've tried Session and ViewState to store the existing messages in the chat session.  Every time that I receive a message from Exodus, it seems that I lose my Session/ViewState.  In one sense, this is correct as I'm transferring data between Windows and Web clients.  On the other hand, it doesn't make sense as the Session/ViewState isn't implicitly being destroyed by anything.

The AJAX UpdatePanel/Timer combo seems to work fairly well, but it refreshes my whole page despite the Timer being nested in the UpdatePanel.  I know that this isn't a Microsoft ALAX forum - but I'm post this knowing there are others who are trying to accomplish the same thing.

Off to research reverse AJAX...

Tim
Avatar
Alex #9
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
hi,

i think it would be useful if somebody could attach a vs2005 solution to the thread. Then we can take a look at the code if we have some minutes. Doesn't matter if the code is not working correctly

Alex
Avatar
tradle #10
Member since Jan 2007 · 10 posts
Group memberships: Members
Show profile · Link to this post
I'd be happy to - but I can't seem to get my code into the forum.  Is that maybe a permissions issue?
Avatar
Alex #11
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
if you expand the Post options below the text input field you can attach a file.

Alex
Avatar
tradle #12
Member since Jan 2007 · 10 posts
Group memberships: Members
Show profile · Link to this post
Sorry about that.
The author has attached one file to this post:
WebMessenger.zip 339.1 kBytes
You have no permission to open this file.
Avatar
tradle #13
Member since Jan 2007 · 10 posts
Group memberships: Members
Show profile · Link to this post
All -

This code base works.  It's messy as all get out, but it's a 1.0 kind of thing.

Tim
The author has attached one file to this post:
WebMessenger.zip 338.1 kBytes
You have no permission to open this file.
Avatar
Alex #14
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
i took a quick look at the code.
I think the session stuff looks ok, but let me explain the problem again.

The events OnMessage, OnLogin etc... get raised by agsXMPP which runs on the server. This means if you get this events while debugging they are on the webserver, not on the client. And you can't update the clients GUI without reloading the page or other techniques like temporary polling or reverse AJAX.

The easiest way is polling. This means you have a queue on the server which queues all events from agsXMPP, then poll from the client with AJAX in a given interval and process they queue if there are waiting events.

Alex
Avatar
mansoor #15
User title: Mansoor
Member since Aug 2009 · 1 post
Group memberships: Members
Show profile · Link to this post
In reply to post #13
Subject: zip file is corrupt
Hi there
zip file that u have attached is corrupt i think. Could u plz attach a fresh copy. I need to take a look at your code....Thanks
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:
Page:  1  2  next
Forum: agsXMPP RSS