Introduction
Hello friends, This blog is related to a chat application which conatins the exchanging the words in between and also you can share the files.This is a live chat applcation which uses SignalR as a Dot net library for exchanging the data. Lets start the discussion in brief.I hope you will be able to integrate this applcation in your projects after reading this blog.
What is SignalR ?
SignalR is an asp.net library which helps you to add real time functionality to your application.It communicates between server and client to transfer the data in between without refreshing the page.
How to add signalR to the project ?
1. Open any visual studio project in VS 2012
2. Right click on the solution.You will get an option of "Manage NuGet Packages" select the option.
3. Add Microsoft ASP.NET signalR,OWIN and Microsoft.OWIN to your project
OWIN will provode the startup interface to the project and microsoft.owin provides a set of helper tyeps for creating the owin components.
4.After installing all the packages.Add a startup file startup.cs which is a starting point of application to map signalR.
using System.Web.Routing;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(ProjectName.Startup))]
namespace ProjectName
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
- chatHub.server.connect()
- chatHub.server.sendMessageToAll(mydata)
- chatHub.server.sendPrivateMessage(mydata)
- chatHub.client.onConnected
- chatHub.client.onNewUserConnected
- chatHub.client.NoExistAdmin
- chatHub.client.onUserDisconnected
- chatHub.client.messageReceived
- chatHub.client.sendPrivateMessage
First three are the definitions which will call the server side methods which will be explained later.Next six are the client side methods which are explained below.myData is a parameterlist(Optional) which can be sent between the client and server method.The data required to write the code can be sent through the parameters of the definitions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace ProjectName
{
public class ChatHub : Hub
{
#region Data Members
public void Connect(string myData)
{
var id = Context.ConnectionId;
// send to caller
Clients.Caller.onConnected(myData);
// send to all except caller client
Clients.AllExcept(id).onNewUserConnected(myData));
}
public void SendMessageToAll(string myData)
{
// Broad cast message
Clients.All.messageReceived(myData);
}
public void SendPrivateMessage(string toUserId, string message)
{
string fromUserId = Context.ConnectionId;
if (toUserId!= null && fromUserId !=null)
{
// send to
Clients.Client(toUserId).sendPrivateMessage(fromUserId, message);
// send to caller user
Clients.Caller.sendPrivateMessage(toUserId,message);
}
}
public override Task OnDisconnected(bool stopCalled)
{
var id = Context.ConnectionId;
Clients.All.onUserDisconnected(id);
return base.OnReconnected();
}
public void SendMessageToGroup(string myData)
{
groupID = "1";
Clients.Group(groupID).getMessages(myData);
}
#endregion
}
}