翼度科技»论坛 编程开发 .net 查看内容

.Net MVC 实现WebSocket

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
WebSocket 
1.基于Html5,IIS8.0版本以上,前端代码和服务器都必须支持WebSocket才能使用;
2.请求必须以WS:开头
下面是后台接收前端websocket申请的方法:
  1. /// <summary>
  2.         /// WebSocket建立链接的方法
  3.         /// </summary>
  4.         /// <param name="name"></param>
  5.         public void MyWebSocket(string name)
  6.         {
  7.             //MVC中的上下文中存在IsWebSocketRequest这样一个属性,来看当前是否是websocket
  8.             if (HttpContext.IsWebSocketRequest)
  9.             {
  10.                 this.UserName = name;
  11.                 //如果是websocket,那需要指定一个委托 ,把方法ProcessChat当做一个参数传入AcceptWebSocketRequest中来执行。
  12.                 HttpContext.AcceptWebSocketRequest(ProcessChat);
  13.             }
  14.             else
  15.             {
  16.                 HttpContext.Response.Write("我不处理");
  17.             }
  18.         }
复制代码
 
从上面代码可以知道申请的连接必须是websocket,否则不做处理,如果我浏览器上通过url调用这个方法,而不是通过创建websocket对象来调用的话,会出现下面结果:

 如果是websocket对象调用的话就能够走通:
  1. var url = "ws://localhost:57211/Home/MyWebSocket";
  2.         var socket;
  3.         function connect() {
  4.             var webSocketUrl = url + "?name=" + $("#userName").val();
  5.             //注意:下面这行代码执行之后就已经调通到后台的MyWebSocket方法中了。
  6.             socket = new WebSocket(webSocketUrl)
  7.         }
复制代码
就能够执行到后台自定义的ProcessChat方法中了,这个方法专门处理websocket连接的相关事务逻辑。比如说前端发来消息,后端回复收到:
  1. public async Task ProcessChat(AspNetWebSocketContext socketContext)
  2.         {
  3.             //socketContext.WebSocket这里获取到的是当前浏览器传到服务器端一个websocket对象信息,通过这个对象就能在当前的连接通过中进行信息的处理
  4.             System.Net.WebSockets.WebSocket socket = socketContext.WebSocket;
  5.             ArraySegment<byte> buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes($"这里是服务器,客户端你的消息我收到了"));
  6.             CancellationToken cancellation = new CancellationToken();
  7.             //第三个参数需要设置为true
  8.             await socket.SendAsync(buffer, WebSocketMessageType.Text, true, cancellation);
  9.         }
复制代码
WebSocket四大事件
1.OnOpen: 连接打开时触发
2.OnMessage: 接收消息时触发 (来自于服务器的消息)
3.OnError: 异常时触发
4.OnClose: 连接关闭时触发
前端整体代码如下:
  1. @{
  2.     Layout = null;
  3. }
  4. <h3>WebSocket</h3>
  5. <form id="form1" runat="server">
  6.    
  7.         <input id="userName" type="text" />
  8.         <input id="conn" type="button" value="连接" />
  9.         <input id="close" type="button" value="关闭" />
  10.         <span id="tips"></span>
  11.         <input id="content" type="text" />
  12.         <input id="send" type="button" value="发送" />
  13.    
  14.    
  15.         <ul></ul>
  16.    
  17. </form>
复制代码
 
点击连接按钮:

补充:
SendAsync方法:
 
  1. //
  2.         // 摘要:
  3.         //     发送 WebSocket 上连接异步的数据。
  4.         //
  5.         // 参数:
  6.         //   buffer:
  7.         //     要通过连接发送的缓冲区。
  8.         //
  9.         //   messageType:
  10.         //     指示应用是否发送二进制或文本消息。
  11.         //
  12.         //   endOfMessage:
  13.         //     指示在“缓冲区”的数据是否实消息的最后一部分。
  14.         //
  15.         //   cancellationToken:
  16.         //     传播有关应取消操作的通知的标记。
  17.         //
  18.         // 返回结果:
  19.         //     返回 System.Threading.Tasks.Task。表示异步操作的任务对象。
  20.         public abstract Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken);
复制代码
ArraySegment
WebSocket:还支持ashx/aspx/webapi,不仅仅是支持MVC。有时间可以研究下基于WebApi的实现。
 如果是多人同时和服务器端进行聊天,服务器如何分辨并回复?
下面先简单实现一个功能: 客户端1和客户端2能够实现对话
思路:webSocket每次链接到服务器之后,就在服务器端把链接保存起来。我现在的做法是浏览器发送消息的时候需要将对方的名称写上,根据这个名称找到对应负责通信的websocket对象,实现通信。实际开发中可以根据需求进行更改。
一个封装好的聊天类:
 
  1. namespace Utility
  2. {
  3.     public class ChatManager
  4.     {
  5.         /// <summary>
  6.         /// 每一个Socket对应一个客户端和服务器的连接(也可理解成一个用户)
  7.         ///  
  8.         /// </summary>
  9.         public static List<SocketModel> socketlist = new List<SocketModel>();
  10.         //SocketModel 建议大家保存在NoSql  Redis  MongoDb;
  11.         /// <summary>
  12.         /// 发送消息  这里在发送的消息上是做了格式限制的
  13.         /// </summary>
  14.         /// <param name="messge">浏览器传来的消息,默认的格式:user1;你好,就是用户名:发送信息的内容</param>
  15.         /// <param name="cancellationToken"></param>
  16.         public static void SendOne(string messge, CancellationToken cancellationToken)
  17.         {
  18.             //   user1;你好
  19.             string[] messageArray = messge.Split(':');  //toUser:Message;
  20.             //用户名
  21.             string toUser = messageArray[0];
  22.             //消息
  23.             string toMessage = messageArray[1];
  24.             //根据用户名找到对应的socket
  25.             var socketModel = socketlist.FirstOrDefault(a => toUser.Equals(a.UserName));
  26.             if (socketModel != null)
  27.             {
  28.                 //使用当前用户的socket对象进行通信
  29.                 WebSocket toSocket = socketModel.Socket;
  30.                 ArraySegment<byte> buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(toMessage));
  31.                 toSocket.SendAsync(buffer, WebSocketMessageType.Text, true, cancellationToken);
  32.             }
  33.         }
  34.         /// <summary>
  35.         /// 添加一个用户(包含了这个用户对应的Socket)
  36.         /// </summary>
  37.         /// <param name="socketGuid"></param>
  38.         /// <param name="userName"></param>
  39.         /// <param name="socket"></param>
  40.         public static void AddUser(string socketGuid, string userName, WebSocket socket)
  41.         {
  42.             socketlist.Add(new SocketModel()
  43.             {
  44.                 SocketGuid = socketGuid,
  45.                 UserName = userName,
  46.                 Socket = socket
  47.             });
  48.         }
  49.         /// <summary>
  50.         /// 删除已经连接的用户
  51.         /// </summary>
  52.         /// <param name="socketGuid"></param>
  53.         public static void RemoveUser(string socketGuid)
  54.         {
  55.             socketlist = socketlist.Where(a => a.SocketGuid != socketGuid).ToList();
  56.         }
  57.         
  58.     }
  59. }
复制代码
SocketModel类:
 
  1. namespace Utility
  2. {
  3.     public class SocketModel
  4.     {
  5.         /// <summary>
  6.         /// 链接的唯一ID
  7.         /// </summary>
  8.         public string SocketGuid { get; set; }
  9.         /// <summary>
  10.         ///  用户名称
  11.         /// </summary>
  12.         public string UserName { get; set; }
  13.         /// <summary>
  14.         /// 每一个用户链接进来以后 对应的这一个Socket实例
  15.         /// </summary>
  16.         public WebSocket Socket { get; set; }
  17.     }
  18. }
复制代码
接收浏览器信息以及进行的逻辑判断:
  1. private string UserName = string.Empty;        /// <summary>
  2.         /// WebSocket建立链接的方法
  3.         /// </summary>
  4.         /// <param name="name"></param>
  5.         public void MyWebSocket(string name)
  6.         {
  7.             //MVC中的上下文中存在IsWebSocketRequest这样一个属性,来看当前是否是websocket
  8.             if (HttpContext.IsWebSocketRequest)
  9.             {
  10.                 this.UserName = name;
  11.                 //如果是websocket,那需要指定一个委托 ,把方法ProcessChat当做一个参数传入AcceptWebSocketRequest中来执行。
  12.                 HttpContext.AcceptWebSocketRequest(ProcessChat);
  13.             }
  14.             else
  15.             {
  16.                 HttpContext.Response.Write("我不处理");
  17.             }
  18.         }        public async Task ProcessChat(AspNetWebSocketContext socketContext)        {            //socketContext.WebSocket这里获取到的是当前浏览器传到服务器端一个websocket对象信息,通过这个对象就能在当前的连接通过中进行信息的处理            System.Net.WebSockets.WebSocket socket = socketContext.WebSocket;            //(1)只要有websocket链接进来,直接保存。            CancellationToken token = new CancellationToken();            string socketGuid = Guid.NewGuid().ToString();             {                ChatManager.AddUser(socketGuid, UserName, socket);            } //(2)准备接受消息然后转发个目标方            while (socket.State == WebSocketState.Open)            {                ArraySegment buffer = new ArraySegment(new byte[2048]);                //接受来自于浏览器的消息                 WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, token);                // 解析来自于浏览器发送过来的消息内容                   string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);                //是否关闭链接                if (result.MessageType == WebSocketMessageType.Close)                {                                    }                else                {                    ChatManager.SendOne(userMessage, token);                 }            }        }
复制代码
 
结果:

 
 基于上面代码,实现群聊
思路:群都是有上限的,所以我们可以定义一个上限数量的群,只要进来一个用户,就占用一定数量内的一个socket对象,群发的时候就只发给socket对象不空的。
 后端代码:
 
  1. private string UserName = string.Empty;        /// <summary>
  2.         /// WebSocket建立链接的方法
  3.         /// </summary>
  4.         /// <param name="name"></param>
  5.         public void MyWebSocket(string name)
  6.         {
  7.             //MVC中的上下文中存在IsWebSocketRequest这样一个属性,来看当前是否是websocket
  8.             if (HttpContext.IsWebSocketRequest)
  9.             {
  10.                 this.UserName = name;
  11.                 //如果是websocket,那需要指定一个委托 ,把方法ProcessChat当做一个参数传入AcceptWebSocketRequest中来执行。
  12.                 HttpContext.AcceptWebSocketRequest(ProcessChat);
  13.             }
  14.             else
  15.             {
  16.                 HttpContext.Response.Write("我不处理");
  17.             }
  18.         }         ///         /// websocket请求的执行方法        ///         /// AspNetWebSocketContext:提供有关各个 System.Web.WebSockets.AspNetWebSocket 请求的表示上下文详细信息的基本类。        ///         public async Task ProcessChat(AspNetWebSocketContext socketContext)        {            //socketContext.WebSocket这里获取到的是当前浏览器传到服务器端一个websocket对象信息,通过这个对象就能在当前的连接通过中进行信息的处理            System.Net.WebSockets.WebSocket socket = socketContext.WebSocket;            //(1)只要有websocket链接进来,直接保存。            CancellationToken token = new CancellationToken();            string socketGuid = Guid.NewGuid().ToString();            OldChatManager.AddUser(socketGuid, UserName, socket, token);            //只要是有人进入聊天室,就应该发送一个消息,xxx进入聊天室;            await OldChatManager.Say(token, UserName, "进入聊天室。。。");            //(2)准备接受消息然后转发个目标方            while (socket.State == WebSocketState.Open)            {                ArraySegment buffer = new ArraySegment(new byte[2048]);                //接受来自于浏览器的消息                 WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, token);                // 解析来自于浏览器发送过来的消息内容                   string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);                //是否关闭链接                if (result.MessageType == WebSocketMessageType.Close)                {                    OldChatManager.RemoveUser(socketGuid);                    await OldChatManager.Say(token, UserName, "离开聊天室");                    await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, token);                }                else                {                    await OldChatManager.SengdMessage(token, UserName, userMessage);                }            }        }
复制代码
 
OldChatManager:
  1. public class OldChatManager
  2.     {
  3.         ///一个群就应该有固定的的人数;
  4.         /// <summary>
  5.         /// 默认某一个群组里面有这么一些人
  6.         /// 1.默认这个群里就有四个人;
  7.         /// </summary>
  8.         public static List<SocketModel> socketlist = new List<SocketModel>() {
  9.              new SocketModel(){ SocketGuid=string.Empty,UserName="User1",Socket=null },
  10.              new SocketModel(){ SocketGuid=string.Empty,UserName="User2",Socket=null },
  11.              new SocketModel(){ SocketGuid=string.Empty,UserName="User3",Socket=null },
  12.              new SocketModel(){ SocketGuid=string.Empty,UserName="User4",Socket=null }
  13.         };
  14.         // string: 要发谁   ArraySegment<byte>:要发送的消息
  15.         public static Dictionary<string, List<ArraySegment<byte>>> chatList = new Dictionary<string, List<ArraySegment<byte>>>();
  16.         /// <summary>
  17.         /// 增加
  18.         /// </summary>
  19.         /// <param name="socketGuid"></param>
  20.         /// <param name="userName"></param>
  21.         /// <param name="socket"></param>
  22.         /// <param name="token"></param>
  23.         public static void AddUser(string socketGuid, string userName, WebSocket socket, CancellationToken token)
  24.         {
  25.             socketlist.ForEach(item =>
  26.             {
  27.                 if (userName == item.UserName)
  28.                 {
  29.                     item.Socket = socket;
  30.                     item.SocketGuid = socketGuid;
  31.                 }
  32.             });
  33.             #region  离线消息的处理  把这段代码注释掉之后,新来的用户也不会收到之前的 消息了
  34.             if (chatList.ContainsKey(userName) && chatList[userName].Count > 0)
  35.             {
  36.                 foreach (var item in chatList[userName])
  37.                 {
  38.                     socket.SendAsync(item, WebSocketMessageType.Text, true, token);
  39.                 }
  40.             }
  41.             #endregion
  42.         }
  43.         /// <summary>
  44.         /// 退出登录之后去掉通信的websocket对象
  45.         /// </summary>
  46.         /// <param name="socketGuid"></param>
  47.         public static void RemoveUser(string socketGuid)
  48.         {
  49.             socketlist.ForEach(item =>
  50.             {
  51.                 if (socketGuid == item.SocketGuid)
  52.                 {
  53.                     item.Socket = null;
  54.                     item.SocketGuid = null;
  55.                 }
  56.             });
  57.         }
  58.         /// <summary>
  59.         ///  群发消息 包括离线消息
  60.         /// </summary>
  61.         /// <param name="token"></param>
  62.         /// <param name="userName"></param>
  63.         /// <param name="content"></param>
  64.         /// <returns></returns>
  65.         public static async Task SengdMessage(CancellationToken token, string userName, string content)
  66.         {
  67.             ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[2048]);
  68.             buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes($"{DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss:fff")}{userName}:{content}"));
  69.             foreach (var socketInfo in socketlist)
  70.             {
  71.                 //如果为空,表示这个websocket对象实例没有被分配,不在线。
  72.                 if (socketInfo.Socket == null)
  73.                 {
  74.                     #region  chatList里面存的是离线人员,在这里是存储信息,等待离线人员上线之后能够接收到信息
  75.                     //这里主要就是负责离线消息的,确保新登录的用户可以看到之前的消息,
  76.                     //然后看看要发送的对象列表中有没有这个用户,存在的话就把这个消息暂存到这个用户对应的信息中,如果这个用户上线之后,就可以把这些数据全部发给他。当然也可以将这些数据存到数据库中,                       但是不怎么好,最好还是放到redis,nosql,MongoDb。
  77.                     if (chatList.ContainsKey(socketInfo.UserName))
  78.                     {
  79.                         chatList[socketInfo.UserName].Add(buffer);
  80.                     }
  81.                     else
  82.                     {
  83.                         chatList.Add(socketInfo.UserName, new List<ArraySegment<byte>>() { buffer });
  84.                     }
  85.                     #endregion
  86.                 }
  87.                 else
  88.                 {
  89.                     await socketInfo.Socket.SendAsync(buffer, WebSocketMessageType.Text, true, token);
  90.                 }
  91.             }
  92.         }
  93.         /// <summary>
  94.         /// 这个不管离线消息 当前用户进来
  95.         /// </summary>
  96.         /// <param name="token"></param>
  97.         /// <param name="userName"></param>
  98.         /// <param name="content"></param>
  99.         /// <returns></returns>
  100.         public static async Task Say(CancellationToken token, string userName, string content)
  101.         {
  102.             ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[2048]);
  103.             buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes($"{DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss:fff")}{userName}:{content}"));
  104.             foreach (var socketInfo in socketlist)
  105.             {
  106.                 if (socketInfo.Socket != null)
  107.                 {
  108.                     await socketInfo.Socket.SendAsync(buffer, WebSocketMessageType.Text, true, token);
  109.                 }
  110.             }
  111.         }
  112.     }
复制代码
 
 
结果:

 
 
 
心跳包和断线重连
Reconnecting断线自动重连
 
zhuangzai :
https://www.cnblogs.com/anjingdian/p/15327526.html
 

来源:https://www.cnblogs.com/BluceLee/archive/2023/03/24/WebSoket.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具