项目分析:

  一个实时的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" />&nbsp;&nbsp;
<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>:&nbsp;&nbsp;' + 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 实现心跳包的更多相关文章

  1. 在后台主机中托管SignalR服务并广播心跳包

    什么是后台主机 在之前的 Asp.NETCore 轻松学系列中,曾经介绍过一个轻量级服务主机 IHostedService ,利用 IHostedService 可以轻松的实现一个系统级别的后台服务, ...

  2. 闲说HeartBeat心跳包和TCP协议的KeepAlive机制

    很多应用层协议都有HeartBeat机制,通常是客户端每隔一小段时间向服务器发送一个数据包,通知服务器自己仍然在线,并传输一些可能必要的数据.使用心跳包的典型协议是IM,比如QQ/MSN/飞信等协议. ...

  3. heart beat/心跳包

    为什么需要heart beat/心跳包?因为tcp keep-alive不能满足人们的实时性的要求,就是这么简单. socket的长时间连接的话,是需要心跳包.心跳包就是维持双方的连接,每隔一段时间发 ...

  4. TCP连接探测中的Keepalive和心跳包

    TCP连接探测中的Keepalive和心跳包 tcp keepalive 心跳 保活 Linuxtcp心跳keepalive保活1. TCP保活的必要性 1) 很多防火墙等对于空闲socket自动关闭 ...

  5. 为什么心跳包(HeartBeat)是必须的?

    几乎所有的网游服务端都有心跳包(HeartBeat或Ping)的设计,在最近开发手游服务端时,也用到了心跳包.思考思考,心跳包是必须的吗?为什么需要心跳包?TCP没有提供断线检测的方法吗?TCP提供的 ...

  6. TCP之心跳包实现思路

    说起网络应用编程,想到最多的就是聊天类的软件.当然,在这类软件中,一般都会有一个用户掉线检测功能.今天我们就通过使用自定义的HeartBeat方式来检测用户的掉线情况. 心跳包实现思路 我们采用的思路 ...

  7. socket的心跳包机制

    网络中的接收和发送数据都是使用操作系统中的SOCKET进行实现.但是如果此套接字已经断开,那发送数据和接收数据的时候就一定会有问题.可是如何判断这个套接字是否还可以使用呢?这个就需要在系统中创建心跳机 ...

  8. TCP连接探测中的Keepalive 和心跳包

    采用TCP连接的C/S模式软件,连接的双方在连接空闲状态时,如果任意一方意外崩溃.当机.网线断开或路由器故障,另一方无法得知TCP连接已经失效,除非继续在此连接上发送数据导致错误返回.很多时候,这不是 ...

  9. UDP打洞和心跳包设计

    一.设备终端class DeviceClient { int deviceID; int IP; int port; char connectID[16]; time_t lastTime; stru ...

随机推荐

  1. VS2012的安装项目只能用InstallShield Limited Edition

    [吐槽]VS2012的安装项目只能用InstallShield Limited Edition[附资源下载]   以前版本的Visual Stuido中安装项目都可以使用微软自家的Visual Stu ...

  2. BlackBerry Phonegap项目的搭建

    前言 事实上,这称不上是搭建,因为整个项目依旧是phonegap包里的例子项目,是使用ant来构建的. 准备材料: 1. JDK(不是java系的童鞋请自觉离开) 2. Apache ant http ...

  3. UI基础UIButton

    UI基础UIButton 前面写了UIWindow.UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实 ...

  4. linux memory release commands内存清理/释放命令

    linux 内存清理/释放命令 You could find reference from here: http://jingyan.baidu.com/article/597a06436a687f3 ...

  5. mosquitto awareness when before it's being compiling, and you do settings and testing

    Mostly, this clearify the usage of   ' mosquitto.conf ' in easy-understanding language.   1. compile ...

  6. 一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!

    一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法! 对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有 ...

  7. linux下http服务器开发

    linux下http服务器开发 1.mystery引入 1)超文本传输协议(HTTP)是一种应用于分布式.合作式.多媒体信息系统的应用层协议 2)工作原理 1)客户端一台客户机与服务器建立连接后,会发 ...

  8. OleDbCommand cmd.Parameters.AddWithValue 添加参数时需要按照存储过程参数的顺序加入

    在使用存储过程时,参数出入的顺序要一致.

  9. 怎样求FIRST集、FOLLOW集和SELECT集

    一,要知道什么是终结符和非终结符. 终结符:通俗的说就是不能单独出现在推导式左边的符号,也就是说终结符不能再进行推导. 非终结符:不是终结符的都是非终结符.(非男即女,呵呵) 如:A-->B,则 ...

  10. LeetCode题解 15题 第二篇

    之前写过一篇,这是第二篇.上一篇用了多种编程语言来做,这一次是以学算法为主,所以打算都用python来完成. 4. Median of Two Sorted Arrays There are two ...