一 首先引用相关DLL

Startup文件为SignalR的入口

using Microsoft.Owin;using Owin;

[assembly: OwinStartup(typeof(SignalR.Core.Startup))]namespace SignalR.Core
{    public class Startup
    {        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }

    }
}

三 一组SignalR的JS文件



四 实现具体业务的核心类文件,它需要集成Hub,需要使用特性HubName为SignalR起个名字,以便在客户端去调用它

using Microsoft.AspNet.SignalR;using Microsoft.AspNet.SignalR.Hubs;using SignalR.Core.Model;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web;namespace SignalR.Core
{

    [HubName("UrlHub")]    public class UrlValid : Hub
    {        static List ConnectedUsers = new List();        public void Connect(string url,string userID)
        {            var id = Context.ConnectionId;if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
            {
                ConnectedUsers.Add(new CurrentUser
                {
                    ConnectionId = id,
                    UserID = userID,
                });
                Clients.Caller.onConnected(id, userID, url);                //Clients.AllExcept(id).onNewUserConnected(id, userID);
                Clients.Client(id).onNewUserConnected(id, userID);
            }            else
            {

                Clients.Caller.onConnected(id, userID, url);
                Clients.Client(id).onExistUserConnected(id, userID);                // Clients.AllExcept(id).onExistUserConnected(id, userID);            }
        }        /// 
        /// 登出        /// 
        public void Exit(string userID)
        {            var id = Context.ConnectionId;   
            OnDisconnected();
            Clients.Caller.onConnected(id, userID, "");
            Clients.Client(id).onExit(id, userID);
        }        /// 
        /// 断开        /// 
        /// 
        public override System.Threading.Tasks.Task OnDisconnected()
        {            var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);            if (item != null)
            {
                ConnectedUsers.Remove(item);                var id = Context.ConnectionId;
                Clients.All.onUserDisconnected(id, item.UserID);

            }            return base.OnDisconnected();
        }

    }
}

五 核心JS功能代码片断

    
    

    
    
    
    

    
        $(function () {            // Declare a proxy to reference the hub
            var chatHub = $.connection.UrlHub;
            registerClientMethods(chatHub);            // Start Hub            $.connection.hub.start().done(function () {
                registerEvents(chatHub);
            });

        });        //注册客户端事件        function registerEvents(chatHub) {
            $("#btn").click(function () {                var url = "dasfjasldfj";
                chatHub.server.connect(url);
            });

            $("#logOut").click(function () {
                chatHub.server.exit();
            });

        }        //注册客户端方法        function registerClientMethods(chatHub) {

            chatHub.client.onConnected = function (id, userID, url) {
                console.log("与服务器建立了链接" + url);
            }

            chatHub.client.onUserDisconnected = function (id, userID) {
                console.log("与服务器取消了链接");
            }

            chatHub.client.onNewUserConnected = function (id, userID) {
                alert("新用户完成为合法");
            }

            chatHub.client.onExistUserConnected = function (id, userID) {
                alert("用户" + userID + "不能重复登陆");
            }

            chatHub.client.onExit = function (id, userID) {

                alert("用户" + userID + "成功退出!");
            }

        }    



静态类

 public static class BroadCast
    {
        public static void SendAllMessage(string name, string mes)
        {
            IHubContext chat = GlobalHost.ConnectionManager.GetHubContext();
            chat.Clients.All.broadcastMessage(name, mes);
        }

    }