比官方教程代码更简短的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测试版]官方教程与升级报告导读部分----------------------------------------------------------------- ...
随机推荐
- 数据结构0103汉诺塔&八皇后
主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...
- angularJS- $http请求
angular使用post.get向后台传参的问题 一.问题的来源 我们都知道向后台传参可以使用get.put,其形式就类似于name=jyy&id=001.但是在ng中我却发现使用$http ...
- LeetCode 104. Maximum Depth of Binary Tree
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
- php 二位数组按某个键值排序
$arr=[ array( 'name'=>'小坏龙', 'age'=>28 ), array( 'name'=>'小坏龙2', 'age'=>14 ), array( 'na ...
- map<虽然一直不喜欢map>但突然觉得挺好用的
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...
- Print a Binary Tree in Vertical Order
http://www.geeksforgeeks.org/print-binary-tree-vertical-order/ package algorithms; import java.util. ...
- noi往届题目泛做
noi2015 Day1 t1 程序自动分析 离散化+并查集 t2 软件包管理器 裸树链剖分 t3 寿司晚宴 状压dp Day2 t1 荷马史诗 哈夫曼多叉树 t2 品酒大会 后缀数组按照hei ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- asp.net mvc引用控制台应用程序exe
起因:有一个控制台应用程序和一个web程序,web程序想使用exe程序的方法,这个时候就需要引用exe程序. 报错:使用web程序,引用exe程序 ,vs调试没有问题,但是部署到iis就报错,如下: ...
- ajax和jquery
ajax的定义: AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 Ja ...