天天看點

GFF高仿QQ用戶端及伺服器

一、GFF簡介

GFF是仿QQ界面,通信基于SAEA.MessageSocket、SAEA.Http、SAEA.MVC實作包含用戶端和伺服器的程式,源碼完全公開,項目源碼位址:

https://github.com/yswenli/GFF

 ,大家可以去我的github了解,歡迎follow,star與fork。

GFF消息采用高性能基于IOCP模型的tcp實作,檔案采用http實作,代碼簡潔,一目了然,非常适合想要了解聊天通信關鍵技術的朋友。

二、運作界面

GFF已實作了消息、表情、圖檔、截圖等關鍵功能,已編譯的綠色版

https://github.com/yswenli/GFF/releases

下載下傳下來後運作如下圖:

GFF高仿QQ用戶端及伺服器
GFF高仿QQ用戶端及伺服器
GFF高仿QQ用戶端及伺服器

三、關鍵技術

1.界面采用了CSkin的一套QQ皮膚,更多的可以百度一下CSkin相關的資料,或者檢視GFF的源碼。

2.用戶端通信使用了SAEA.MessageSocket的封裝類MessageHelper,代碼非常簡潔,不到100行代碼,輕松實作通信。

1 /*****************************************************************************************************
  2  * 本代碼版權歸Wenli所有,All Rights Reserved (C) 2015-2016
  3  *****************************************************************************************************
  4  * 所屬域:WENLI-PC
  5  * 登入使用者:Administrator
  6  * CLR版本:4.0.30319.17929
  7  * 唯一辨別:20da4241-0bdc-4a06-8793-6d0889c31f95
  8  * 機器名稱:WENLI-PC
  9  * 聯系人郵箱:[email protected]
 10  *****************************************************************************************************
 11  * 命名空間:MCITest
 12 
 13 
 14  * 建立年份:2015
 15  * 建立時間:2015-12-02 11:15:24
 16  * 建立人:Wenli
 17  * 建立說明:
 18  *****************************************************************************************************/
 19 
 20 using GFF.Component.Config;
 21 using SAEA.MessageSocket;
 22 using System;
 23 using System.Net;
 24 using System.Text;
 25 using System.Threading.Tasks;
 26 
 27 namespace GFFClient
 28 {
 29     public class MessageHelper
 30     {
 31         public delegate void OnErrorHander(Exception ex, string msg);
 32 
 33         public delegate void OnMessageHanndle(string channelID, string msg);
 34 
 35         private static readonly object lockObj = new object();
 36 
 37         private string _channelID;
 38 
 39         private string _userName;
 40 
 41         ClientConfig clientConfig;
 42 
 43         public MessageHelper()
 44         {
 45             clientConfig = ClientConfig.Instance();
 46         }
 47 
 48         /// <summary>
 49         ///     Tcp用戶端
 50         /// </summary>
 51         public MessageClient Client { get; private set; }
 52 
 53         public void Start(string userName, string channelID)
 54         {
 55             _userName = userName;
 56             _channelID = channelID;
 57 
 58             Client = new MessageClient(10240, clientConfig.IP, clientConfig.Port);
 59             Client.OnChannelMessage += Client_OnChannelMessage;
 60             Client.OnPrivateMessage += Client_OnPrivateMessage;
 61             Client.OnError += Client_OnError;
 62             Client.Connect();
 63             Client.Login();
 64             Client.Subscribe(channelID);
 65         }
 66 
 67         private void Client_OnError(string ID, Exception ex)
 68         {
 69             OnError.Invoke(ex, ex.Message);
 70         }
 71 
 72         private void Client_OnChannelMessage(SAEA.MessageSocket.Model.Business.ChannelMessage msg)
 73         {
 74             OnMessage?.Invoke(_channelID, msg.Content);
 75         }
 76 
 77         private void Client_OnPrivateMessage(SAEA.MessageSocket.Model.Business.PrivateMessage msg)
 78         {
 79             OnMessage?.Invoke(msg.Receiver, msg.Content);
 80         }
 81 
 82         public void Publish(string channelID, string value)
 83         {
 84             Client.SendChannelMsg(channelID, value);
 85         }
 86 
 87 
 88         public void SendFile(string channelID, string fileName, Action<string> callBack)
 89         {
 90             HttpSendFileAsync(fileName, url => { callBack?.Invoke(url); });
 91         }
 92 
 93 
 94         public void HttpSendFileAsync(string fileName, Action<string> callBack)
 95         {
 96             Task.Run(() =>
 97             {
 98                 using (WebClient webClient = new WebClient())
 99                 {
100                     var url = clientConfig.Url + Encoding.UTF8.GetString(webClient.UploadFile(clientConfig.Url + "Upload", fileName));
101                     callBack.Invoke(url);
102                 }
103             });
104         }
105 
106         public void Stop()
107         {
108             try
109             {
110                 Client.Dispose();
111             }
112             catch { }
113         }
114 
115         public event OnMessageHanndle OnMessage;
116 
117         public event OnErrorHander OnError;
118     }
119 }      

3.服務端使用SAEA.MessageSocket實作服務端消息處理邏輯、SAEA.MVC實作檔案處理邏輯,有興趣的朋友可以在此基礎上實作更多實際業務。

1 /*****************************************************************************************************
 2  * 本代碼版權歸Wenli所有,All Rights Reserved (C) 2015-2016
 3  *****************************************************************************************************
 4  * 所屬域:WENLI-PC
 5  * 登入使用者:Administrator
 6  * CLR版本:4.0.30319.17929
 7  * 唯一辨別:20da4241-0bdc-4a06-8793-6d0889c31f95
 8  * 機器名稱:WENLI-PC
 9  * 聯系人郵箱:[email protected]
10  *****************************************************************************************************
11  * 命名空間:MCITest
12 
13 
14  * 建立年份:2015
15  * 建立時間:2015-12-02 11:15:24
16  * 建立人:Wenli
17  * 建立說明:
18  *****************************************************************************************************/
19 
20 using GFF.Component.Config;
21 using GFF.Helper;
22 using SAEA.MessageSocket;
23 using SAEA.MVC;
24 using SAEA.Sockets.Interface;
25 using System;
26 
27 namespace GFFServer
28 {
29     internal class Program
30     {
31         private static MessageServer messageServer;
32 
33         private static SAEAMvcApplication mvcApplication;
34 
35         private static void Main(string[] args)
36         {
37             Console.Title = "GFFServer";
38 
39 
40             ConsoleHelper.WriteLine("正在初始化消息伺服器...", ConsoleColor.Green);
41             messageServer = new MessageServer();
42             messageServer.OnAccepted += Server_OnAccepted;
43             messageServer.OnError += Server_OnError;
44             messageServer.OnDisconnected += Server_OnDisconnected;
45             ConsoleHelper.WriteLine("消息伺服器初始化完畢...", ConsoleColor.Green);
46 
47 
48 
49             ConsoleHelper.WriteLine("正在初始化檔案伺服器...", ConsoleColor.DarkYellow);
50             var filePort = ServerConfig.Instance().FilePort;
51             mvcApplication = new SAEAMvcApplication(port: filePort);
52             mvcApplication.SetDefault("File", "Test");
53             ConsoleHelper.WriteLine("檔案伺服器初始化完畢,http://127.0.0.1:" + filePort + "/...", ConsoleColor.DarkYellow);
54 
55 
56 
57             ConsoleHelper.WriteLine("正在啟動消息伺服器...", ConsoleColor.Green);
58             messageServer.Start();
59             ConsoleHelper.WriteLine("消息伺服器啟動完畢...", ConsoleColor.Green);
60 
61 
62 
63             ConsoleHelper.WriteLine("正在啟動檔案伺服器...", ConsoleColor.DarkYellow);
64             mvcApplication.Start();
65             ConsoleHelper.WriteLine("檔案伺服器啟動完畢...", ConsoleColor.DarkYellow);
66 
67 
68 
69             ConsoleHelper.WriteLine("點選回車,結束服務");
70             Console.ReadLine();
71         }
72 
73         private static void Server_OnDisconnected(string ID, Exception ex)
74         {
75             ConsoleHelper.WriteInfo(string.Format("用戶端{0}已斷開連接配接,目前連接配接數共記:{1}", ID, messageServer.ClientCounts));
76         }
77 
78         private static void Server_OnError(string ID, Exception ex)
79         {
80             ConsoleHelper.WriteErr(ex);
81         }
82 
83         private static void Server_OnAccepted(IUserToken userToken)
84         {
85             ConsoleHelper.WriteInfo(string.Format("用戶端{0}已連接配接,目前連接配接數共記:{1}", userToken.ID, messageServer.ClientCounts));
86         }
87     }
88 }      
1 using SAEA.MVC;
 2 using System.IO;
 3 
 4 namespace GFFServer.Controllers
 5 {
 6     /// <summary>
 7     /// 檔案處理
 8     /// </summary>
 9     public class FileController : Controller
10     {
11         public ActionResult Test()
12         {
13             return Content("GFF File Server");
14         }
15 
16         [HttpPost]
17         public ActionResult Upload()
18         {
19             var postFile = HttpContext.Request.PostFiles[0];
20             var filePath = HttpContext.Server.MapPath("/Files");
21             if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
22             filePath = Path.Combine(filePath, postFile.FileName);
23             System.IO.File.WriteAllBytes(filePath, postFile.Data);
24             return Content("Download?fileName=" + postFile.FileName);
25         }
26 
27 
28         public ActionResult Download(string fileName)
29         {
30             var filePath = Path.Combine(HttpContext.Server.MapPath("/Files"), fileName);
31             return File(filePath);
32         }
33     }
34 }      

四、項目結構

GFF高仿QQ用戶端及伺服器

1.GFF.Component 封裝客戶的截圖、聊天展現、表情、配置等

2.GFF.Helper 封裝了GFF項目中需要使用的一些工具類

3.GFF.Model 是GFF中使用到類、接口、枚舉等

4.GFFClient 是GFF的用戶端主體項目

5.GFFServer 是GFF的服務端主體項目

轉載請标明本文來源: https://www.cnblogs.com/yswenli/p/6274526.html 更多内容歡迎我的的github: https://github.com/GFF 如果發現本文有什麼問題和任何建議,也随時歡迎交流~

感謝您的閱讀,如果您對我的部落格所講述的内容有興趣,請繼續關注我的後續部落格,我是yswenli 。

繼續閱讀