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 ...
随机推荐
- 运用Unity实现AOP拦截器
运用Unity实现AOP拦截器[结合异常记录实例] 本篇文章将通过Unity实现Aop异常记录功能:有关Unity依赖注入可以看前两篇文章: 1:运用Unity实现依赖注入[结合简单三层实例] 2:运 ...
- 使用axis2访问webservice(webserivice基于.net平台实现)
webservice url=http://10.90.11.240:8081/ExceptionWebService.asmx?WSDL: 下载axis2组件,解压,进入bin目录,通过命令wsdl ...
- ul和li实现分两列(多列)布局显示
简单语句实现DIV+CSS分两列(多列)布局显示 <style type="text/css"> .my ul { width: 210px; } .my li { w ...
- javascript中字符串常用操作整理
javascript中字符串常用操作整理 字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用 ...
- jQuery.fn.attr与jQuery.fn.prop
jQuery.fn.attr与jQuery.fn.prop jQuery.fn.attr.jQuery.fn.prop的区别 假设页面有下面这么个标签,$('#ddd').attr('nick').$ ...
- linux访问windows共享文件夹
如有版权问题,纯属巧合! ======================================================= windows 上开启共享目录 比如共享了X盘的share文件 ...
- win32多线程-重写消息循环
最近正在学习<win32多线程程序设计>,这是其中一段重写消息循环的代码事例,以后可能用的上. while (!quit || gNumPrinting > 0) { // Wait ...
- Python中BeautifulSoup中对HTML标签的提取
一开始使用了beautifulSoup的get_text()进行字符串的提取,后来一直提取失败,并提示错误为TypeError: 'NoneType' object is not callable 返 ...
- java ArrayList的序列化分析
一.绪论 所谓的JAVA序列化与反序列化,序列化就是将JAVA 对象以一种的形式保持,比如存放到硬盘,或是用于传输.反序列化是序列化的一个逆过程. JAVA规定被序列化的对象必须实现java.io.S ...
- Fragment与Activity交互(使用Bundle)
将需要传输的数据封装在一个Bundle对象里,然后将该Bundle对象通过 fragment.setArguments()放到fragment内. Bundle arguments = new Bun ...