GFF是仿QQ界面,通訊基於SAEA.MessageSocket、SAEA.Http、SAEA.MVC實現包含客戶端和服務器的程序,源碼徹底公開,項目源碼地址:https://github.com/yswenli/GFF ,你們能夠去個人github瞭解,歡迎follow,star與fork。html
GFF消息採用高性能基於IOCP模型的tcp實現,文件採用http實現,代碼簡潔,一目瞭然,很是適合想要了解聊天通訊關鍵技術的朋友。git
GFF已實現了消息、表情、圖片、截圖等關鍵功能,已編譯的綠色版https://github.com/yswenli/GFF/releases下載下來後運行以下圖:github
1.界面採用了CSkin的一套QQ皮膚,更多的能夠百度一下CSkin相關的資料,或者查看GFF的源碼。web
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 * 聯繫人郵箱:wenguoli_520@qq.com 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實現文件處理邏輯,有興趣的朋友能夠在此基礎上實現更多實際業務。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 * 聯繫人郵箱:wenguoli_520@qq.com 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 }
1.GFF.Component 封裝客戶的截圖、聊天展示、表情、配置等tcp
2.GFF.Helper 封裝了GFF項目中須要使用的一些工具類ide
3.GFF.Model 是GFF中使用到類、接口、枚舉等工具
4.GFFClient 是GFF的客戶端主體項目post
5.GFFServer 是GFF的服務端主體項目
轉載請標明本文來源:http://www.javashuo.com/article/p-wjvuzxjl-x.html
更多內容歡迎個人的github:https://github.com/GFF若是發現本文有什麼問題和任何建議,也隨時歡迎交流~