What is SignalR?
SignalR is a open source library that are used to communication across the network.You can used this library in webapp as well as mobile app.
What is Owin Cors?
Owin CORS(CROS DOMAIN REQUEST) is used to connect with cross domain client i.e. if i have HUB that run under www.test1.com and client are try to connect from www.test2.com then SignalR giving error for HTTP 500 error.For remove this problem we used CORS(CROSS DOMAIN REQUEST).
ADD these Library in Project--
Add MicroSoft.AspNet.SignalR and MicroSoft.Owin.Cors library in application from Nugget Package Manager Solution.
Sample Application Using SignalR
Step 1- Open Visual Studio and click on new and create a new Asp.net application.
Step 2- Add MicroSoft.Asp.net.SignalR library in Project after adding this library get a script folder in project.
Step 3-Create a class called ChatHub and below code-
Step 3-Create a class called ChatHub and below code-
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalRDemo
{
public class ChatHub:Hub
{
public void Send(string name, string message)
{
Clients.All.broadcastMessage(name, message);
}
}
}
Step 4- Creating a new class which name is startup class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalRDemo.Startup))]
namespace SignalRDemo
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
Step 5-Add HTML Page for user interaction which api.
Step 6- Add below java script code to call method where sndmsg,name,msg is a Id of send Click button,textboxes of name and message
$.connection.hub.start().done(function () { $('#sndmsg').click(function () { // Call the Send method on the hub. chat.server.send($('#name').val(), $('#msg').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); });
Step 7- Add Jquery and signalR script properly in html page.
Step 8- Run Application and open it from different place its work like a chatter application.
No comments:
Post a Comment