• 大小: 112KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-13
  • 语言: C#
  • 标签: WebSocket  WinForm  Client  

资源简介

C#编写的基于WebSocket winform客户端Demo源码,实现连接、收到消息、关闭事件。适合初学者参考。需要DotNet4.5框架

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebSocketClientDemo
{

    public partial class Form1 : Form
    {
        private ClientWebSocket client;
        private delegate void UI();
        delegate void ClientThread(ClientWebSocket client);
        public Form1()
        {
            InitializeComponent();

           
        }

        private void button1_Click(object sender EventArgs e)
        {
            

            string line = txtMsg.Text;
      
                var array = new ArraySegment(Encoding.UTF8.GetBytes(line));
                client.SendAsync(array WebSocketMessageType.Text true CancellationToken.None);
        }

        public async void StartReceiving(object obj)
        {
            ClientWebSocket client = (ClientWebSocket)obj;
            while (client.State == WebSocketState.Open)
            {
                var array = new byte[4096];
                var result = await client.ReceiveAsync(new ArraySegment(array) CancellationToken.None);
                if (result.MessageType == WebSocketMessageType.Text)
                {
                    string msg = Encoding.UTF8.GetString(array 0 result.Count);
                    UI u =delegate{
                        lstMsg.Items.Add(msg);
                    };
                    this.Invoke(u);
                    
                }
            }
        }

        public static void Test(object i)
        {

        }

        private void button2_Click(object sender EventArgs e)
        {
            if(client == null || client.State != WebSocketState.Open)
            {
                client = new ClientWebSocket();
                String url = txtUrl.Text;
                client.ConnectAsync(new Uri(url) CancellationToken.None).Wait();
                //WaitCallback back = new WaitCallback(StartReceiving);

                //ThreadPool.QueueUserWorkItem (back client);
              

                Thread t = new Thread(new ParameterizedThreadStart(StartReceiving));
                t.IsBackground = true;
                t.Start(client);

                btnOpen.Enabled = false;
                btnSend.Enabled = true;
                btnClose.Enabled = true;
            }
            
        }

        private void Form1_FormClosed(object sender FormClosedEventArgs e)
        {
            
        }

        private void button3_Click(object sender EventArgs e)
        {
            if (client != null)
            {
                client.CloseAsync(WebSocketCloseStatus.Empty null CancellationToken.None);
                client.Dispose();
            }
            btnOpen.Enabled = true;
            btn

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     34816  2018-08-30 15:37  WebSocketClientDemo\.vs\WebSocketClientDemo\v14\.suo

     文件        184  2018-08-30 15:06  WebSocketClientDemo\WebSocketClientDemo\App.config

     文件      13312  2018-08-30 14:53  WebSocketClientDemo\WebSocketClientDemo\bin\Debug\WebSocketClientDemo.exe

     文件        189  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\bin\Debug\WebSocketClientDemo.exe.config

     文件      24064  2018-08-30 14:53  WebSocketClientDemo\WebSocketClientDemo\bin\Debug\WebSocketClientDemo.pdb

     文件      22696  2018-08-30 14:53  WebSocketClientDemo\WebSocketClientDemo\bin\Debug\WebSocketClientDemo.vshost.exe

     文件        189  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\bin\Debug\WebSocketClientDemo.vshost.exe.config

     文件        490  2018-04-12 07:35  WebSocketClientDemo\WebSocketClientDemo\bin\Debug\WebSocketClientDemo.vshost.exe.manifest

     文件        189  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\bin\Release\WebSocketClientDemo.exe.config

     文件      22696  2018-08-30 14:54  WebSocketClientDemo\WebSocketClientDemo\bin\Release\WebSocketClientDemo.vshost.exe

     文件        189  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\bin\Release\WebSocketClientDemo.vshost.exe.config

     文件        490  2018-04-12 07:35  WebSocketClientDemo\WebSocketClientDemo\bin\Release\WebSocketClientDemo.vshost.exe.manifest

     文件      12800  2018-08-30 15:06  WebSocketClientDemo\WebSocketClientDemo\bin\x86\Release\WebSocketClientDemo.exe

     文件        184  2018-08-30 15:06  WebSocketClientDemo\WebSocketClientDemo\bin\x86\Release\WebSocketClientDemo.exe.config

     文件      24064  2018-08-30 15:06  WebSocketClientDemo\WebSocketClientDemo\bin\x86\Release\WebSocketClientDemo.pdb

     文件      22696  2018-08-30 15:06  WebSocketClientDemo\WebSocketClientDemo\bin\x86\Release\WebSocketClientDemo.vshost.exe

     文件        184  2018-08-30 15:06  WebSocketClientDemo\WebSocketClientDemo\bin\x86\Release\WebSocketClientDemo.vshost.exe.config

     文件        490  2018-04-12 07:35  WebSocketClientDemo\WebSocketClientDemo\bin\x86\Release\WebSocketClientDemo.vshost.exe.manifest

     文件       3090  2018-08-30 14:59  WebSocketClientDemo\WebSocketClientDemo\Form1.cs

     文件       6209  2018-08-30 14:59  WebSocketClientDemo\WebSocketClientDemo\Form1.Designer.cs

     文件       5817  2018-08-30 14:59  WebSocketClientDemo\WebSocketClientDemo\Form1.resx

     文件        868  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7369  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2018-08-30 11:48  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       1257  2018-08-30 14:53  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\WebSocketClientDemo.csproj.FileListAbsolute.txt

     文件       1012  2018-08-30 13:25  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\WebSocketClientDemo.csproj.GenerateResource.Cache

     文件       2384  2018-08-30 12:05  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\WebSocketClientDemo.csprojResolveAssemblyReference.cache

     文件      13312  2018-08-30 14:53  WebSocketClientDemo\WebSocketClientDemo\obj\Debug\WebSocketClientDemo.exe

............此处省略51个文件信息

评论

共有 条评论