一步一步学习SignalR进行实时通信_6_案例1
标签(空格分隔): SignalR
前言
由于这段时间在找房子,所以耽误了一段时间。前几讲把一些基础的东西稍微介绍了下,这一讲就简单介绍一个小例子,大致功能是实现在线聊天,并显示在线用户、用户上下线实时提醒。这个例子并不复杂只是把之前介绍联系起来。
在这里为了简单起见,暂时不涉及到数据库的操作。
类的定义
用户信息类UserInfo
public class UserInfo
{
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"><summary></span></span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> ConnectionId</span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"></summary></span></span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> ConnectionId { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"><summary></span></span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> 姓名</span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"></summary></span></span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"><summary></span></span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> 上线时间</span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"></summary></span></span>
<span class="hljs-keyword">public</span> DateTime ConnectedAt { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"><summary></span></span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> 在线标志</span>
<span class="hljs-comment"><span class="hljs-doctag">///</span> <span class="hljs-doctag"></summary></span></span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">bool</span> IsOnline { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
}
各块功能
后台
上线
public override Task OnConnected()
{
Interlocked.Increment(ref _usersCount);
var user = new UserInfo()
{
ConnectionId = Context.ConnectionId,
IsOnline = true,
Name = "user" + _usersCount,
ConnectedAt = DateTime.Now
};
_users[Context.ConnectionId] = user;
var notifyAll = (Task)Clients.All.NewUserNotification(user);
var updateMessage = (Task)Clients.Caller.UpdateMessage(user.Name, _users.Values.ToArray());
var sendToAll = (Task)Clients.Others.welcome(_users.Values.ToArray());
return notifyAll.ContinueWith(_ => updateMessage).ContinueWith(_ => sendToAll);
}
下线
public override Task OnDisconnected()
{
UserInfo user;
if (_users.TryRemove(Context.ConnectionId, out user))
{
return Clients.All.UserDisconnectedNotification(user);
}
return base.OnDisconnected();
}
修改昵称
public Task ChangeNickname(string newName)
{
UserInfo user;
if (_users.TryGetValue(Context.ConnectionId, out user))
{
var oldName = user.Name;
user.Name = newName;
return Clients.All.NicknameChangedNotification(user, oldName);
}
return null;
}
发送消息给所有人
public Task Send(string message)
{
UserInfo user;
if (_users.TryGetValue(Context.ConnectionId, out user))
{
var msgToSend = string.Format("[{0}]: {1}", user.Name, message);
return Clients.All.Message(msgToSend);
}
return null;
}
前台
用户上线消息
function newUserNotification(user) {
if (getUserElement(user.ConnectionId).length == 0) {
$("#users").append($(getUserListItem(user)));
}
systemMessage("欢迎 " + user.Name + " 用户进入聊天室!");
}
昵称改变消息
function nicknameChangedNotification(user, oldName) {
var userElement = getUserElement(user.ConnectionId);
if (userElement.length > 0) {
userElement.replaceWith($(getUserListItem(user)));
systemMessage(oldName + " 改名为 " + user.Name + ".");
}
if (user.Id === $.connection.hub.id) {
$("#name").text(user.Name);
}
用户下线消息
function userDisconnectedNotification(user) {
var userElement = getUserElement(user.ConnectionId);
if (userElement.length > 0) {
systemMessage(user.Name + " 离开聊天室.");
userElement.remove();
}
}
更新个人信息
function updateMessage(assignedNickname, userList) {
var result = "";
for (var i = 0; i < userList.length; i++) {
var user = userList[i];
result += getUserListItem(user);
}
$("#users").empty();
$("#users").append(result);
$("#username").text(assignedNickname);
$("#user-info").show();
}
用户上线提醒
function welcome(userList) {
var result = "";
for (var i = 0; i < userList.length; i++) {
var user = userList[i];
result += getUserListItem(user);
}
$("#users").empty();
$("#users").append(result);
}
发送消息
function message(message) {
var $panel = $("#chatpanel");
$panel.append("<li class='list-group-item'>" + message + "</li>");
$panel.scrollTop($panel[0].scrollHeight);
}
效果图
结束语
功能较为简单,本来还加了其他的功能,但是由于最近太忙了,打好了一半然后又全部删掉了,后面有时间再慢慢加上去。
本文发布至作业部落