c# webapi websocket 服务端消息发送
参考网址
https://www.cnblogs.com/wangx036/p/12565128.html
WebSocket相当于在MvC中做了一个一直运行的服务
将其他来源的数据接收
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
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;
using cxweb_pg.Models;
namespace cxweb_pg.Controllers
{
public class WsChatController : ApiController
{
private static List<WebSocket> _sockets = new List<WebSocket>();
[ ]
public HttpResponseMessage Connect()
{
//在服务器端接受Web Socket请求,传入的函数作为Web Socket的处理函数,
//待Web Socket建立后该函数会被调用,在该函数中可以对Web Socket进行消息收发
HttpContext.Current.AcceptWebSocketRequest(ProcessRequest);
//构造同意切换至Web Socket的Response.
return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
}
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]);
//对web socket进行异步接收数据
var receivedResult = await socket.ReceiveAsync(buffer, CancellationToken.None);
if (receivedResult.MessageType == WebSocketMessageType.Close)
{
//如果client发起close请求,对client进行ack
await socket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);
_sockets.Remove(socket);
break;
}
if (socket.State == WebSocketState.Open)
{
string recvMsg = Encoding.UTF8.GetString(buffer.Array, 0, receivedResult.Count);
var recvBytes = Encoding.UTF8.GetBytes(recvMsg);
var sendBuffer = new ArraySegment<byte>(buffer.Array);
//当接收到文本消息时,对当前服务器上所有web socket连接进行广播
foreach (var innerSocket in _sockets)
{
if (innerSocket != socket)
{
await innerSocket.SendAsync(sendBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}
}
}
}
}
前端在使用的时候先调用Connect方法
然后在调用SendMsg方法来发送数据
xxxController
private readonly ClientWebSocket webSocket = new ClientWebSocket();
private readonly CancellationToken _cancellation = new CancellationToken();
[ ]
public async Task SendMsg(PostDataModel msg)
{
await webSocket.ConnectAsync(new Uri("ws://192.168.100.28:3030/zhzcapi/WsChat/Connect"), _cancellation);
var sendBytes = Encoding.UTF8.GetBytes(msg.message);//发送的数据
var bsend = new ArraySegment<byte>(sendBytes);
await webSocket.SendAsync(bsend, WebSocketMessageType.Binary, true, _cancellation);
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "1", _cancellation);
webSocket.Dispose();
}
运行过程 Connect先运行的是建立了一个委托
该委托一直执行While内的循环直至连接关闭 (实际上是一直打开的 相当于保持该连接 不然会被清理)
每当有一个新消息SendMsg传过来时 向前端发送该消息