SignalR推送框架两个项目永久连接通讯使用

SignalR是.net中一个不错的推送框架。它包含长连接跟集线器两种方式,我需要的是两个项目之间的通讯,也就是A控制台项目,向B web项目发送数据,然后B就推送到各个用户页面。
下面的代码是A控制台项目代码

class getData
    {
        //日志输出
        private static ILog log = log4net.LogManager.GetLogger(typeof(getData));
        //连接web
        public  static Connection connection =new Connection("http://localhost:4669/webConnections");

        static getData() //静态构造初始化长连接参数
        {
            //添加头文件,这样B就知道是A传输数据而不是页面客户端,还可以使用connection.CookieContainer来区分
            connection.Headers.Add("where", "windowserver");

            try
            {
                connection.Received+=Console.WriteLine;//控制台输出
                connection.Start().Wait();  //wait()方法十分重要,要不然就异步执行后面的语句了
            }
            catch (System.Exception ex)
            {
                log.Error("无法连接web应用,Start方法不能使用" + ex.ToString());
                throw ex;
            }
        }
        public void QueryDataAndToWeb(object sta)
        {
            string datajson = connection.JsonSerializeObject(sta);//序列化成JSON传送到web       
            try
            {
                connection.Send(datajson).Wait();
            }
            catch (System.Exception ex)
            {
                Console.Write(ex);
                log.Error("无法连接web应用,Send方法不能使用" + ex.ToString());
            }
        }

    }

在上述代码中,我只是简单的写出使用方法,一些异常处理,还没处理完。
下面贴出B web项目
创建一个Startup.cs

[assembly: OwinStartup(typeof(Startup))]
namespace signalr
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
            // 配置上文实现的ChatConnections
            app.MapSignalR<ChatConnection>("/webConnections");
        }
    }
}

ChatConnection类:

public class ChatConnection : PersistentConnection
    {
        private static string _WinServiceID = "";//为空字符串表示断线,用于从内部触发推送
        protected override Task OnConnected(IRequest request, string connectionId)
        {
             string where = request.Headers.Get("where");
            if (where !=null&&where.Equals("windowserver")) 
                 _WinServiceID = connectionId;

            return Connection.Send(connectionId, "welcome!");
        }

        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
           string where = request.Headers.Get("where");
            if (where !=null&&where.Equals("windowserver"))
                Connection.Broadcast(Newtonsoft.Json.JsonConvert.DeserializeObject(data), connectionId);
            else
            {//说明页面需要一些请求比如添加删除修改之类东西,并且需要发送到A项目
                Connection.Send(_WinServiceID, data);
            }
        }
    }

这个时候有个问题就是A项目接受B项目发送的数据,比如B项目客户端页面删除,增加,修改A项目的东西,那就不能用控制台输出,这时候需要对传送的数据进行分析是增加还是删除还是修改。

connection.Received += data => { connection_Received(data); };//用这个替换上面代码  connection.Received+=Console.WriteLine;
  public void connection_Received(string obj)
        { //在这里面对web发送的数据进行解析然后进行操作
            switch (obj)
            {
                case "delete":
                    break;
                default: //包含添加跟更新两种命令 
                    break;
            }

        }

页面javaScript的客户端连接我就不贴出代码了,那些百度搜索资料很多也可以看下面给出的链接地址。
这样就完成一个简单的两个项目之间的通讯,两者之间可以相互利用JSON通讯。

SignalR参考链接:

https://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr#run
http://www.cnblogs.com/Wddpct/p/5650015.html#2.1

猜你喜欢

转载自blog.csdn.net/a748448660/article/details/53725561