• 大小: 43.73MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-06-22
  • 语言: 其他
  • 标签: dotnetty  

资源简介

https://blog.csdn.net/qq_34719168/article/details/88127780,博文项目源代码

资源截图

代码片段和文件信息

using CommonLib;
using DotNetty.Handlers.Timeout;
using DotNetty.Transport.Channels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Client
{
    public class HelloClientHandler : ChannelHandlerAdapter
    {
        public event CommandReceiveEvent MessageReceived;
        public event MessageSendEvent MessageSend;

        public IChannelHandlerContext _Socket { get; set; }

        /// 
        /// 发送数据
        /// 

        /// 
        /// 
        public async void SendData(T obj)
        {
            try
            {
                await _Socket.WriteAndFlushAsync(obj);
            }
            catch (Exception ex) { }
        }

        private void OnMessageReceive(Message msg) => MessageReceived?.Invoke(this new MessageEventArgs(msg));
        private void OnMessageSend(Message msg) => MessageSend?.Invoke(this new MessageEventArgs(msg));

        public override async void UserEventTriggered(IChannelHandlerContext context object evt)
        {
            await Task.Run(() =>
            {
                Console.WriteLine(“客户端循环心跳监测发送: “ + DateTime.Now);
                if (evt is IdleStateEvent eventState)
                {
                    if (eventState.State == IdleState.WriterIdle)
                    {
                        Message ms = new Message { Command = COMMAND.HeartBeat Content = $“biubiu:{DateTime.Now}“ };
                        OnMessageSend(ms);
                    }
                }
            });
        }

        public override async void ChannelActive(IChannelHandlerContext context)
        {
            _Socket = context;

            Console.WriteLine(“我是客户端.“);
            Console.WriteLine($“连接至服务端{context}.“);

            await Task.Run(() =>
            {
                Message ms = new Message { Command = COMMAND.Message Content = $“客户端{(context.Channel.LocalAddress as IPEndPoint).Port}“ };
                OnMessageSend(ms);
            });
        }

        public override async void ChannelRead(IChannelHandlerContext context object message)
        {
            await Task.Run(() =>
            {
                if (message is Message oo)
                {
                    OnMessageReceive(oo);
                }
            });
        }

        public override async void ChannelReadComplete(IChannelHandlerContext context) => await Task.Run(() => { context.Flush(); });

        public override void HandlerAdded(IChannelHandlerContext context)
        {
            Console.WriteLine($“服务端{context}上线.“);
            base.HandlerAdded(context);
        }

        public override void HandlerRemoved(IChannelHandlerContext context)
        {
            Console.WriteLine($“服务端{context}下线.“);
            base.HandlerRemoved(con

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.    416256  2019-03-04 17:37  Lession4\.vs\Lession4\v15\.suo

     文件          0  2019-02-25 19:25  Lession4\.vs\Lession4\v15\Server\sqlite3\db.lock

     文件     733184  2019-03-04 17:21  Lession4\.vs\Lession4\v15\Server\sqlite3\storage.ide

     文件      32768  2019-03-04 11:07  Lession4\.vs\Lession4\v15\Server\sqlite3\storage.ide-shm

     文件    4132392  2019-03-04 17:37  Lession4\.vs\Lession4\v15\Server\sqlite3\storage.ide-wal

     文件       1336  2019-02-25 12:43  Lession4\Client\App.config

     文件      16384  2019-03-04 16:34  Lession4\Client\bin\Debug\Client.exe

     文件      18503  2019-02-25 13:43  Lession4\Client\bin\Debug\Client.exe.config

     文件      40448  2019-03-04 16:34  Lession4\Client\bin\Debug\Client.pdb

     文件       7680  2019-03-04 16:34  Lession4\Client\bin\Debug\CommonLib.dll

     文件       1210  2019-02-25 12:43  Lession4\Client\bin\Debug\CommonLib.dll.config

     文件      32256  2019-03-04 16:34  Lession4\Client\bin\Debug\CommonLib.pdb

     文件     175184  2018-10-08 10:15  Lession4\Client\bin\Debug\DotNetty.Buffers.dll

     文件     130620  2018-10-08 16:55  Lession4\Client\bin\Debug\DotNetty.Buffers.xml

     文件     140392  2018-10-08 10:14  Lession4\Client\bin\Debug\DotNetty.Codecs.dll

     文件      38209  2018-10-08 16:55  Lession4\Client\bin\Debug\DotNetty.Codecs.xml

     文件     153168  2018-10-08 10:14  Lession4\Client\bin\Debug\DotNetty.Common.dll

     文件     146861  2018-10-08 16:55  Lession4\Client\bin\Debug\DotNetty.Common.xml

     文件      52296  2018-10-08 10:14  Lession4\Client\bin\Debug\DotNetty.Handlers.dll

     文件      32387  2018-10-08 16:55  Lession4\Client\bin\Debug\DotNetty.Handlers.xml

     文件     180816  2018-10-08 10:14  Lession4\Client\bin\Debug\DotNetty.Transport.dll

     文件     198320  2018-10-08 16:55  Lession4\Client\bin\Debug\DotNetty.Transport.xml

     文件     279552  2018-01-30 15:19  Lession4\Client\bin\Debug\MessagePack.dll

     文件      32049  2018-01-30 15:19  Lession4\Client\bin\Debug\MessagePack.xml

     文件      35320  2016-11-14 20:41  Lession4\Client\bin\Debug\Microsoft.Extensions.DependencyInjection.Abstractions.dll

     文件      53520  2016-11-14 18:50  Lession4\Client\bin\Debug\Microsoft.Extensions.DependencyInjection.Abstractions.xml

     文件      44032  2017-02-17 17:01  Lession4\Client\bin\Debug\Microsoft.Extensions.Logging.Abstractions.dll

     文件      33625  2017-02-17 13:48  Lession4\Client\bin\Debug\Microsoft.Extensions.Logging.Abstractions.xml

     文件      18432  2017-02-17 17:01  Lession4\Client\bin\Debug\Microsoft.Extensions.Logging.dll

     文件       1652  2017-02-17 13:48  Lession4\Client\bin\Debug\Microsoft.Extensions.Logging.xml

............此处省略5201个文件信息

评论

共有 条评论