-
C#教程之C#教程之WebSocket
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
System.Net.WebSockets.WebSocket
需要.NET 4.5,IIS8以上,Windows Server2008R2自带的IIS不支持,Windows8及Server2012以上自带的IIS可以
1、实现IHttpHandler用于处理webSocket请求
using System; using System.Net.WebSockets; using System.Threading.Tasks; using System.Web; using System.Web.WebSockets; namespace WebSocketTest { public class MyWebSocketHandler : IHttpHandler { /// <summary> /// 设置实例不可以重复使用 /// </summary> public bool IsReusable { get { return false; } } /// <summary> /// 判断是否是WebSocket请求 /// 是交由handlerWebSocketAsync方法处理 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { if (context.IsWebSocketRequest) { context.AcceptWebSocketRequest(handlerWebSocketAsync); } } private async Task handlerWebSocketAsync(AspNetWebSocketContext context) { WebSocket webSocket = context.WebSocket; await webSocket.SendMsgAsync("连接成功"); while (true) { if (webSocket.State == WebSocketState.Open) { string msg = await webSocket.ReceiveMsgAsync(); webSocket.SendMsgAsync(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + @",Receive:" + msg); } } } } }
webSocket.ReceiveMsgAsync 和 webSocket.SendMsgAsync 是我自己封装的两个扩展方法,用于字符串和byte[]转换
using System; using System.Net.WebSockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace WebSocketTest { public static class WebSocketHelper { /// <summary> /// 发送消息 /// </summary> /// <param name="webSocket"></param> /// <param name="msg"></param> /// <returns></returns> public static async Task SendMsgAsync(this WebSocket webSocket, string msg) { byte[] bytesMsg = Encoding.UTF8.GetBytes(msg); ArraySegment<byte> segment = new ArraySegment<byte>(bytesMsg); await webSocket.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None); } /// <summary> /// 接收消息 /// </summary> /// <param name="webSocket"></param> /// <returns></returns> public static async Task<string> ReceiveMsgAsync(this WebSocket webSocket) { ArraySegment<byte> receiveSegment = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(receiveSegment, CancellationToken.None); string clientMsg = Encoding.UTF8.GetString(receiveSegment.Array, 0, receiveResult.Count); return clientMsg; } } }
2、在web.Config中加入自定义的handler,集成模式需要在<system.webServer>节点配置,经典模式在<system.web>节点配置
<system.web> <compilation debug="true" targetFramework="4.6.1"/> <httpRuntime targetFramework="4.5"/> <!--<httpHandlers> <add verb="*" path="*" type="WebSocketTest.MyWebSocketHandler, WebSocketTest" /> </httpHandlers>--> </system.web> <system.webServer> <handlers> <add name="webSocketHandler" verb="*" path="*" type="WebSocketTest.MyWebSocketHandler, WebSocketTest"/> </handlers> </system.webServer>
3、运行VS,进行测试
运行结果
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能