ASP.NET+SignalR聊天室

源码下载:https://pan.baidu.com/s/1l_XNjpGrrhrx9fz0MXQHLQ  提取码: pr8q

Nuget里搜索安装SignalR,非ASP.NET CORE版本。

1.增加Startup类

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(CHATROOM.Startup))]

namespace CHATROOM
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
            app.MapSignalR();
        }
    }
}
View Code

2.增加继承自Hub的类

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace CHATROOM
{
    [HubName("CHATROOM")]
    public class BaseHub : Hub
    {
        public static Dictionary<string, string> onlineList = new Dictionary<string, string>();
        public override Task OnConnected()
        {
            string username = Context.QueryString["username"].ToString();
            onlineList.Add(Context.ConnectionId, username);
            Clients.All.onLine(username, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), onlineList.ToList());
            return base.OnConnected();
        }
        public override Task OnDisconnected(bool stopCalled)
        {
            string username = Context.QueryString["username"].ToString();
            onlineList.Remove(Context.ConnectionId);
            Clients.All.offLine(username, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), onlineList.ToList());
            return base.OnDisconnected(stopCalled);
        }
        public void Send(string username, string message, string sendto)
        {
            message = HttpUtility.HtmlEncode(message).Replace("\r\n", "
").Replace("\n", "
"); if (string.IsNullOrWhiteSpace(sendto)) { Clients.All.receiveMessage(username, message, sendto, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } else { List<string> connids = sendto.Split(',').ToList(); var st = string.Join(",", onlineList.Where(o => connids.Contains(o.Key)).Select(o => o.Value)); Clients.Caller.receiveMessage(username, message, st, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); Clients.Clients(connids).receiveMessage(username, message, st, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } } } }
View Code

3.前端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="CHATROOM.index" %>



"http://www.w3.org/1999/xhtml">
"server">
"Content-Type" content="text/html; charset=utf-8"/>
    
    
    
    
    


    
"form1" runat="server">
class="divLeft">
class="msgBox" id="msgBox">
class="msgOper">
用户名: "lbUsername" runat="server" Text=""> class="lbTo">发送给: "lbSendtoUser">
"button" class="btnSend" id="btnSend" value="发 送"/> "button" class="btnClear" id="btnClear" value="清 屏"/> "btnQuit" runat="server" Text="退 出" OnClick="btnQuit_Click" />
class="divMid">
class="spliter">
class="divRight">
class="userList">
    "olUserlist">
View Code

4.效果图

ASP.NET+SignalR聊天室_第1张图片

 

 ASP.NET+SignalR聊天室_第2张图片

 

 ASP.NET+SignalR聊天室_第3张图片

 

你可能感兴趣的:(ASP.NET+SignalR聊天室)