1 Star 0 Fork 2

落羽成舟/WebSocketForWebAPI

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MsgApiController.cs 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
落羽成舟 提交于 2018-12-14 17:17 . 上传文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.WebSockets;
namespace TestWebSocket.Controllers
{
[RoutePrefix("api/msg")]
public class MsgApiController : ApiController
{
private static List<WebSocket> _sockets = new List<WebSocket>();
[Route]
[HttpGet]
public HttpResponseMessage Connect()
{
HttpContext.Current.AcceptWebSocketRequest(ProcessRequest); //在服务器端接受Web Socket请求,传入的函数作为Web Socket的处理函数,待Web Socket建立后该函数会被调用,在该函数中可以对Web Socket进行消息收发
return Request.CreateResponse(HttpStatusCode.SwitchingProtocols); //构造同意切换至Web Socket的Response.
}
public async Task ProcessRequest(AspNetWebSocketContext context)
{
var socket = context.WebSocket;//传入的context中有当前的web socket对象
_sockets.Add(socket);//此处将web socket对象加入一个静态列表中
//进入一个无限循环,当web socket close是循环结束
while (true)
{
var buffer = new ArraySegment<byte>(new byte[1024]);
var receivedResult = await socket.ReceiveAsync(buffer, CancellationToken.None);//对web socket进行异步接收数据
if (receivedResult.MessageType == WebSocketMessageType.Close)
{
await socket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);//如果client发起close请求,对client进行ack
_sockets.Remove(socket);
break;
}
if (socket.State == System.Net.WebSockets.WebSocketState.Open)
{
string recvMsg = Encoding.UTF8.GetString(buffer.Array, 0, receivedResult.Count);
var recvBytes = Encoding.UTF8.GetBytes(recvMsg);
var sendBuffer = new ArraySegment<byte>(recvBytes);
foreach (var innerSocket in _sockets)//当接收到文本消息时,对当前服务器上所有web socket连接进行广播
{
if (innerSocket != socket)
{
await innerSocket.SendAsync(sendBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/lycz/WebSocketForWebAPI.git
git@gitee.com:lycz/WebSocketForWebAPI.git
lycz
WebSocketForWebAPI
WebSocketForWebAPI
master

搜索帮助