1)为什么使用SuperSocket?

性能高,易上手。有中文文档,我们可以有更多的时间用在业务逻辑上,SuperSocket有效的利用自己的协议解决粘包

2)SuperSocket的协议内容?

命令 body  列如:TestCommand 1 2

3)怎样在Net下使用 SuperSocket?

1)新建项目命名为SuperSocketWeb

2)引用程序集-》NuGet工具搜索安装SuperSocket,SuperSocket.Engine两个组件

3)下载测试工具SocketTool 官网demo存在

4)新建链接类 TestSession,每个链接为一个Session

using SuperSocket.SocketBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Protocol; namespace SuperSocket
{
public class TestSession : AppSession<TestSession>
{
public int CustomID { get; set; }
public string CustomName { get; set; }
/// <summary>
/// 用户连接会话
/// </summary>
protected override void OnSessionStarted()
{
Console.WriteLine("新的用户请求");
base.OnSessionStarted();
} /// <summary>
/// 未知的用户请求命令
/// </summary>
/// <param name="requestInfo"></param>
protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
{
base.HandleUnknownRequest(requestInfo);
}
/// <summary>
/// 会话关闭
/// </summary>
/// <param name="reason"></param>
protected override void OnSessionClosed(CloseReason reason)
{
base.OnSessionClosed(reason);
}
}
}

5)新建命令类TestCommand

using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SuperSocket
{
public class TestCommand : CommandBase<TestSession, StringRequestInfo>
{
/// <summary>
/// 命令处理类
/// </summary>
/// <param name="session"></param>
/// <param name="requestInfo"></param>
public override void ExecuteCommand(TestSession session, StringRequestInfo requestInfo)
{
session.CustomID = new Random().Next(, );
session.CustomName = "hello word"; var key = requestInfo.Key;
var param = requestInfo.Parameters;
var body = requestInfo.Body;
//返回随机数session.Send(session.CustomID.ToString());
//返回
 session.Send(body);
       } 
}
}

6)新建服务类 TestServer

using SuperSocket.SocketBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SuperSocket
{
public class TestServer : AppServer<TestSession>
{
}
}

7)开启tcp/ip监听

在Global->Application_Start中开启服务监听,最好使用日志记录监听的开启情况

           var appServer = new TestServer();
if (!appServer.Setup()) //开启的监听端口
{
return;
}
if (!appServer.Start())
{
return;
}

4)测试demo

打开SocketTool,链接服务器ip+端口号。注意在命令结束要按回车哦,红色框的位置

返回表示成功,你也可以自定义自己想要的返回值

源码地址:https://github.com/MrsongJl/SuperSocketWeb

原文地址:http://www.cnblogs.com/songjl/p/6907514.html

最新文章

  1. 相关css 细节处理 neat.css
  2. Objective-C(NSString、BOOL、多文件开发)
  3. Linux shell入门基础(三)
  4. XShell连接CentOS 7.2显示中文乱码问题的解决方法
  5. Java socket字节流传输的示例
  6. 用OC和Swift一起说说二叉树
  7. P3146 [USACO16OPEN]248 &amp; P3147 [USACO16OPEN]262144
  8. #pragma warning(disable 4786)
  9. http错误代码含义
  10. java基础篇---异常处理
  11. 计算概论(A)/基础编程练习1(8题)/4:求一元二次方程的根
  12. js replaceChild
  13. css冲突2 要关闭的css在项目代码以外,但是是通过&lt;link&gt;标签引入的css(例如bootstrap):解决方法,在APP.css中使用全局样式
  14. zookeeper的c API 单线程与多线程问题 cli_st和cli_mt
  15. 9.spring:事务管理(下):声明式事务管理
  16. HTTP缓存机制--客户端缓存(转)
  17. python基础之反射内置方法元类
  18. 自己搭建MVC时遇到的一些问题及解决办法
  19. Selenium2+python自动化68-html报告乱码问题【转载】
  20. Vue creatElement

热门文章

  1. 详解Python的*args和 **kwargs
  2. PHP小函数集-篇一
  3. html title属性
  4. TCP协议中的三次握手和四次挥手(图解)【转载】
  5. bzoj 3824: [Usaco2014 Dec]Guard Mark【状压dp】
  6. 洛谷P2384 最短路(dijkstra解法)
  7. ROS学习笔记十:URDF详解
  8. [ZPG TEST 115] 字符串【归类思想】
  9. How many Fibs? POJ - 2413
  10. 【转载】WebConfigurationManager和ConfigurationManager