websocket

刚接触C#的时候了解过Socket编程,当时知道可以通过Socket通信实现即时通信,可是做出来类似于飞Q的小东西,但自从做了Web后端之后,对Socket编程渐渐遗忘,前端获取消息通常都是通过轮询的方式定期获取信息,但这种方式并不能达到即时的效果,也会存在大量的资源浪费,最近闲暇时分了解了一下WebSocket,可以通过WebSocket实现即时通信。话不多说,直接上Demo。

Server端(第三方WebSocket库Fleck):

using Fleck;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            FleckLog.Level = LogLevel.Debug;
            var allSockets = new List<IWebSocketConnection>();
            var server = new WebSocketServer("ws://0.0.0.0:7181");
            server.Start(socket =>
            {
                socket.OnOpen = () =>
                {
                    Console.WriteLine("Open!");
                    allSockets.Add(socket);
                };
                socket.OnClose = () =>
                {
                    Console.WriteLine("Close!");
                    allSockets.Remove(socket);
                };
                socket.OnMessage = message =>
                {
                    Console.WriteLine(message);
                    allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
                };
                socket.OnBinary = file => {

                    string path = ("D:/test.txt");
                    //创建一个文件流
                    FileStream fs = new FileStream(path, FileMode.Create);

                    //将byte数组写入文件中
                    fs.Write(file, 0, file.Length);
                    //所有流类型都要关闭流,否则会出现内存泄露问题
                    fs.Close();
                };
            });


            var input = Console.ReadLine();
            while (input != "exit")
            {
                foreach (var socket in allSockets.ToList())
                {
                    socket.Send(input);
                }
                input = Console.ReadLine();
            }
        }
    }
}

JS端

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>websocket client</title>
    <script type="text/javascript">
        var start = function () {
            var inc = document.getElementById('incomming');
            var wsImpl = window.WebSocket || window.MozWebSocket;
            var form = document.getElementById('sendForm');
            var input = document.getElementById('sendText');

            inc.innerHTML += "connecting to server ..<br/>";

            // create a new websocket and connect
            window.ws = new wsImpl('ws://localhost:7181/');

            // when data is comming from the server, this metod is called
            ws.onmessage = function (evt) {
                inc.innerHTML += evt.data + '<br/>';
            };

            // when the connection is established, this method is called
            ws.onopen = function () {
                inc.innerHTML += '.. connection open<br/>';
            };

            // when the connection is closed, this method is called
            ws.onclose = function () {
                inc.innerHTML += '.. connection closed<br/>';
            }

            form.addEventListener('submit', function (e) {
                e.preventDefault();
                var val = input.value;
                ws.send(val);
                input.value = "";
            });

        }
        window.onload = start;
    </script>
</head>
<body>
    <form id="sendForm">
        <input id="sendText" placeholder="Text to send" />
    </form>
    <pre id="incomming"></pre>
</body>
</html>

 C#客户端

using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "ws://192.168.4.4:7181";
            ClientWebSocket client = new ClientWebSocket();
            client.ConnectAsync(new Uri(url), new CancellationToken()).Wait();
            try
            {
                StartReceiving(client);
                string line;
                while ((line = Console.ReadLine()) != "exit")
                {
                    var array = new ArraySegment<byte>(Encoding.UTF8.GetBytes(line));
                    client.SendAsync(array, WebSocketMessageType.Text, true, CancellationToken.None);
                }
            }
            catch (Exception ex)
            {
                string ss = ex.ToString();
                client.CloseAsync(WebSocketCloseStatus.Empty, null, new CancellationToken()).Wait();
            }

            client.CloseAsync(WebSocketCloseStatus.Empty,null, new CancellationToken()).Wait();
        }

        static async void StartReceiving(ClientWebSocket client)
        {
            while (true)
            {
                var array = new byte[4096];
                var result = await client.ReceiveAsync(new ArraySegment<byte>(array), CancellationToken.None);
                if (result.MessageType == WebSocketMessageType.Text)
                {
                    string msg = Encoding.UTF8.GetString(array, 0, result.Count);
                    Console.WriteLine("--> {0}", msg);
                }

            }

        }
    }
}
原文地址:https://www.cnblogs.com/chenyishi/p/8390942.html