中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

C# websocketserver與客戶端通信

c#
小樊
102
2024-07-16 15:48:57
欄目: 編程語言

要實現C# WebSocket服務器與客戶端通信,您可以使用.NET的WebSocket庫來創建WebSocket服務器和客戶端。以下是一個簡單的示例代碼,演示了如何在C#中實現WebSocket服務器和客戶端之間的通信:

WebSocket服務器端代碼示例:

using System;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var listener = new HttpListener();
        listener.Prefixes.Add("http://localhost:8080/");
        listener.Start();

        while (true)
        {
            var context = await listener.GetContextAsync();
            if (context.Request.IsWebSocketRequest)
            {
                ProcessWebSocketRequest(context);
            }
            else
            {
                context.Response.StatusCode = 400;
                context.Response.Close();
            }
        }
    }

    static async Task ProcessWebSocketRequest(HttpListenerContext context)
    {
        var webSocketContext = await context.AcceptWebSocketAsync(null);
        var webSocket = webSocketContext.WebSocket;

        var buffer = new byte[1024];
        WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

        while (!result.CloseStatus.HasValue)
        {
            var message = System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
            Console.WriteLine($"Received message: {message}");

            await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);

            result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
        }

        await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
    }
}

WebSocket客戶端代碼示例:

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

class Program
{
    static async Task Main(string[] args)
    {
        var webSocket = new ClientWebSocket();
        await webSocket.ConnectAsync(new Uri("ws://localhost:8080/"), CancellationToken.None);

        var input = Console.ReadLine();
        var buffer = Encoding.UTF8.GetBytes(input);
        await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);

        var receiveBuffer = new byte[1024];
        var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
        var message = Encoding.UTF8.GetString(receiveBuffer, 0, result.Count);
        Console.WriteLine($"Received message: {message}");

        await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed by client", CancellationToken.None);
    }
}

這是一個簡單的示例,演示了如何在C#中實現WebSocket服務器和客戶端之間的通信。您可以根據自己的需求和業務邏輯進一步擴展和修改這段代碼。

0
晋中市| 平度市| 潜山县| 普格县| 措美县| 紫金县| 高要市| 枣强县| 文山县| 绥滨县| 永宁县| 松阳县| 龙井市| 浑源县| 洛阳市| 札达县| 杨浦区| 鲁甸县| 洛川县| 肇州县| 法库县| 抚远县| 塔河县| 濮阳县| 新密市| 进贤县| 怀仁县| 阜新市| 望都县| 通渭县| 宿松县| 乌鲁木齐市| 如皋市| 明星| 大邑县| 云霄县| 木里| 望城县| 静海县| 日照市| 禄丰县|