WIP: Rezygnacja z biblioteki websocket .netu

This commit is contained in:
Arek 2021-05-17 13:01:27 +02:00
parent 9e253e6f05
commit 5011fc6adb
3 changed files with 45 additions and 22 deletions

View File

@ -1,8 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>library</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="websocket-sharp" Version="1.0.0" />
</ItemGroup>
</Project> </Project>

View File

@ -1,12 +1,13 @@
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Net.Security; using System.Net.Security;
using System.Net.WebSockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Text; using System.Text;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using WebSocketSharp;
using WebSocketSharp.Net;
namespace ArStomp namespace ArStomp
{ {
@ -17,7 +18,8 @@ namespace ArStomp
{ {
private Task runner = null; private Task runner = null;
public static bool Debug { get; set; } = false; public static bool Debug { get; set; } = false;
private ClientWebSocket ws = new ClientWebSocket(); private WebSocket ws;
private volatile bool stompConnected = false;
public CancellationTokenSource Token { get; } = new CancellationTokenSource(); public CancellationTokenSource Token { get; } = new CancellationTokenSource();
private readonly X509Certificate2Collection certCollection; private readonly X509Certificate2Collection certCollection;
private readonly Dictionary<string, Subscription> subs = new Dictionary<string, Subscription>(); private readonly Dictionary<string, Subscription> subs = new Dictionary<string, Subscription>();
@ -34,9 +36,6 @@ namespace ArStomp
/// <param name="certCollection">collection of root ca certificates (if TLS is used)</param> /// <param name="certCollection">collection of root ca certificates (if TLS is used)</param>
public StompClient(X509Certificate2Collection certCollection = null) public StompClient(X509Certificate2Collection certCollection = null)
{ {
ws = new ClientWebSocket();
ws.Options.AddSubProtocol("v12.stomp");
if (certCollection != null && certCollection.Count > 0) if (certCollection != null && certCollection.Count > 0)
{ {
this.certCollection = certCollection; this.certCollection = certCollection;
@ -112,19 +111,35 @@ namespace ArStomp
/// <param name="uri">uri in format ws://host[:port][/path] or wss://host[:port][/path]</param> /// <param name="uri">uri in format ws://host[:port][/path] or wss://host[:port][/path]</param>
/// <param name="login">login name</param> /// <param name="login">login name</param>
/// <param name="password">password</param> /// <param name="password">password</param>
public async Task Connect(Uri uri, string login, string password) public Task Connect(Uri uri, string login, string password)
{ {
if (runner != null) throw new Exception("Cannot connect in this state. Should close before"); if (ws != null) throw new Exception("Cannot connect in this state. Should close before");
ws = new WebSocket(uri.ToString(), "v12.stomp");
var ct = Token.Token; var ct = Token.Token;
await ws.ConnectAsync(uri, ct);
StompFrm connect = new StompFrm(login, password); ws.OnClose += (sender, o) =>
await connect.Serialize(ws, ct); {
Token.Cancel();
};
Frame fr = await Helpers.GetFrame(ws, ct); ws.OnOpen += (sender, o) =>
{
StompFrm connect = new StompFrm(login, password);
Task.Run(async () => { await connect.Serialize(ws, ct);});
};
ExpectFrame(fr, FrameType.Connected); ws.OnMessage += (sender, o) =>
runner = Run(); // Run is async {
//Frame fr = await Helpers.GetFrame(ws, ct);
//ExpectFrame(fr, FrameType.Connected);
System.Console.WriteLine("Msg: type {0} data: {1}", o.Type.ToString(), o.ToString());
};
ws.Connect();
//runner = Run(); // Run is async
return Task.CompletedTask;
} }
/// <summary> /// <summary>
/// Reports state of conection /// Reports state of conection
@ -132,7 +147,7 @@ namespace ArStomp
/// <returns>true if it looks like we have proper connection to server</returns> /// <returns>true if it looks like we have proper connection to server</returns>
public bool IsConnected() public bool IsConnected()
{ {
return ws.CloseStatus == null; return ws.IsAlive;
} }
/// <summary> /// <summary>
/// Send message /// Send message
@ -194,7 +209,7 @@ namespace ArStomp
Frame fr = null; Frame fr = null;
try try
{ {
fr = await Helpers.GetFrame(ws, ct); //fr = await Helpers.GetFrame(ws, ct);
} }
catch (ThreadInterruptedException) catch (ThreadInterruptedException)
{ {
@ -229,18 +244,19 @@ namespace ArStomp
/// Cancel current operaton and close connection /// Cancel current operaton and close connection
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task Close() public Task Close()
{ {
try try
{ {
Token.Cancel(); Token.Cancel();
var ct = new CancellationTokenSource().Token; var ct = new CancellationTokenSource().Token;
await ws.CloseAsync(WebSocketCloseStatus.Empty, null, ct); ws.Close();
} }
catch catch
{ {
// skip // skip
} }
return Task.CompletedTask;
} }
} }
/// <summary> /// <summary>

View File

@ -1,11 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.WebSockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.IO; using System.IO;
using System.Text; using System.Text;
using WebSocketSharp;
using WebSocketSharp.Net;
namespace ArStomp namespace ArStomp
{ {
@ -68,7 +68,7 @@ namespace ArStomp
return sb.ToString(); return sb.ToString();
} }
internal Task Serialize(ClientWebSocket ws, CancellationToken cancellationToken) internal Task Serialize(WebSocket ws, CancellationToken cancellationToken)
{ {
var utf8 = Encoding.UTF8; var utf8 = Encoding.UTF8;
var EOL = utf8.GetBytes("\n"); var EOL = utf8.GetBytes("\n");
@ -100,7 +100,10 @@ namespace ArStomp
stream.Flush(); stream.Flush();
var array = stream.GetBuffer(); var array = stream.GetBuffer();
if (StompClient.Debug) Console.WriteLine(">>>\n{0}\n>>>\n", this); if (StompClient.Debug) Console.WriteLine(">>>\n{0}\n>>>\n", this);
return ws.SendAsync(new ArraySegment<byte>(array, 0, (int)stream.Position), WebSocketMessageType.Binary, true, cancellationToken); stream.Position = 0;
ws.Send(stream, (int) stream.Length, false);
return Task.CompletedTask;
//return ws.SendAsync(new ArraySegment<byte>(array, 0, (int)stream.Position), WebSocketMessageType.Binary, true, cancellationToken);
} }
} }