views:

45

answers:

2

I've written an updatescript that updates a few div's every second with serverside info. Now the problem is it seems kinda heavy usage, in google chrome my mouseanimation even keeps loading.

I hope somebody can tell me how to better this script or maybe got an other solution.

Here is my aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns='http://www.w3.org/1999/xhtml' >
<head id="Head1" runat='server'>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery.countdown.js" type="text/javascript"></script>

    <title>Page</title>

</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>



<script type="text/javascript">
    $(function hello() {
        // function that does something exciting? 
        var liftOff = function () {
            // ....  
        };

        // Get a date that is some (short) time in the future 
        var getDeadline = function () {
            var shortly = new Date();
            shortly.setSeconds(shortly.getSeconds() + 5.5);
            return shortly;
        };

        // Attach click handler to all our buttons 
        $("button.resetButton").click(function (event) {

            $.ajax({
                type: "POST",
                url: "WebService.asmx/HelloWorld2",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {},
                failure: function () { alert("Uh oh"); }
            });



        });

        function highlightLast5() {
            $.ajax({
                type: "POST",
                url: "WebService.asmx/HelloWorld",
                data: "{'number':'0'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) { $("div.shortly").countdown('change', { until: msg.d }); },
                failure: function () { alert("Uh oh"); }
            });



        }
        // Start all countdowns going on page load 
        $('div.shortly').countdown({
            until: getDeadline(),
            onTick: highlightLast5,
            onExpiry: liftOff,
            layout: '{sn}'
        });
    });

</script>


    <div class="mainpanel"> 
    <div> 
        test 
    </div> 
    <div class="shortly" > 

    </div> 
    <button id="button" type="button" class="resetButton"> 
        Reset 
    </button> 
</div>
</form>
</body>

</html>

And the webservice which returns the information:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    List<Auction> auctions;
    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
        auctions = (List<Auction>)Application["Auction"];
    }

    [WebMethod]
    public string HelloWorld(int number) {

        return auctions[0].seconds.ToString();
    }

    [WebMethod]
    public void HelloWorld2()
    {
        auctions[0].seconds = 30;

    }

}

Now as you can see the jquery script get's the data every second, this is kinda needed because it's for an live auction. If I use this script on an real server as I tried the server crashes in like 2 minutes because of the heavy usage, this is sadly only with one client open. So whenever more clients would use the script at the same time I would have gotten an real problem.

+3  A: 

Since you want live streaming of data, you could try implementing Comet with, say, WebSync. You could squeeze much more performance, and it will be more scalable.

Alexander Gyoshev
Hmmm also Comet, I really need to go look into this, thanks Aritos and Alexander. If everyone else got something he might help me with, please do so.
Julian
Websync really got a few nice demo's that show it works great. Thanks, although websync seems expensive. Is it posible to make that stuff on your own with comet? Just like 1 or 2 variable pushing through?
Julian
Hm, you could try lightstreamer, too. http://www.lightstreamer.com/ I haven't used Comet in any scenarios, though -- I am only aware of it like a concept. There are many non-.NET implementations - if this is applicable in your scenario. There's also some binding available in Dojo - http://infrequently.org/2006/03/comet-low-latency-data-for-the-browser/
Alexander Gyoshev
Thanks Alexander, I'm looking into all the links to see what is most posible for us. And within the budget ^^
Julian
+2  A: 

About Comet Technique.

Here is a similar question with answer. http://stackoverflow.com/questions/3408735/pushing-messages-to-clients-from-a-server-side-application

Comet is not call the server every second but its just open a connection that stay open from the server side, when the server have something to replay its replay to, and close the connection, and so on...

http://en.wikipedia.org/wiki/Comet_(programming)

http://www.frozenmountain.com/websync/

http://www.aaronlerch.com/blog/2007/07/08/creating-comet-applications-with-aspnet/

Aristos
Thanks I'm going to look into all the links, I'm really glad you helped me here. Had this problem like over 2 months now..
Julian