4

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">

<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.

Julian
  • 1,105
  • 2
  • 26
  • 57
  • 1
    Do you know about comet programming style ? http://en.wikipedia.org/wiki/Comet_(programming). Maybe this is what you need to do, and not make every second requests. – Aristos Aug 21 '10 at 11:48
  • Never heard of it, you maybe got good link you have used or something? Btw don't look the variable names and other stuff looking weird, it's just for testing so no code cleaning etc. – Julian Aug 21 '10 at 11:50

2 Answers2

3

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.

Alex Gyoshev
  • 11,929
  • 4
  • 44
  • 74
  • 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 Aug 21 '10 at 11:51
  • 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 Aug 21 '10 at 11:54
  • 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/ – Alex Gyoshev Aug 21 '10 at 12:00
  • Thanks Alexander, I'm looking into all the links to see what is most posible for us. And within the budget ^^ – Julian Aug 21 '10 at 12:03
3

About Comet Technique.

Here is a similar question with answer. 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/

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • 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 Aug 21 '10 at 11:55