由于这段时间在找房子,所以耽误了一段时间。前几讲把一些基础的东西稍微介绍了下,这一讲就简单介绍一个小例子,大致功能是实现在线聊天,并显示在线用户、用户上下线实时提醒。这个例子并不复杂只是把之前介绍联系起来。
在这里为了简单起见,暂时不涉及到数据库的操作。
用户信息类UserInfo
public class UserInfo
{
/// <summary>
/// ConnectionId
/// </summary>
public string ConnectionId { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 上线时间
/// </summary>
public DateTime ConnectedAt { get; set; }
/// <summary>
/// 在线标志
/// </summary>
public bool IsOnline { get; set; }
}
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);
}
功能较为简单,本来还加了其他的功能,但是由于最近太忙了,打好了一半然后又全部删掉了,后面有时间再慢慢加上去。
源码下载
本文发布至 作业部落
SignalR Programming in Microsoft ASP.NET pdf 下载