Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Nancy and VB.NetSignalR, Quartz.Net and ASP.Net »
    comments

    Introduction

    In my previous post I made a SignalR server with Quartz.net and a consoleclient.

    Now it was time to make a webclient. And that took just a bit longer than expected.

    The client

    First of all we go and read the documentation on how to do this.

    I wish I would follow my advice from time to time.

    Then I added an index.html page to my server code (and VS2012 really hated me for that).

    then we add our code.

    1. <!DOCTYPE html>
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4.     <title>NewCases in Becare</title>
    5. </head>
    6. <body>
    7.     <h2 id="title">Waiting for messages</h2>
    8.  
    9.     <p id="datetime"></p>
    10.     <p id="numberofcases"></p>
    11.     <ul id="messages">
    12.     </ul>
    13.     <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    14.     <script src="Scripts/jquery.signalR-1.0.0-alpha2.min.js" type="text/javascript"></script>
    15.     <script src="signalr/hubs" type="text/javascript"></script>
    16.     <script type="text/javascript">
    17.         $(function () {
    18.             var connection = $.hubConnection('http://localhost/');
    19.             var proxy = connection.createHubProxy('beCare');
    20.  
    21.             proxy.on('newCases', function (data) {
    22.                 var d = new Date();
    23.                 var time = d.toLocaleString();
    24.                 $('#title').text('New cases');
    25.                 $('#datetime').text('Last updated: ' + time);
    26.                 $('#numberofcases').text('Number of cases found: ' + data.length);
    27.                 $('#messages').empty();
    28.                 $.each(data, function (i, val) { $('#messages').append('<li>' + val + '</li>'); });
    29.             });
    30.  
    31.             connection.start();
    32.         });
    33.     </script>
    34.    
    35. </body>
    36. </html>

    Some gotchas. I had to give the url for the hubconnection to work. You have to be carefull with all the different syntaxes you might find all over the internet, they just loo similar. For instance $.connection.start(); does not work but might just work in other cases. You can even have a different syntax that should work with hubs too. But for some reason I never got that to work.

    The above works just fine with the server I made earlier. Well it does if you are testing with chrome. It did not work if I asked my users to tests it on their browsers which is by default IE in all shapes and sizes. The error you get is.

    No JSON parser found. Please ensure json2.js is referenced before the SignalR.js file if you need to support clients without native JSON parsing support, e.g. IE<8.

    It might say <ie8 but it didn't work on my IE9 either.

    And of course there is a FAQ for that.

    Why doesn't SignalR work in browser IE6/IE7?

    SignalR requires a JSON parser and ability to send xhr requests (for long polling). If your browser has none, you'll need to include json2.js in your application (SignalR will throw an error telling your you need it as well). You can get it on NuGet.

    But I did not read that before implementing this. Oops.

    So you nuget json2 and add this line before the signalr script tag.

    <script src="Scripts/json2.min.js" type="text/javascript"></script>

    Conclusion

    I should really read the docs before doing this kind of stuff.... nah, it's much more fun this way.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)