ChatUserList
14 | {
15 | get
16 | {
17 | return _chatUserList;
18 | }
19 | set
20 | {
21 | _chatUserList = value;
22 | }
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Models/ChatUser.cs:
--------------------------------------------------------------------------------
1 | namespace Dorisoy.ChatServer.Models
2 | {
3 | public class ChatUser
4 | {
5 | public string ConnectionId { get; set; }
6 | public string ChatUsername { get; set; }
7 | public DateTime ActionTime { get; set; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Error.cshtml:
--------------------------------------------------------------------------------
1 | @page
2 | @model ErrorModel
3 | @{
4 | ViewData["Title"] = "Error";
5 | }
6 |
7 | Error.
8 | An error occurred while processing your request.
9 |
10 | @if (Model.ShowRequestId)
11 | {
12 |
13 | Request ID: @Model.RequestId
14 |
15 | }
16 |
17 | Development Mode
18 |
19 | Swapping to the Development environment displays detailed information about the error that occurred.
20 |
21 |
22 | The Development environment shouldn't be enabled for deployed applications.
23 | It can result in displaying sensitive information from exceptions to end users.
24 | For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development
25 | and restarting the app.
26 |
27 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Error.cshtml.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using Microsoft.AspNetCore.Mvc.RazorPages;
3 | using System.Diagnostics;
4 |
5 | namespace WebApplication2.Pages
6 | {
7 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8 | [IgnoreAntiforgeryToken]
9 | public class ErrorModel : PageModel
10 | {
11 | public string? RequestId { get; set; }
12 |
13 | public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14 |
15 | private readonly ILogger _logger;
16 |
17 | public ErrorModel(ILogger logger)
18 | {
19 | _logger = logger;
20 | }
21 |
22 | public void OnGet()
23 | {
24 | RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Index.cshtml:
--------------------------------------------------------------------------------
1 | @page
2 | @model IndexModel
3 | @{
4 | ViewData["Title"] = "Dorisoy.Chat Client";
5 | }
6 |
7 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Index.cshtml.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using Microsoft.AspNetCore.Mvc.RazorPages;
3 |
4 | namespace Dorisoy.ChatServer.Pages
5 | {
6 | public class IndexModel : PageModel
7 | {
8 | private readonly ILogger _logger;
9 |
10 | public IndexModel(ILogger logger)
11 | {
12 | _logger = logger;
13 | }
14 |
15 | public void OnGet()
16 | {
17 |
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Privacy.cshtml:
--------------------------------------------------------------------------------
1 | @page
2 | @model PrivacyModel
3 | @{
4 | ViewData["Title"] = "Privacy Policy";
5 | }
6 | @ViewData["Title"]
7 |
8 | Use this page to detail your site's privacy policy.
9 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Privacy.cshtml.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using Microsoft.AspNetCore.Mvc.RazorPages;
3 |
4 | namespace WebApplication2.Pages
5 | {
6 | public class PrivacyModel : PageModel
7 | {
8 | private readonly ILogger _logger;
9 |
10 | public PrivacyModel(ILogger logger)
11 | {
12 | _logger = logger;
13 | }
14 |
15 | public void OnGet()
16 | {
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
13 |
14 |
15 | @RenderBody()
16 |
17 |
18 |
19 |
20 | @await RenderSectionAsync("Scripts", required: false)
21 |
22 |
23 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Shared/_Layout.cshtml.css:
--------------------------------------------------------------------------------
1 | /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2 | for details on configuring this project to bundle and minify static web assets. */
3 |
4 | a.navbar-brand {
5 | white-space: normal;
6 | text-align: center;
7 | word-break: break-all;
8 | }
9 |
10 | a {
11 | color: #0077cc;
12 | }
13 |
14 | .btn-primary {
15 | color: #fff;
16 | background-color: #1b6ec2;
17 | border-color: #1861ac;
18 | }
19 |
20 | .nav-pills .nav-link.active, .nav-pills .show > .nav-link {
21 | color: #fff;
22 | background-color: #1b6ec2;
23 | border-color: #1861ac;
24 | }
25 |
26 | .border-top {
27 | border-top: 1px solid #e5e5e5;
28 | }
29 | .border-bottom {
30 | border-bottom: 1px solid #e5e5e5;
31 | }
32 |
33 | .box-shadow {
34 | box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
35 | }
36 |
37 | button.accept-policy {
38 | font-size: 1rem;
39 | line-height: inherit;
40 | }
41 |
42 | .footer {
43 | position: absolute;
44 | bottom: 0;
45 | width: 100%;
46 | white-space: nowrap;
47 | line-height: 60px;
48 | }
49 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using Dorisoy.ChatServer
2 | @namespace Dorisoy.ChatServer.Pages
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Pages/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Program.cs:
--------------------------------------------------------------------------------
1 | using Dorisoy.ChatServer.Hubs;
2 |
3 | var builder = WebApplication.CreateBuilder(args);
4 |
5 | // Add services to the container.
6 | builder.Services.AddRazorPages();
7 | builder.Services.AddSignalR();
8 |
9 | var app = builder.Build();
10 |
11 | // Configure the HTTP request pipeline.
12 | if (!app.Environment.IsDevelopment())
13 | {
14 | app.UseExceptionHandler("/Error");
15 | // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
16 | app.UseHsts();
17 | }
18 |
19 | //app.UseHttpsRedirection();
20 | app.UseStaticFiles();
21 |
22 | app.UseRouting();
23 |
24 | app.UseAuthorization();
25 |
26 | app.MapRazorPages();
27 |
28 | app.MapControllerRoute(
29 | name: "default",
30 | pattern: "{controller=Home}/{action=Index}/{id?}");
31 |
32 | app.MapHub("/chatHub");
33 |
34 | app.Run();
35 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:20851",
7 | "sslPort": 44342
8 | }
9 | },
10 | "profiles": {
11 | "http": {
12 | "commandName": "Project",
13 | "dotnetRunMessages": true,
14 | "launchBrowser": true,
15 | "applicationUrl": "http://192.168.0.3:5067",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "https": {
21 | "commandName": "Project",
22 | "dotnetRunMessages": true,
23 | "launchBrowser": true,
24 | "applicationUrl": "https://localhost:7268;http://localhost:5067",
25 | "environmentVariables": {
26 | "ASPNETCORE_ENVIRONMENT": "Development"
27 | }
28 | },
29 | "IIS Express": {
30 | "commandName": "IISExpress",
31 | "launchBrowser": true,
32 | "environmentVariables": {
33 | "ASPNETCORE_ENVIRONMENT": "Development"
34 | }
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "DetailedErrors": true,
3 | "Logging": {
4 | "LogLevel": {
5 | "Default": "Information",
6 | "Microsoft.AspNetCore": "Warning"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Dorisoy.ChatServer/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Warning"
6 | }
7 | },
8 | "AllowedHosts": "*"
9 | }
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Dorisoy.ChatApp
2 |
3 | 基于 .NET MAUI 开发的完整聊天应用程序。它遵循 MVVM 模式。该项目包括使用 SignalR 的 C# 服务器端 chathub 实现。您可以轻松地将此代码集成到现有或新项目中。
4 |
5 | ## 解决方案包含两个项目:
6 |
7 | ChatServer(.NET SignalR 聊天中心服务器)
8 | ChatApp(.NET MAUI 项目)
9 |
10 |
11 | ## 如何运行应用程序
12 |
13 | 步骤 1: 从 Visual Studio 运行“ChatServer”项目(它是一个 SignalR 聊天中心服务器)并获取服务器的 IP 地址。
14 |
15 | 步骤 2: 在项目 ChatApp/Helpers/ChatHelper.cs 文件下名为“GetInstance”的函数中设置 IP 地址
16 |
17 | 注意:如果您在本地主机中运行“ChatServer”,则可以通过 conveyor 插件获取 ip。[https://keyoti.com/products/conveyor/index.html] 安装此 visual studio 插件。然后运行“ChatServer”项目。现在单击 conveyor 提供的 IP 地址。
18 |
19 | ## 应用程序包包含:
20 |
21 | 适用于 Android、iOS 和 Windows 的 .NET MAUI 应用程序
22 |
23 | 采用 MVVM 模式构建
24 |
25 | 使用 SignalR 的服务器端实现
26 |
27 | 定义良好的 xaml 和 c# 代码
28 |
29 | 漂亮的 UI 设计
30 |
31 | 注册页面
32 |
33 | 登录页面
34 |
35 | 聊天用户列表页面
36 |
37 | 消息页面
38 |
39 | 打字指示器
40 |
41 | 在线/离线状态
42 |
43 | 完整的源代码
44 |
45 |
46 | ## 要求:
47 |
48 | Visual Studio 2022
49 |
50 | .NET >= 6
51 |
52 | ## 快速入门:
53 |
54 | ### Visual Studio 解决方案包含两个项目:
55 |
56 | 聊天应用程序
57 | 聊天服务器
58 |
59 | ### ChatApp项目实现了所有客户端功能:
60 |
61 | #### 登录页面
62 |
63 | 在验证用户身份后,您必须调用ChatHub 服务器的 Connect函数,然后重定向到 ChatList 页面。
64 |
65 | #### 聊天列表页面
66 |
67 | 在此页面上,您必须从 ChatServer 调用已连接用户列表。获取已连接用户列表后,您必须将其呈现到列表视图中。要发起聊天,您必须点击该项目以导航聊天页面。
68 |
69 | #### 聊天页面
70 |
71 | #### 您可以在此处向已连接的用户发送消息。此处实现了两个重要功能。
72 |
73 | . 调用“ReceiveMessage”从对方获取消息。收到消息后,您必须以良好的方式呈现到列表视图。
74 | . 调用“SendMessage”将消息发送给您的聊天用户。此处您必须传递必要的参数,如连接 ID、用户 ID、消息等。
75 |
76 | #### ChatServer项目包含ChatHub.cs文件,其中实现了以下服务器端功能:
77 |
78 | 发送消息:在这个函数中,你可以发送消息给其他人。你必须传递接收用户ID和消息。
79 |
80 | 打字:此函数返回聊天用户的输入指示。这里您必须传递连接 ID 和您的姓名。
81 |
82 | 连接异步:当两个用户之间建立连接时,将调用此函数。在这里,您可以根据需要添加自定义实现。
83 |
84 | 断开连接时异步:当两个用户之间的连接断开时,将调用此函数。在这里,您可以根据需要添加自定义实现。
85 |
86 | 获取已连接用户:在此功能中您可以获取所有已连接的用户。
87 |
88 | 断开:要断开会话,您可以调用此函数。
89 |
--------------------------------------------------------------------------------