SuperSocket教程八:通过服务器计划将数据推送到客户端

17 篇文章 13 订阅

前面我们知道了如何使用客户端去连接服务端
然后给服务端发送消息
根据消息的关键字服务端进行返回等等
但是那都是来自客户端的请求
我们服务端如何去主动给客户端发消息呢
这些逻辑到哪里去写呢
这里我写了一份代码供大家参考

官方文档说了
建议session的逻辑写在session相关的类里面
建议server的写在server的类里面
那我们按它的来
当服务端开启的时候
我准备定时去给我的客户端发送消息
既然是开始的时候去发送 那么我们是需要去开启一个线程来做这件事情的
那代码该卸载哪里 当然是OnStarted()这里

using Socket.appReceive;
using Socket.appSession;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Socket.appServer
{
    public class MyAppServer : AppServer<MyAppSession>
    {
        protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
        {
            return base.Setup(rootConfig, config);
        }

        
        protected override void OnStarted()
        {
            Console.WriteLine(this.Name+"服务器已启动");
            this.NewRequestReceived += new RequestHandler<MyAppSession, StringRequestInfo>(appServer_NewRequestReceived);
            Task.Factory.StartNew(()=> {
                while (true) {
                    Thread.Sleep(5000);
                    if (this.GetAllSessions() != null)
                    {
                        foreach (var item in this.GetAllSessions())
                        {
                            item.Send("hello");
                        }
                    }
                    else {
                        Console.WriteLine("无客户端连接");
                    }
                    
                    
                }
               
            });
        }


        /// <summary>
        /// 对接收的数据进行处理
        /// </summary>
        /// <param name="session"></param>
        /// <param name="requestInfo"></param>
        private  void appServer_NewRequestReceived(MyAppSession session, StringRequestInfo requestInfo)
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.Green;
                int flag = 0;
                switch (requestInfo.Key.ToUpper())
                {
                    case ("ECHO"):
                        if (requestInfo.Body.Contains("hello"))
                        {
                            session.Send("hi my name is Server");
                            flag++;
                        }
                        else
                        {
                            session.Send("ninainaider");
                            flag++;
                        }
                        break;

                    case ("ADD"):
                        new ADD().ExecuteCommand(session, requestInfo);
                        flag++;
                        break;

                    case ("MULT"):
                        new MULT().ExecuteCommand(session, requestInfo);
                        flag++;
                        break;
                }
                if (flag == 0)
                {
                    Console.WriteLine("信息来自:"+ session.RemoteEndPoint +"==>"+ requestInfo.Key + requestInfo.Body);
                }
               

            }
            catch (Exception)
            {

                Console.WriteLine("异常");
            }

        }
       

        protected override void OnStopped()
        {
            
            base.OnStopped();
        }



    }
}

代码如上
在这里插入图片描述
每隔五秒去向所有客户端发一次hello
启动程序
开启两个网络调试助手客户端

在这里插入图片描述
可以看到每隔五秒会接收到服务端的消息
我们可以把这种情况运用到很多场景
比如采集器的一些信息收集
定时读取某些设备的信息等等

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值