惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - 猫猫

解决VS2012上面EF字段说明备注没有的方法 生产百万级随机数 (转帖)C#批量重命名文件代码的实现 MyClipse DataBase Explorer 连接 ACCESS 配置说明 一个文本处理小工具(原创) C# 算法之 冒泡排序 实现数组转换为DataTable TreeView 部署到服务器上无法显示图标(失效) 仿google的suggest C# 把网页内容转为EXCEL,WORD C# 生产新闻文章分页 C# 实现页面3秒后跳转 ASPNET动态生成静态页面 正则表达式30分钟入门教程 转 正则表达式之道 转 正则表达式基础知识 转 常用正则表式 转 转 正则表达式教程 正则表达式教程 (转)
SignalR 2.0 初次使用说明
猫猫 · 2013-11-26 · via 博客园 - 猫猫

如何使用SignalR 2.0

一:首先通过Nuget安装SignalR 2.0 【本人使用的时候最新版本为2.0】2.0与之前1.X有部分命名空间和配置不同请注意

二:建一个专门的类库用来负责SignalR 2.0的服务端

如果之前已经安装好了SignalR 2.0 可能新添加的类库里面没有SignalR 2.0相关代码 那么可以通过以下方式安装进去

安装语句  install-package Microsoft.AspNet.SignalR

注意在前台的SignalR 版本必须跟服务端的一致 不然会出现无法通信的情况

三:服务端代码示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace PB.Common.HubLibrery
{
    [HubName("myHubPms")]
    public class HubPMS : Hub
    {

        public void send(string msg)
        {
            Clients.All.addMessage(msg);
        }

    }
}

在前台建立一个Startup类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(PB.UI.BS.Startup))]

namespace PB.UI.BS
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }

    }
}

在MVC的webconfig里面添加一行代码
appSettings里面添加

  <add key="owin:AppStartup" value="PB.UI.BS.Startup, PB.UI.BS" />

这里的格式是这样的

<add key="owin.AppStartup" value="命名空间.Startup,命名空间"/>

好了现在可以看下前台如何调用

在页面上面引入一下JS

<script src="/Scripts/jquery.signalR-2.0.0.min.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>

JS代码 此JS代码为登陆的时候自动向服务端发送消息

$(function () {
    $("#d_username").focus();
    var IWannaChat = $.connection.myHubPms;

    $.connection.hub.start().done(function () {
        $('#btnLogin').click(function () {
            //服务
            IWannaChat.server.send($("#d_username").val() + "正在登陆中...");
            // Call the Send method on the hub. 
            chat.server.send($('#displayname').val(), $('#message').val());
            // Clear text box and reset focus for next comment. 
            loginIn();
        });
    });



    $("#btnLogin").click(loginIn);
})

 JS2:此JS为接收服务端发送的消息 显示在页面上

$(function () {
    var IWannaChat = $.connection.myHubPms;

    //这个主要是接收后台处理的结果,然后打印到前台来
    IWannaChat.client.addMessage = function (message) {
        $("#log").append("<li>" + message + "</li>");
    };

    //开启(长轮训的方式)
    $.connection.hub.start();
});