Mert 8/7/2016 0

SignalR Simplified SignalR makes it easy to build real-time multi-user connected applications. The goal of this article is not to provide a detailed explanation of SignalR architecture and internals, but rather to help you to jump start writing SignalR applications. There are 3 steps involved in writing SignalR applications: Add SignalR components either through the new Visual Studio template or the NuGet package. Write the server code in your preferred .NET language. Write the client code in HTML and JavaScript.

 Startup Class

The server needs to know which URL to intercept and direct to SignalR.
To do that, we add an OWIN startup class.
Right-click your project in Solution Explorer,
select Add and Owin Startup Class.
By convention the name of the new class should be Startup but it's not required.
A new class will be created for you and no more changes are required. [assembly: OwinStartup(typeof(SignalRDemo.Startup))] public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here app.MapSignalR(); } }
 Hub Class

Hubs are classes to implement push services in SignalR. 
They provide the abstraction on top of persistent connection and
they are responsible for sending messages between server and client.
Public methods defined in the hub are callable by the clients.
Hub sends messages to clients by invoking client-side methods. Also it detects and handles clients' connections. public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name, message); } }
 Client Code

It connects HTML with JavaScript to send and receive data from the server.

Declare a proxy to reference the ChatHub (defined in the server code).

var chat = $.connection.chatHub;
Create a callback function in the script. The hub class on the server calls 
this function to push content updates to each client. chat.client.broadcastMessage = function (name, message) { // Code the show the content pushed by the server }; Call the Send method on the hub. chat.server.send(name, message); The last step is to start the connection and you're done. $.connection.hub.start();

Report Bug

Please Login to Report Bug

Reported Bugs


Please Login to Comment