Signalr 实现心跳包
项目分析:
一个实时的IM坐席系统,客户端和坐席使用IM通信,客户端使用android和ios的app,坐席使用web。
web端可以保留自己的登录状态,但为防止意外情况的发生(如浏览器异常关闭,断网,断电),对坐席的实时在线状态造成影响,我们在后台跑一个服务,实时向每个坐席发送一个心跳包,当坐席的状态是在线,但是又不能接收到服务端的心跳包的时候,认为该坐席已经被异常下线。
实时通信Signalr
使用中发现signalr的服务端必须需要 .net frameword4.5及以上版本,对signalr使用了自行托管,使服务端和页面相互独立。
配置过程:
控制台部分:
1. 用VS创建一个名为 "SignalRSelfHost" 的控制台项目
2. 在程序包管理器控制台,输入如下命令
Install-Package Microsoft.AspNet.SignalR.SelfHost
3. 输入如下命令:
Install-Package Microsoft.Owin.Cors
4. 控制台代码:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Hosting;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers; namespace SignalRSelfHost
{
class Program
{
static void Main(string[] args)
{
// This will *ONLY* bind to localhost, if you want to bind to all addresses
// use http://*:8080 to bind to all addresses.
// See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
// for more information.
string url = "http://localhost:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class MyHub : Hub
{
public static List<User> onlineUsers = new List<User>();
public void Send(string name, string message)
{
Console.WriteLine("client messsage from ["+name+"],message:"+message);
//Clients.All.addMessage(name, "voip:[" + name+"],message:"+message);
var user = onlineUsers.Where(u => u.Voip == name).FirstOrDefault();
Clients.Client(user.ConnectionId).addMessage(user.ConnectionId, "voip:[" + name + "],message:" + message);
} public void LoginIn(string voip) {
var user = onlineUsers.Where(u => u.Voip == voip).FirstOrDefault();
if (user == null)
{
string connId = Context.ConnectionId;
user = new User
{
Voip = voip,
Second = ,
ConnectionId=connId
};
onlineUsers.Add(user);
Console.WriteLine(user.Voip + "上线了");
//Console.ReadLine();
user.HeartBeatAction += () =>
{
SendHeartBeat(connId);
};
user.LogoutAction += () =>
{
LoginOut(voip);
};
}
else {
user.HeartBeatAction += () =>
{
SendHeartBeat(user.ConnectionId);
};
user.LogoutAction += () =>
{
LoginOut(user.Voip);
};
Console.WriteLine(user.Voip + "已经在线了");
Console.ReadLine();
} } /// <summary>
/// 发送心跳包
/// </summary>
/// <param name="voip"></param>
private void SendHeartBeat(string connid)
{
Clients.Client(connid).recieveHeartBeat(connid);
// Clients.All.recieveHeartBeat(voip);
} /// <summary>
/// 接收心跳包
/// </summary>
/// <param name="id"></param>
public void RecieveHeartBeat(string connid)
{
var user = onlineUsers.Where(u => u.ConnectionId == connid).FirstOrDefault();
if (user == null) return;
user.Second = ; } /// <summary>
/// 用户主动下线
/// </summary>
/// <param name="voip"></param>
public void LoginOut(string voip)
{ var user = onlineUsers.Where(u => u.Voip == voip).FirstOrDefault();
Console.WriteLine(user.Voip + " 下线了"); onlineUsers.Remove(user); } private void UserLoginOut(string voip)
{
LoginOut(voip);
}
} public class User
{
public string Voip { get; set; }
public int Second { get; set; }
public string ConnectionId { get; set; } private readonly Timer timer;//定时器
/// <summary>
/// 间隔秒数
/// </summary>
private int During=; /// <summary>
/// 掉线后的操作
/// </summary>
public event Action LogoutAction; /// <summary>
/// 发送心跳包的动作
/// </summary>
public event Action HeartBeatAction;
public User() {
Second = ;
if (timer == null) {
timer = new Timer();
}
timer.Start();//计时器启动
timer.Elapsed += (sender, args) =>
{
Second++;
//每5s发送一次心跳包
if (Second % == ) {
if (HeartBeatAction != null) {
HeartBeatAction();
}
}
if (Second >= During) {
timer.Stop();
timer.Dispose();
//用户30s无心跳包应答,则视为掉线,会抛出事件,然后处理用户掉线动作。
if (LogoutAction != null)
{
LogoutAction();
}
}
}; } }
}
上面的代码包括四个类:
Program,包含程序的主方法.在这个方法中,类型为Startup的web应用程序启动于指定的URL (http://localhost:8080). 如果需要更加安全一点,可以支持SSL. 请去这里看看How to: Configure a Port with an SSL Certificate
Startup, 该类含有SignalR服务端的配置(该教程使用的唯一的配置是用来调用UseCors), MapSignalR为所有形式的Hub对象创建了路由规则.
MyHub, SignalR的Hub 类是程序要提供给客户端的.
User,存储当前登录坐席的信息
js部分:
1. 创建web项目
2. 初始化客户端需要的东西
Install-Package Microsoft.AspNet.SignalR.JS
3. 创建html页,添加客户端代码:
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<!--<input type="hidden" id="displayname" />-->
<span>please enter your name:</span> <input type="text" id="displayname" />
<input type="button" id="btnLogin" value="LoginIn" />
<input type="button" id="btnLoginOut" value="LoginOut" /><br /><br />
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" /> <ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.2.1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="http://localhost:8080/signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
//Set the hubs URL for the connection
$.connection.hub.url = "http://localhost:8080/signalr";
// Declare a proxy to reference the hub.
var chat = $.connection.myHub; // Create a function that the hub can call to broadcast messages.
chat.client.addMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
}; chat.client.recieveHeartBeat = function (connId) {
chat.server.recieveHeartBeat(connId);
// chat.server.send(localStorage.LoginvoipAccount, "1");
console.log('***************************** connId:' + connId);
};
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
}); $('#btnLogin').click(function () {
chat.server.loginIn($('#displayname').val());
}); $('#btnLoginOut').click(function () {
chat.server.loginOut($('#displayname').val());
})
});
});
</script>
</body>
</html>
参考:http://www.cnblogs.com/humble/p/3856357.html
Signalr 实现心跳包的更多相关文章
- 在后台主机中托管SignalR服务并广播心跳包
什么是后台主机 在之前的 Asp.NETCore 轻松学系列中,曾经介绍过一个轻量级服务主机 IHostedService ,利用 IHostedService 可以轻松的实现一个系统级别的后台服务, ...
- 闲说HeartBeat心跳包和TCP协议的KeepAlive机制
很多应用层协议都有HeartBeat机制,通常是客户端每隔一小段时间向服务器发送一个数据包,通知服务器自己仍然在线,并传输一些可能必要的数据.使用心跳包的典型协议是IM,比如QQ/MSN/飞信等协议. ...
- heart beat/心跳包
为什么需要heart beat/心跳包?因为tcp keep-alive不能满足人们的实时性的要求,就是这么简单. socket的长时间连接的话,是需要心跳包.心跳包就是维持双方的连接,每隔一段时间发 ...
- TCP连接探测中的Keepalive和心跳包
TCP连接探测中的Keepalive和心跳包 tcp keepalive 心跳 保活 Linuxtcp心跳keepalive保活1. TCP保活的必要性 1) 很多防火墙等对于空闲socket自动关闭 ...
- 为什么心跳包(HeartBeat)是必须的?
几乎所有的网游服务端都有心跳包(HeartBeat或Ping)的设计,在最近开发手游服务端时,也用到了心跳包.思考思考,心跳包是必须的吗?为什么需要心跳包?TCP没有提供断线检测的方法吗?TCP提供的 ...
- TCP之心跳包实现思路
说起网络应用编程,想到最多的就是聊天类的软件.当然,在这类软件中,一般都会有一个用户掉线检测功能.今天我们就通过使用自定义的HeartBeat方式来检测用户的掉线情况. 心跳包实现思路 我们采用的思路 ...
- socket的心跳包机制
网络中的接收和发送数据都是使用操作系统中的SOCKET进行实现.但是如果此套接字已经断开,那发送数据和接收数据的时候就一定会有问题.可是如何判断这个套接字是否还可以使用呢?这个就需要在系统中创建心跳机 ...
- TCP连接探测中的Keepalive 和心跳包
采用TCP连接的C/S模式软件,连接的双方在连接空闲状态时,如果任意一方意外崩溃.当机.网线断开或路由器故障,另一方无法得知TCP连接已经失效,除非继续在此连接上发送数据导致错误返回.很多时候,这不是 ...
- UDP打洞和心跳包设计
一.设备终端class DeviceClient { int deviceID; int IP; int port; char connectID[16]; time_t lastTime; stru ...
随机推荐
- [整理]在命令行执行 UIAutomation
instruments -t /Developer/Platforms/iPhoneOS.platform/Developer/Library/Instruments/PlugIns/Automati ...
- mmDeferred
前端异步解决方案——mmDeferred Deferred是前端解决异步操作的一种编程范式,后来出现的Promise规范更是让其普适性大大提高.不过Promise规范也存在分岐.现在最流行的是Prom ...
- ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇]
ASP.NET Web API消息处理管道:Self Host下的消息处理管道[下篇] 我们知道ASP.NET Web API借助于HttpSelfHostServer以Self Host模式寄宿于当 ...
- WP中一些耗时的东西
MediaPlayer.GameHasControl 耗时1.5ms MediaPlayer.State 耗时0.4ms 上面两个原本放在游戏的update中,后来注释掉发现其它的游戏逻辑只要0.2m ...
- 探讨C++ 变量生命周期、栈分配方式、类内存布局、Debug和Release程序的区别
探讨C++ 变量生命周期.栈分配方式.类内存布局.Debug和Release程序的区别(一) 今天看博客园的文章,发现博问栏目中有一个网友的问题挺有趣的,就点进去看了下,标题是“C++生存期问题”,给 ...
- mongDB主从
MongoDB[第二篇]MongodDB主从 官方网站:http://www.mongodb.org/ MongoDB的一些参数 --logpath 日志文件路径 --master 指定为主机器 -- ...
- JavaScript设置简单的自动时间
下面就是一段简单的JavaScript设置简单的自动时间,时间显示在一个输入框input里面. <html> <head> </head> <body> ...
- mysqldump备份错误:诡异的#mysql50#.mozilla数据库
今天测试mysql自动备份功能 在red_hat_linux5系统下安装了mysql_5.5 系统提示执行脚本抛错, 手动执行脚本, 返回错误:mysqldump: Got error: 1102: ...
- 安装javajava整合Flex
PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 这几天在学一个叫Flex的东西,之前也没搞过这一类的东西认为既神秘又生疏,随 ...
- schemaeasyui实例:SSh结合Easyui实现Datagrid的分页显示
查了好多资料,发现还是不全,干脆自己整理吧,最少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧! 克日学习Easyui,发现非常好用,界面很雅观.将学习的心得在此写下,这篇博客写SSh结合E ...