Socket--WebSocket的使用

Socket–WebSocket的使用

一、后端程序

public class HomeController : Controller
{
    public ActionResult WebSocket()
    {
        return View();
    }

    private string UserName = string.Empty;

    /// 
    /// WebSocket建立链接的方法
    /// 
    /// 
    public void MyWebSocket(string name)
    {
        if (HttpContext.IsWebSocketRequest)
        {
            this.UserName = name;
            HttpContext.AcceptWebSocketRequest(ProcessChat);
        }
        else
        {
            HttpContext.Response.Write("我不处理");
        }
    }


    public async Task ProcessChat(AspNetWebSocketContext socketContext)
    {
        //  SuperSocket:Session
        // 表示客户端发起请求的一个链接
        System.Net.WebSockets.WebSocket socket = socketContext.WebSocket;

        CancellationToken token = new Cancellat

你可能感兴趣的:(C#,.Net,websocket,网络协议,网络)