博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebSocket异步通讯,实时返回数据实例
阅读量:7071 次
发布时间:2019-06-28

本文共 3567 字,大约阅读时间需要 11 分钟。

定义类中的异步方法

using System;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WindowsServiceTest;

namespace WindowsService

{
/// <summary>
/// 客户端的通讯公共类对象
/// </summary>
public class WindowsOSHelperUtils
{

/// <summary>

/// Socket通讯的url
/// </summary>
//public static string Url = "ws://192.168.1.11:1234/";

public static async Task<string> Login(string name, string no, string level, string imagestring)

{
ClientWebSocket cln = new ClientWebSocket();
cln.ConnectAsync(new Uri(FunctionBaseClass.SocketUrl), new CancellationToken()).Wait();
byte[] bytess = Encoding.Default.GetBytes($"login#{name}#{no}#{level}#{imagestring}");
cln.SendAsync(new ArraySegment<byte>(bytess), WebSocketMessageType.Text, true, new CancellationToken()).Wait();
string returnValue = await GetAsyncValue(cln);//异步方法
return returnValue;
}

public static async Task<string> EvaluateWithReasons()
{
ClientWebSocket cln = new ClientWebSocket();
cln.ConnectAsync(new Uri(FunctionBaseClass.SocketUrl), new CancellationToken()).Wait();
byte[] bytess = Encoding.Default.GetBytes($"asses");
cln.SendAsync(new ArraySegment<byte>(bytess), WebSocketMessageType.Text, true, new CancellationToken()).Wait();
string returnValue = await GetAsyncValue(cln);//异步方法
return returnValue;
}

public static async Task<string> FaceValidateWithIdCard()
{
//FaceValidateWithIdCard faceValidateWithIdCard = (FaceValidateWithIdCard)command;
ClientWebSocket cln = new ClientWebSocket();
cln.ConnectAsync(new Uri(FunctionBaseClass.SocketUrl), new CancellationToken()).Wait();
byte[] bytess = Encoding.Default.GetBytes($"begin");
cln.SendAsync(new ArraySegment<byte>(bytess), WebSocketMessageType.Text, true, new CancellationToken()).Wait();
string returnValue = await GetAsyncValue(cln);//异步方法
return returnValue;
#region MyRegion
//string url = "ws://127.0.0.1:1234/";
//try
//{
// ClientWebSocket cln = new ClientWebSocket();
// cln.ConnectAsync(new Uri(url), new CancellationToken()).Wait();

// byte[] bytess = Encoding.Default.GetBytes("begin#70");

// cln.SendAsync(new ArraySegment<byte>(bytess), WebSocketMessageType.Text, true, new CancellationToken()).Wait();
// byte[] bytes2 = new byte[1000 * 500];
// //var webSocketReceiveResult = cln.ReceiveAsync(new ArraySegment<byte>(bytes2), CancellationToken.None);

// //GetAsyncValue(cln);//异步方法,很关键

// //string xx = Encoding.Default.GetString(bytes2);

// Console.WriteLine("11111111111");
// Console.Read();

//}

//catch (Exception ex)
//{
// string ss = ex.ToString();
// Console.WriteLine(ss);
// //}
// Console.Read();
//}
#endregion
}

public static async Task<string> GetAsyncValue(ClientWebSocket clientWebSocket)

{
string returnValue = null;
ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]);
WebSocketReceiveResult result = null;
using (var ms = new MemoryStream())
{
do
{
result = await clientWebSocket.ReceiveAsync(buffer, CancellationToken.None);
ms.Write(buffer.Array, buffer.Offset, result.Count);
}
while (!result.EndOfMessage);
ms.Seek(0, SeekOrigin.Begin);
if (result.MessageType == WebSocketMessageType.Text)
{
using (var reader = new StreamReader(ms, Encoding.UTF8))
{
returnValue = reader.ReadToEnd();
//Console.WriteLine(returnValue);
}
}
}
return returnValue;
}
}
}

 

调用异步方法

string returnvalue = WindowsOSHelperUtils.Login(userName, userNo, "5", byteString).Result;

string returnvalue = WindowsOSHelperUtils.EvaluateWithReasons().Result;

string returnvalue = WindowsOSHelperUtils.FaceValidateWithIdCard().Result;

 

转载地址:http://hshll.baihongyu.com/

你可能感兴趣的文章
基本名词介绍
查看>>
HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)
查看>>
C++ 脑筋急转弯
查看>>
P3723 [AH2017/HNOI2017]礼物
查看>>
spring web app的结构
查看>>
Python+Appium手机纯H5页面测试
查看>>
ES6---数组array新增方法
查看>>
为什么掌握 UML 建模是成为编程高手的一条捷径?
查看>>
【转】vi查询卡片
查看>>
call dword prt[eax]
查看>>
SpringMVC学习笔记:单例与并发问题
查看>>
[20190524]sqlplus 与输出&.txt
查看>>
事务的超时和只读属性
查看>>
Web.config配置文件详解(新手必看)<转>
查看>>
【转】shell编程:数学运算
查看>>
linux琐碎知识点
查看>>
LightOj 1284
查看>>
ASP.NET
查看>>
使用mosh取代ssh提高n2n网络连接稳定性
查看>>
Introduction - 介绍
查看>>