资源简介
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个文件信息
相关资源
- SignalR 2.0 Winform版
- C# winform Socket大文件传输
- C#将Excel导入到Access数据库表(winFor
- winform图片标尺,画矩形
- C# 斑马标签打印 WEBWINFORM
- C#动画控件超炫
- WeifenLuo.WinFormsUI.Docking3.1.0
- Bunifu_Ui_WinForms_v1.10.0.0_Downloadly.ir.rar
- Winform可视化打印模板设计
- 最新9个C# .NET Winform的多线程进度条源
- Winform绘制圆形和椭圆形图章
- c# 图像旋转 winform 窗体 图片转动
- 优化C# WinForm从中国银行网站抓取汇率
- C#项目 网吧管理系统
- Winform通用经典快速开发CS框架-outloo
- 人事管理系统 winform代码
- 宾馆管理系统(c#2008access2003winform)
- C# Winform 酒店管理系统源代码及数据
- C# Winform 弹窗等待效果图
- winform自动更新程序
- C# 使用 TcpClient,Socket 聊天
- winform c# mschart demo 范例
- WinForm 实现图片滚动
- c#winform做的hosts文件管理
- 控件重绘 C# WinForm控件美化扩展系列之
- .net winform插件大全 .net 控件大全.net插
- winform chart动态跟进式折线图
- WinForm C#Demo 会员与消费系统 进销存系
- c# 模仿windows扑克游戏
- 幸运52winform写的游戏
评论
共有 条评论