WebSocket服务端和客户端使用

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

namespace WindowsService
{
/// <summary>
/// 客户端的通信公共类对象
/// </summary>
public class WindowsOSHelperUtils
{websocket

bool isStarted = false;
public static void StartListen()
{
//线程启动监听服务
Thread thread = new Thread(new ThreadStart(ServerMethod));
thread.IsBackground = true;
thread.Start();
}
public static async void ServerMethod()
{
try
{
while (true)
{
try
{
//listener = new HttpListener();
//listener.Prefixes.Add("http://+:" + Port + "/");
//listener.Start();
var listen = new HttpListener();
listen.Prefixes.Add("http://192.168.1.201:1234/");
listen.Start();
}
catch (Exception e)
{
Thread.Sleep(1000);
}异步

try
{
while (true)
{
//ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]);
//接收数据
byte[] abuf = new byte[8192];
var wsdata = await ws.ReceiveAsync(new ArraySegment<byte>(abuf), new CancellationToken());
Console.WriteLine(wsdata.Count);
byte[] bRec = new byte[wsdata.Count];
Array.Copy(abuf, bRec, wsdata.Count);
Console.WriteLine(Encoding.Default.GetString(bRec));
}
}
catch (Exception e)
{
}
}
}
catch (Exception e)
{
}socket

 

//ok的代码下面ianasync

//var content = listen.GetContext();
//var wsContext = await content.AcceptWebSocketAsync(null);
//var ws = wsContext.WebSocket;lua

////ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]);
////接收数据
//byte[] abuf = new byte[8192];
//var wsdata = await ws.ReceiveAsync(new ArraySegment<byte>(abuf), new CancellationToken());
//Console.WriteLine(wsdata.Count);
//byte[] bRec = new byte[wsdata.Count];
//Array.Copy(abuf, bRec, wsdata.Count);
//Console.WriteLine(Encoding.Default.GetString(bRec));spa


//ClientWebSocket cln = new ClientWebSocket();
//cln.ConnectAsync(new Uri("ws://192.168.1.201:3344/"), 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> 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.Binary, 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.Binary, true, new CancellationToken()).Wait();
string returnValue = await GetAsyncValue(cln);//异步方法
return returnValue;
}string


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.Binary, true, new CancellationToken()).Wait();
string returnValue = await GetAsyncValue(cln);//异步方法
return returnValue;
}

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;
}
}
}

 

 

调用websocket方法

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