1. 程式人生 > >GFF高仿QQ客戶端及伺服器

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

下載下來後執行如下圖:

三、關鍵技術

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 }
View Code

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 }
View Code
 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 }
View Code

 

四、專案結構

 

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
如果發現本文有什麼問題和任何建議,也隨時歡迎交流~