zoukankan      html  css  js  c++  java
  • 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  * 联系人邮箱: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 }
    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  * 联系人邮箱: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 }
    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
    如果发现本文有什么问题和任何建议,也随时欢迎交流~

     
  • 相关阅读:
    归并排序python实现源码
    华为手机使用应用沙盒动态修改基带参数
    三星5.0以上机器最简单激活Xposed框架的经验
    python正常时间和unix时间戳时间的相互转换源码
    三星5.0以上设备最完美激活XPOSED框架的经验
    华为6.0系统设备最完美激活Xposed框架的经验
    C语言经典算法
    水果店小程序推广步骤笔记
    三星手机使用应用沙盒一键修改路由mac数据
    python通过装饰器检查函数参数的数据类型的代码
  • 原文地址:https://www.cnblogs.com/yswenli/p/6274526.html
Copyright © 2011-2022 走看看