WebSocket简单尝试
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); } } } } }
<span>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[]); WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(receiveSegment, CancellationToken.None); string clientMsg = Encoding.UTF8.GetString(receiveSegment.Array, , 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,进行测试
运行结果
相关推荐
柳木木的IT 2020-11-04
joynet00 2020-09-23
wenf00 2020-09-14
蓝色深海 2020-08-16
wuychn 2020-08-16
取个好名字真难 2020-08-06
darylove 2020-06-26
shufen0 2020-06-20
Lovexinyang 2020-06-14
WangBowen 2020-06-14
firejq 2020-06-14
hjhmpl 2020-06-14
水痕 2020-06-07
guozewei0 2020-06-06
woniyu 2020-06-02
取个好名字真难 2020-06-01
guozewei0 2020-05-28
woniyu 2020-05-26