首页 技术 正文
技术 2022年11月18日
0 收藏 855 点赞 4,812 浏览 2290 个字

1、参考资料:

http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server

http://signalr.net/

http://www.asp.net/signalr/videos/getting-started/signalr-and-web-sockets

.Net FrameWork4.0版本只支持SginalR1.xx的版本,SignalR2.xxx需要Framework更高的版本支持

SignalR1.x应用   http://www.asp.net/signalr/overview/older-versions/tutorial-getting-started-with-signalr

SignalR support in .Net 4        http://stackoverflow.com/questions/15607252/signalr-support-in-net-4

2、Demo

第一步:创建hub公共类,注意只有设置为public类和方法才能被客户端调用;服务器端调用客户端方法–Clients.All.addMessage(message);

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
namespace SignalRDemo
{
public class MyChat : Hub
{
/// <summary>
/// Sends the specified message.
/// </summary>
/// <param name="message">The message.</param>
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.All.addMessage(message);
}
}
}

第二步:定义客户端可以连接到你的hub的路由,调用MapSignalR扩展方法

using Microsoft.Owin;
using Owin;[assembly: OwinStartupAttribute(typeof(SignalRDemo.Startup))]
namespace SignalRDemo
{
public partial class Startup {
public void Configuration(IAppBuilder app) {
app.MapSignalR();
}
}
}

第三步:前端调用hub类方法

1、路由:    <script src=’/signalr/hubs’></script>

2、创建proxy对象 chat = $.connection.myChat;

3、调用服务器端方法 chat.server.send();

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/jquery.signalR-2.2.0.js"></script>
<script src='/signalr/hubs'></script>
<title></title>
</head>
<body>
<script>
var chat;
$(function () {
// Created proxy,此处要特别注意,Hub类的首字母是大写MyChat,但前端使用时,首字母要小写
chat = $.connection.myChat;
// Assign a function to be called by the server
chat.client.addMessage = onAddMessage;
// Register a function with the button click
$("#broadcast").click(onBroadcast);
// Start the connection
$.connection.hub.start().done(function (a) {
console.log("成功");
console.log(a);
});
});
function onAddMessage(message) {
// Add the message to the list
$('#messages').append('<li>' + message + '</li>');
};
function onBroadcast() {
chat.server.send($('#message').val());
}
</script>
<form id="form1" runat="server">
<div>
<input type="text" id="message" />
<input type="button" id="broadcast" value="broadcast" />
<ul id="messages"></ul>
</div>
</form>
</body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,080
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,554
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,403
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,178
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898