WebSocket NetCore

Design Form Sign In

<form method="post" name="frmsignin">
    <p>
        <label>Nick Name</label>
        <input type="text" name="nick" />
    </p>
    <p>
        <button>Sign In</button>
    </p>
</form>

Design Form Chat

<div id="rs"></div>
<button id="btnsignout">Sign Out</button>
<form method="post" name="frmchat">
    <p>
        <label>Welcome: </label>
        <input type="text" readonly="readonly" name="nick" />
    </p>
    <p>
        <label>Message</label>
        <textarea name="msg"></textarea>
    </p>
    <p>
        <button>Send</button>
    </p>
</form>

CSS for Form Chat

<style>
form[name='frmchat']{
    display: none;
}
#btnsignout{
    display: none;
}
</style>

Code Javascript for WebSocket

<script type="text/javascript">
    var ws;
    frmsignin.onsubmit = function (evt) {
        evt.preventDefault();
        var n = this['nick']
        n.value = n.value.trim();
        if (n.value != '') {
            frmchat['nick'].value = n.value;
            this.style.display = 'none';
            frmchat.style.display = 'block';
            btnsignout.style.display = 'block';
           
            ws = new WebSocket('ws://127.0.0.1:8281/socketserver');
            ws.onopen = function () {
                ws.send(frmchat['nick'].value +  ': Hello Everyone');
            }
            ws.onmessage = function (evt) {
                var msg = evt.data;
                rs.innerHTML += msg + '<br>';
            }
            ws.onclose = function () {
                alert('Connection is closed...');
            }
        }
    }
    frmchat.onsubmit = function (evt) {
        evt.preventDefault();
        ws.send(frmchat['nick'].value + ': ' + frmchat['msg'].value);
    }
    btnsignout.onclick = function () {
        window.location.reload();
    }
</script>

Code C# Server Chat

class Program
{
    static Regex regexGet = new Regex("^GET");
    static Regex regexSec = new Regex("Sec-WebSocket-Key: (.*)");

    static void HandleClient(object obj)
    {
        TcpClient client = (TcpClient)obj;
        NetworkStream stream = client.GetStream();
        while (true)
        {
            while (!stream.DataAvailable)
            {

            }
            byte[] bytes = new byte[client.Available];
            stream.Read(bytes, 0, bytes.Length);
            string request = Encoding.UTF8.GetString(bytes);
            Console.WriteLine(request);
            if (regexGet.IsMatch(request))
            {
                Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine
                    + "Connection: Upgrade" + Environment.NewLine
                    + "Upgrade: websocket" + Environment.NewLine
                    + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
                        SHA1.Create().ComputeHash(
                            Encoding.UTF8.GetBytes(
                                regexSec.Match(request).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                            )
                        )
                    ) + Environment.NewLine
                    + Environment.NewLine);

                stream.Write(response, 0, response.Length);
            }
            else
            {
                foreach (TcpClient item in clients)
                {
                    NetworkStream network = item.GetStream();
                    network.Write(bytes, 0, bytes.Length);
                }
            }
        }
    }
    static List<TcpClient> clients = new List<TcpClient>();
    static void Main(string[] args)
    {
        TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8281);

        listener.Start();
        Console.WriteLine("Server Start!");
        while (true)
        {
            TcpClient client = listener.AcceptTcpClient();
            lock (clients) clients.Add(client);
            Thread thread = new Thread(HandleClient);
            thread.Start(client);
        }
    }
}