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

    « SignalR and VB.Net: Hubs and objectsPreprocessor Extensibility in SquishIt 0.9 »
    comments

    Introduction

    If you haven't heard of SignalR then you have probably been living under a rock for the past year or so.

    SignalR is an Async library for .NET to help build real-time, multi-user interactive web applications.

    In other words, it's kind of a client-servery thing.

    So what can we do with it. We can send messages to all the clients that are connected to our server for one.

    Let's try this.

    For this post I took the quickstart example on the SignalR wiki.

    The server

    First thing to do is to setup the server.

    For this I created an Empty ASP.Net web Application.

    Then I did the Nuget thing.

    Install-Package Microsoft.AspNet.SignalR -pre

    Then create a Global.asax file.

    1. Imports System.Web.SessionState
    2. Imports System.Web.Routing
    3. Imports Microsoft.AspNet.SignalR
    4.  
    5. Public Class Global_asax
    6.     Inherits System.Web.HttpApplication
    7.  
    8.     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    9.         RouteTable.Routes.MapConnection(Of MyConnection)("echo", "echo/{*operation}")
    10.     End Sub
    11.  
    12.  
    13. End Class

    this will create the routes that will tell you where you have to go to get your information. As you can see the route echo will use our MyConnection class which we will create next.

    1. Imports Microsoft.AspNet.SignalR
    2. Imports System.Threading.Tasks
    3.  
    4. Public Class MyConnection
    5.     Inherits PersistentConnection
    6.  
    7.     Protected Overrides Function OnReceivedAsync(equest As IRequest, connectionId As String, data As String) As Task
    8.         Return Connection.Broadcast(data)
    9.     End Function
    10.  
    11. End Class

    This is the most basic form. Take the data you recieved and broadcast it to all clients.

    Start this project and you should see something like this.

    Which at least means our server has started.

    Take note of the url you see in your addressbar and remember that for the next session.

    The console client

    The first thing to do after this is to make a consoleapplication to test if it really works.

    Go to your nuget console and copy paste this in. Make sure the nuget console is in the right project first.

    Install-Package -pre Microsoft.AspNet.SignalR.Client

    And here is my slightly improved version of that console app.

    1. Imports Microsoft.AspNet.SignalR.Client
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Dim connection = New Connection("http://localhost:50865/echo")
    7.  
    8.         AddHandler connection.Received, Sub(data) Console.WriteLine(data)
    9.  
    10.         connection.Start().Wait()
    11.  
    12.         Dim line As String = Nothing
    13.         Console.WriteLine("type anything + enter to send, type exit + enter to exit.")
    14.         While line <> "exit"
    15.             line = Console.ReadLine()
    16.             connection.Send(line).Wait()
    17.         End While
    18.     End Sub
    19.  
    20. End Module

    Make sure you use the correct url. You know, the one I told you to remember.
    This will show you the incoming messages via a console.writeline using an AddHandler of the Recieved event of the connection.

    And it will give you the possibility to send messages.

    Build it and go to your output folder where you click on the exe file several times to get several windows open at the same time and start typing in one of them.

    You can see the window I have typed in has the message twice and the other clients will have it once.

    Now that was easy.

    The webclient

    It is just as easy to create the weblient.

    Just create another Empty ASP.Net web application and create an empty index.html file in that.

    Then do the nuget thing again.

    And make sure it created a Scripts folder with 5 js files in it.

    The important file here is jquery.signalR-1.0.0-alpha1.min.js We will need that soon.

    Now copy the following code into the index.html file.

    1. <!DOCTYPE html>
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4.     <title></title>
    5. </head>
    6. <body>
    7.     <script src="http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script>
    8.   <script src="Scripts/jquery.signalR-1.0.0-alpha1.min.js" type="text/javascript"></script>
    9.   <script type="text/javascript">
    10.       $(function () {
    11.           var connection = $.connection('http://localhost:50865/echo');
    12.  
    13.           connection.received(function (data) {
    14.               $('#messages').append('<li>' + data + '</li>');
    15.           });
    16.  
    17.           connection.start();
    18.  
    19.           $("#broadcast").click(function () {
    20.               connection.send($('#msg').val());
    21.           });
    22.       });
    23.   </script>
    24.  
    25.   <input type="text" id="msg" />
    26.   <input type="button" id="broadcast" value="broadcast" />
    27.  
    28.   <ul id="messages">
    29.   </ul>
    30. </body>
    31. </html>

    Make sure you reference the right jquery.signalr file and that you change the url to the signalr server. You know, that url I told you to remember earlier.

    Now you will get a very boring page like this.

    But let's go back to our console app and start typing things in there, and see the magic happen.

    And that's not all you can also do this in the webpage.

    Simples.

    Conclusion

    SignalR is very easy to set up although the doc was wrong in some places. But nothing I couldn't figure out on my own.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    signalr, vb.net
    InstapaperVote on HN

    1 comment

    Comment from: NwCaos [Member] Email
    NwCaos thanks is very useful ... I have tried to Vb translate this lambda expression:

    if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
    ...do someting

    pero no he podido, podrias ayudarme?
    05/29/13 @ 12:58

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