SuperSocket 自定义AppServer、AppSession、CommandBase

1、预期效果如下图。

2、自定义AppServer,代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;namespace ConsoleApp1.Config
{/// <summary>/// 重写AppServer类/// </summary>public class SocketServer:AppServer<SocketSession>{/// <summary>/// 重写启动SocketServer/// </summary>/// <param name="rootConfig"></param>/// <param name="config"></param>/// <returns></returns>protected override bool Setup(IRootConfig rootConfig, IServerConfig config){Console.WriteLine("启动SocketServer");return base.Setup(rootConfig, config);}/// <summary>/// 重写服务已启动/// </summary>protected override void OnStarted(){Console.WriteLine("服务已开始");base.OnStarted();}/// <summary>/// 重写服务已停止/// </summary>protected override void OnStopped(){Console.WriteLine("服务已停止");base.OnStopped();}/// <summary>/// 重写新的连接建立/// </summary>/// <param name="session"></param>protected override void OnNewSessionConnected(SocketSession session){base.OnNewSessionConnected(session);Console.WriteLine("新客户端接入,IP地址为:"+session.RemoteEndPoint.Address.ToString());session.Send("Welcome");}/// <summary>/// 重写客户端连接关闭/// </summary>/// <param name="session"></param>/// <param name="reason"></param>protected override void OnSessionClosed(SocketSession session, CloseReason reason){base.OnSessionClosed(session, reason);Console.WriteLine("客户端断开,IP地址为:" + session.RemoteEndPoint.Address.ToString());}}
}

3、自定义AppSession,代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;namespace ConsoleApp1.Config
{/// <summary>/// 自定义连接类SocketSession/// </summary>public class SocketSession:AppSession<SocketSession>{/// <summary>/// 重写发送/// </summary>/// <param name="message"></param>public override void Send(string message){Console.WriteLine("发送消息:"+message);base.Send(message);}/// <summary>/// 重写客户端建立连接/// </summary>protected override void OnSessionStarted(){Console.WriteLine("客户端IP地址:"+this.LocalEndPoint.Address.ToString());base.OnSessionStarted();}/// <summary>/// 重写客户端断开连接/// </summary>/// <param name="reason"></param>protected override void OnSessionClosed(CloseReason reason){base.OnSessionClosed(reason);}/// <summary>/// 重写未知请求/// </summary>/// <param name="requestInfo"></param>protected override void HandleUnknownRequest(StringRequestInfo requestInfo){Console.WriteLine("遇到位置请求Key:"+requestInfo.Key+"  Body:"+requestInfo.Body);base.HandleUnknownRequest(requestInfo);}/// <summary>/// 重写捕获异常/// </summary>/// <param name="e"></param>protected override void HandleException(Exception e){Console.WriteLine("error:{0}",e.Message);this.Send("error:{0}", e.Message);base.HandleException(e);}}
}

4、自定义CommandBase,代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;namespace ConsoleApp1.Config
{/// <summary>/// 自定义SocketCommand1/// </summary>public class SocketCommand1 : CommandBase<SocketSession, StringRequestInfo>{public override void ExecuteCommand(SocketSession session, StringRequestInfo requestInfo){session.Send("1");}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;namespace ConsoleApp1.Config
{/// <summary>/// 自定义Command1/// </summary>public class SocketCommand2 : CommandBase<SocketSession, StringRequestInfo>{public override void ExecuteCommand(SocketSession session, StringRequestInfo requestInfo){session.Send(string.Format("{0} {1}","1","123"));}}
}

5、主程序Program.cs,代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;//引入命名空间
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketEngine;namespace ConsoleApp1
{class Program{static void Main(string[] args){Console.WriteLine("请输入Start开启服务器");while (!Console.ReadLine().Equals("Start")){Console.WriteLine();continue;}// 初始化Socket服务var bootstrap = BootstrapFactory.CreateBootstrap();if (!bootstrap.Initialize()){Console.WriteLine("初始化失败");Console.ReadKey();return;}var result = bootstrap.Start();Console.WriteLine("服务正在启动: {0}",result);if (result == StartResult.Failed){Console.WriteLine("服务启动失败");Console.ReadKey();return;}Console.WriteLine("服务器启动成功,请输入Stop停止服务器");while (!Console.ReadLine().Equals("Stop")){Console.WriteLine();continue;}//服务停止bootstrap.Stop();Console.WriteLine("服务停止");Console.ReadKey();}/// <summary>/// 客户端连接服务器事件/// </summary>/// <param name="session"></param>private static void AppServer_NewSessionConnected(AppSession session){Console.WriteLine("新客户端已连接IP地址为:" + session.RemoteEndPoint.Address.ToString());session.Send("Welcome");}/// <summary>/// 接收到客户端发送的数据/// </summary>/// <param name="session"></param>/// <param name="requestInfo"></param>private static void AppServer_NewRequestReceived(AppSession session, StringRequestInfo requestInfo){var key = requestInfo.Key;var body = requestInfo.Body;var parameters=requestInfo.Parameters;switch (key){case "1":Console.WriteLine("收到客户端信息1。");session.Send("Received:" + key.ToString());break;default:Console.WriteLine("收到key:"+key.ToString());Console.WriteLine("收到body:" + body.ToString());for (int i = 0; i < parameters.Length; i++){Console.WriteLine("收到parameters:" + parameters[i]);}session.Send("Received Key:" + key.ToString());session.Send("Received Body:" + body.ToString());for (int i = 0; i < parameters.Length; i++){session.Send("Received parameters:" + parameters[i]);}break;}}/// <summary>/// 客户端关闭/// </summary>/// <param name="session"></param>/// <param name="value"></param>private static void AppServer_SessionClosed(AppSession session, CloseReason value){Console.WriteLine("IP地址为:" + session.RemoteEndPoint.Address.ToString()+ "客户端已下线");}}
}

6、App.config配置如下。

<?xml version="1.0" encoding="utf-8" ?>
<configuration><configSections><!--log 日志记录--><section name="log4net" type="System.Configuration.IgnoreSectionHandler"/><!--SocketEngine--><section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/></configSections><!--服务信息描述,在window服务模式下的名称标识--><appSettings><add key="ServiceName" value="ConsoleApp1"/><add key="ServiceDescription" value="bjxingch"/></appSettings><!--SuperSocket服务配置信息 serverType是项目的服务如我自定义的Socketserver--><superSocket><servers><server name="ConsoleApp1"textEncoding="gb2312"serverType="ConsoleApp1.Config.SocketServer,ConsoleApp1"ip="Any"port="2024"maxConnectionNumber="100"/><!--name:实例名称textEncoding:编码方式"gb2312","utf-8" 默认是aciiserverType:需要注意,否则无法启动Serverip:监听ipport:端口号maxConnectionNumber:最大连接数--></servers></superSocket><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /></startup>
</configuration>

7、项目结构如下图。

8、Demo下载路径如下

https://download.csdn.net/download/xingchengaiwei/89311253

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/16559.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

做抖音电商,可以没有货源和经验,但不能没有耐心

我是王路飞。 在抖音做电商这件事&#xff0c;不需要怀疑其可行性。 经过四五年的发展&#xff0c;平台和商家已经证明了抖音电商的前景&#xff0c;它就是我们普通人做抖音最适合的一个渠道。 想在抖音做电商的&#xff0c;再给你们一个经验之谈&#xff0c;你可以没有货源…

linux 查看磁盘使用情况

在Linux系统中&#xff0c;你可以使用以下命令来查看磁盘的情况&#xff1a; 1.df命令&#xff1a;用于显示文件系统的磁盘空间使用情况。 df -h该命令会以人类可读的方式显示文件系统的磁盘空间使用情况&#xff0c;包括文件系统、已用空间、可用空间、已用百分比、挂载点等…

hudi0.13版本clean策略

hudi0.13版本clean策略 在 Apache Hudi 0.13 版本中&#xff0c;清理策略对于数据管理和存储优化起着关键作用。为了确保数据湖的有效利用和性能优化&#xff0c;了解和正确配置清理策略至关重要。以下是 Hudi 0.13 版本的清理策略详细说明及注意事项。 清理策略概述 Hudi 提…

定义字符串报错:ninja: build stopped: subcommand failed.

代码块&#xff1a; char c[] [a,b,c,d,e,\n];报错&#xff1a;ninja: build stopped: subcommand failed.解决&#xff1a; []改成{}

基于 Spring Boot 博客系统开发(十一)

基于 Spring Boot 博客系统开发&#xff08;十一&#xff09; 本系统是简易的个人博客系统开发&#xff0c;为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。&#x1f33f;&#x1f33f;&#x1f33f; 基于 Spring Boot 博客系统开发&#xff08;十&#xff09;&#x…

云上聚智——移动云云服务器进行后端的搭建及部署

什么是移动云 移动云是指将移动设备和云计算技术相结合&#xff0c;为移动应用提供强大的计算和存储能力的服务模式。传统的移动应用通常在本地设备上进行计算和存储&#xff0c;而移动云将这些任务转移到云端进行处理。通过移动云&#xff0c;移动设备可以利用云端的高性能计算…

基于Python+flask+echarts的气象数据采集与分析系统,可实现lstm算法进行预测

背景 基于PythonFlaskEcharts的气象数据采集与分析系统结合了强大的数据处理能力和可视化展示技术&#xff0c;旨在实现对气象数据的实时采集、存储和分析。通过Python编程语言实现数据采集模块&#xff0c;利用Flask框架搭建后端系统&#xff0c;实现数据处理、存储和分析功能…

Python库之selenium的简介、安装、使用方法详细攻略

Python库之selenium的简介、安装、使用方法详细攻略 简介 Selenium是一个强大的自动化测试工具&#xff0c;它支持多种编程语言的绑定&#xff0c;包括Python。Selenium可以用于自动化网页操作&#xff0c;如浏览器界面的测试、数据抓取、自动化表单填写等。它模拟了真实用户…

面试数据库八股文十问十答第七期

面试数据库八股文十问十答第七期 作者&#xff1a;程序员小白条&#xff0c;个人博客 相信看了本文后&#xff0c;对你的面试是有一定帮助的&#xff01;关注专栏后就能收到持续更新&#xff01; ⭐点赞⭐收藏⭐不迷路&#xff01;⭐ 1&#xff09;索引是越多越好吗&#xff…

ORB-SLAM2从理论到代码实现(六):Tracking程序详解(上)

1. Tracking框架 Tracking线程流程框图&#xff1a; 各流程对应的主要函数 2. Tracking整体流程图 上面这张图把Tracking.cc讲的特别明白。 tracking线程在获取图像数据后&#xff0c;会传给函数GrabImageStereo、GrabImageRGBD或GrabImageMonocular进行预处理&#xff0c;这…

【php开发系统性学习】——thinkphp框架的控制器和视图的精简详细的使用

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;开发者-曼亿点 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 曼亿点 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a…

Double 4 VR虚拟情景实训教学系统在法律课堂上的应用

Double 4 VR虚拟情景实训教学系统在法律课堂上的应用&#xff1a; 1. 增强实践性和操作性&#xff1a;虚拟现实技术可以让学生进入模拟的实践环境&#xff0c;操作各种法律事务&#xff0c;从而提高实践能力和操作水平。 2. 提升学习体验&#xff1a;虚拟现实技术能够模拟真实的…

JSON-C库 下载安装

### JSON-C库的下载 您可以通过访问JSON-C的官方GitHub仓库来下载JSON-C库。以下是下载和安装JSON-C库的一般步骤&#xff1a; 1. 打开终端或命令提示符。 2. 输入以下命令来克隆JSON-C的GitHub仓库&#xff1a; git clone https://github.com/json-c/json-c.git …

[Android]联系人-删除修改

界面显示 添加按钮点击&#xff0c;holder.imgDelete.setlog();具体代码 public MyViewHolder onCreateViewHolder(NonNull ViewGroup parent, int viewType) {//映射布局文件&#xff0c;生成相应的组件View v LayoutInflater.from(parent.getContext()).inflate(R.layout.d…

【OceanBase诊断调优】—— 临时文件排查手册

本文介绍临时文件常见的问题及排查方法。 临时文件在不同的 OceanBase 数据库版本上存在不同的使用限制&#xff0c;具体如下&#xff1a; 单个 observer 节点同时写入的最大文件数 OceanBase 数据库 V2.x 及之前版本&#xff0c;默认最大 4000 个&#xff0c;可以通过 _tempo…

胶原蛋白三肽能否深入皮肤?一场关于美丽的科学之旅

在追求美丽的道路上&#xff0c;我们总是对各种护肤成分充满好奇。今天&#xff0c;就让我们一起探讨一个热门话题——胶原蛋白三肽&#xff0c;它究竟能否深入我们的皮肤&#xff0c;为我们带来期待中的美丽改变呢&#xff1f; 首先&#xff0c;我们需要了解胶原蛋白肽是什么…

Cross-Episodic Curriculum for Transformer Agents

我们采用以下六个标准来提供一个全面的框架&#xff0c;用于对机器学习研究&#xff0c;特别是在序列决策和具身智能体背景下的研究进行分类和理解&#xff1a; 学习范式&#xff1a;这个标准区分了算法获取知识的方式。 强化学习 (RL) 是一种试错法&#xff0c;智能体通过与环…

开发心电疾病分类的深度学习模型并部署运行于ARM虚拟硬件平台(AVH)

目录 一、ARM虚拟硬件平台介绍 二、心电疾病分类模型介绍 三、部署流程 3.1 基于百度云平台订阅虚拟硬件镜像 3.2 安装编译相关组件 3.1 数据加载 3.2 模型转换 方式一&#xff1a; tensorflow模型转换为onnx模型&#xff0c;onnx模型转换为TVM模型 方式二&#xff1…

注册表Windows兼容性设置(AppCompatFlags)

属性 - 兼容性 EXE文件属性中有兼容性标签&#xff0c;当有些老版本软件不能正常运行时经常会调整这里的设置。 image.png 上面的所有选项都写在注册表中&#xff0c;其中“更改所有用户的设置”保存在HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\AppC…