比官方教程代码更简短的SignalR Server Broadcast示例
SignalR是微软ASP.NET技术体系中的新成员。
在www.asp.net网站上的SignalR专区有一篇SignalR的入门级教程《Tutorial: Server Broadcast with ASP.NET SignalR (C#)》,介绍了通过SignalR实现服务器端广播的方法。文章中实现了简单的股票信息实时推送,这是一个服务器-客户端双向实时通信的典型应用。然而我觉得这篇教程虽然简单,但是作为入门的话代码量(特别是无关代码量)显得太多了,当时我用此教程学习的时候不得不从几页代码中上下翻找与SignalR的使用有关的关键点,体验不是很好。于是我自己练习时,另外写了一个简化版的Hello world,现在放出来跟大家交流。
我的示例很简单,就是服务器端定时发起更新客户端页面上的一个字符串消息。消息内容也没有花头,直接在几个固定内容中轮换。
服务器端:
public class MessagesHub : Hub
{
static MessagesHub()
{
StringPusher.Init();
}
}
public static class StringPusher
{
private static string[] _messages = { "这是从服务器推送的消息。", "使用ASP.NET SignalR技术实现。", "从此不再需要客户端定时发送请求。", "可实现双向实时通信。" };
private static System.Timers.Timer _timer = new System.Timers.Timer(3000);
private static IHubConnectionContext _clients = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>().Clients;
private static int _messageIndex = 0;
public static void Init()
{
_timer.Elapsed += (sender, e) => Broadcast();
_timer.Start();
}
public static void Broadcast()
{
_messageIndex = (_messageIndex + 1) % _messages.Length;
_clients.All.showMessage(_messages[_messageIndex]);
}
}
客户端:
$(document).ready(function () {
var messagesHub = $.connection.messagesHub;
messagesHub.client.showMessage = function (msg) {
$('#footer').text(msg);
};
$.connection.hub.start();
});
比官方教程代码更简短的SignalR Server Broadcast示例的更多相关文章
- apk反编译(6)ProGuard 工具 android studio版官方教程[作用,配置,解混淆,优化示例]
ProGuard In this document Enabling ProGuard (Gradle Builds) Configuring ProGuard Examples Decoding O ...
- ContentProvider官方教程(5)ContentResolver插入、更新、删除 示例
Inserting, Updating, and Deleting Data In the same way that you retrieve data from a provider, you a ...
- 【caffe】Caffe的Python接口-官方教程-01-learning-Lenet-详细说明(含代码)
01-learning-Lenet, 主要讲的是 如何用python写一个Lenet,以及用来对手写体数据进行分类(Mnist).从此教程可以知道如何用python写prototxt,知道如何单步训练 ...
- 【caffe】Caffe的Python接口-官方教程-00-classification-详细说明(含代码)
00-classification 主要讲的是如何利用caffenet(与Alex-net稍稍不同的模型)对一张图片进行分类(基于imagenet的1000个类别) 先说说教程到底在哪(反正我是找了半 ...
- Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译
本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译
本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译
本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...
- Google Guava官方教程(中文版)
Google Guava官方教程(中文版) 原文链接 译文链接 译者: 沈义扬,罗立树,何一昕,武祖 校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...
- [苏飞开发助手V1.0测试版]官方教程与升级报告
[苏飞开发助手V1.0测试版]官方教程与升级报告导读部分----------------------------------------------------------------- ...
随机推荐
- WooCommerce插件设置教程之设置主页
http://demo.themes4wp.com/documentation/homepage-setup/#videoimage-tutorial
- [leetcode] 题型整理之cycle
找到环的起点. 一快一慢相遇初,从头再走再相逢.
- [leetcode] 提醒整理之进制
12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- Node.js之NPM工具使用
1.NPM介绍:包管理工具 (1)允许用户从NPM服务器下载别人编写的第三方包到本地石使用 (2)允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用 (3)允许用户将自己编写的包或命令行程 ...
- python学习 3笔记
merge dict def merge(defaults, override): r = {} for k, v in defaults.items(): if k in override: if ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
- 给IDEA设置单独的JDK
一.系统参数设置: 1.64位IDEA:增加IDEA_JDK_64系统变量 2.32位IDEA:增加IDEA_JKD系统变量 如图: 二.参考说明 https://intellij-support.j ...
- Idea中包内中的置文件如何发布到编译后的目录中去
1.问题引入: 运行一个maven+springmvc+hibernate的项目的时候出现了下边的错误: Caused by: java.io.FileNotFoundException: class ...
- my links
如何解決MySQL 開動不到的問題 MySQL start fail 10 BEST JQUERY FILE UPLOAD PLUGINS WITH IMAGE PREVIEWS Spring MVC ...
- SOAPUI使用教程-MockService脚本概述
虽然静态MockOperation和MockResponse模型可以给你一定的灵活性,更多的动态行为添加到您的服务让您更模拟复杂的服务功能是很有用的.例如,你可能想: 从请求到响应传输值,修改它们的值 ...