How I can get the AspNetWebSocketContext from the current context? - c#

I am implementing ASP.net websocket server using Microsoft webscoket. I have a function which sends data to the client. Below is the class.
public class MyWebSocketHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.IsWebSocketRequest)
{
context.AcceptWebSocketRequest(DoTalking);
}
}
public bool IsReusable
{
get
{
return false;
}
}
public async Task DoTalking(AspNetWebSocketContext context)
{
WebSocket socket = context.WebSocket;
while (true)
{
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None);
if (socket.State == WebSocketState.Open)
{
string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
userMessage = "Message from client : " + userMessage;
}
else
{
break;
}
}
}
public async Task SendToClient(AspNetWebSocketContext context)
{
WebSocket socket = context.WebSocket;
//WebSocket socket = context.WebSocket;
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(message));
await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
public void callSendToClient()
{
var task = SendToClient(Problem is here!);
task.Wait();
}
private string message { get; set; }
}
}
My problem is how to get the AspNetWebSocketContext when I call the SendToClient method ? I am stuck at this point. Actually I want to call SendToClient() method from another .aspx page. Need some help.

Related

How do you implement a custom ConnectionHandler for aspnetcore?

I have looked through samples in the github repo, but when i develop my process, i get "Connection ID required" when accessing the route that is mapped to a custom ConnectionHandler. My log message is never printed, nor do i land in the implementation with the debugger.
Startup:
builder.Services.AddConnections();
app.UseEndpoints(endpoints =>
{
endpoints.MapConnectionHandler<CustomDelegationHandler>("/proxy/{id}");
});
Implementation:
public class CustomDelegationHandler : ConnectionHandler
{
private readonly ILogger<CustomDelegationHandler> _logger;
public CustomDelegationHandler(ILogger<CustomDelegationHandler> logger)
{
_logger = logger;
}
public override async Task OnConnectedAsync(ConnectionContext connection)
{
_logger.LogWarning("Connection incoming");
while (true)
{
var result = await connection.Transport.Input.ReadAsync();
var buffer = result.Buffer;
try
{
if (!buffer.IsEmpty)
{
var stream = new MemoryStream();
var data = buffer.ToArray();
await stream.WriteAsync(data, 0, data.Length);
stream.Position = 0;
}
else if (result.IsCompleted)
{
break;
}
}
finally
{
connection.Transport.Input.AdvanceTo(buffer.End);
}
}
}
}
You need add the connection like below and it will map the custom ConnectionHandler:
public async Task<IActionResult> Index()
{
var url = "https://localhost:yourPortNumber/proxy/1";
var connection = new HttpConnection(new Uri(url));
await connection.StartAsync();
var bytes = Encoding.UTF8.GetBytes("aaaa");
async Task SendMessage()
{
await connection.Transport.Output.WriteAsync(bytes);
}
// Send the receive concurrently so that back pressure is released
// for server -> client sends
await SendMessage();
return View();
}

Named Pipes not working for asynchronous duplex communication

Somehow I am not able to get named pipes work for duplex communication between client and server for .Net app.
Duplex communication works fine when I go for sequential messaging, but when I make it random i.e., server and client can ping each other at any random time, it just doesn't work.
The other posts are not much helpful either - c# Full Duplex Asynchronous Named Pipes .NET
I am attaching my code as below:
-Server code:
namespace Server
{
class Program
{
static void Main(string[] args)
{
var namedPipeServerStream = new NamedPipeServerStream("myPipe",
PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
Task.Run(() => StartListeningAsync(namedPipeServerStream, (msg) => Console.WriteLine(msg)));
Task.Run(() => SendMessageAsync(namedPipeServerStream));
Console.ReadLine();
}
public static async Task SendMessageAsync(NamedPipeServerStream namedPipeServer)
{
using (var stream = new StreamWriter(namedPipeServer))
{
while (true)
{
await Task.Delay(2000);
try
{
var serialized = JsonConvert.SerializeObject($"Server {DateTime.Now}");
byte[] messageBytes = Encoding.UTF8.GetBytes(serialized);
if (!namedPipeServer.IsConnected)
{
namedPipeServer.WaitForConnection();
Console.WriteLine("Client connected");
}
await namedPipeServer.WriteAsync(messageBytes, 0, messageBytes.Length);
await namedPipeServer.FlushAsync();
namedPipeServer.WaitForPipeDrain();
}
catch (Exception exception)
{
Console.WriteLine($"Exception:{exception}");
}
}
}
}
public static async Task StartListeningAsync(NamedPipeServerStream namedPipeServer, Action<string> messageRecieved)
{
while (true)
{
try
{
StringBuilder messageBuilder = new StringBuilder();
string messageChunk = string.Empty;
byte[] messageBuffer = new byte[1024];
do
{
if (!namedPipeServer.IsConnected)
{
namedPipeServer.WaitForConnection();
Console.WriteLine("Client connected");
}
await namedPipeServer.ReadAsync(messageBuffer, 0, messageBuffer.Length);
messageChunk = Encoding.UTF8.GetString(messageBuffer);
messageBuilder.Append(messageChunk);
messageBuffer = new byte[messageBuffer.Length];
} while (!namedPipeServer.IsMessageComplete);
if (messageRecieved != null)
{
messageRecieved(JsonConvert.DeserializeObject<string>(messageBuilder.ToString()));
}
}
catch (Exception exception)
{
Console.WriteLine($"Exception:{exception}");
}
}
}
}
}
Client code:
namespace Client
{
class Program
{
static void Main(string[] args)
{
var namedPipeClientStream = new NamedPipeClientStream(".", "server", PipeDirection.InOut, PipeOptions.Asynchronous,
TokenImpersonationLevel.Impersonation);
Task.Run(() => StartListeningAsync(namedPipeClientStream, (msg) => Console.WriteLine(msg)));
Task.Run(() => SendMessageAsync(namedPipeClientStream));
Console.ReadLine();
}
public static async Task SendMessageAsync(NamedPipeClientStream namedPipeClient)
{
using (var stream = new StreamWriter(namedPipeClient))
{
while (true)
{
try
{
await Task.Delay(3000);
var serialized = JsonConvert.SerializeObject($"Client {DateTime.Now}");
byte[] messageBytes = Encoding.UTF8.GetBytes(serialized);
if (!namedPipeClient.IsConnected)
{
namedPipeClient.Connect();
namedPipeClient.ReadMode = PipeTransmissionMode.Message;
Console.WriteLine("Client connected");
}
await namedPipeClient.WriteAsync(messageBytes, 0, messageBytes.Length);
await namedPipeClient.FlushAsync();
namedPipeClient.WaitForPipeDrain();
}
catch (Exception exception)
{
Console.WriteLine($"Exception:{exception}");
}
}
}
}
public static async Task StartListeningAsync(NamedPipeClientStream namedPipeClient, Action<string> messageRecieved)
{
using (var streamReader = new StreamReader(namedPipeClient))
{
while (true)
{
try
{
StringBuilder messageBuilder = new StringBuilder();
string messageChunk = string.Empty;
byte[] messageBuffer = new byte[1024];
do
{
if (!namedPipeClient.IsConnected)
{
namedPipeClient.Connect();
namedPipeClient.ReadMode = PipeTransmissionMode.Message;
}
await namedPipeClient.ReadAsync(messageBuffer, 0, messageBuffer.Length);
messageChunk = Encoding.UTF8.GetString(messageBuffer);
messageBuilder.Append(messageChunk);
messageBuffer = new byte[messageBuffer.Length];
} while (!namedPipeClient.IsMessageComplete);
if (messageRecieved != null)
{
messageRecieved(JsonConvert.DeserializeObject<string>(messageBuilder.ToString()));
}
}
catch (Exception exception)
{
Console.WriteLine($"Exception:{exception}");
}
}
}
}
}
}

Azure App Service- GZip Compression in Request

With this code (without the request compression part) I'm able to get gzip compressed content from Azure App Service (Xamarin.Froms App with offline sync). But when i try to gzip the request http-content i get a "Bad Request".
Any ideas? Is it possible to gzip the request content with Azure App Service?
namespace XXX.XXX.XXX.XXX.XXX
{
public class HttpGZipClientHandler : System.Net.Http.HttpClientHandler
{
long time = 0;
private long _downloadedBytesFromServer;
private long _downloadedProcessedBytes;
private long _intendedUploadedBytesToServer;
private long _uploadedBytesToServer;
private long _additionalTimeOverhead = 0;
public override bool SupportsAutomaticDecompression { get { return true; } }
public long DownloadedBytesFromServer { get { return _downloadedBytesFromServer; } }
public long DownloadedProcessedBytes { get { return _downloadedProcessedBytes; } }
public long IntendedUploadedBytesToServer { get { return _intendedUploadedBytesToServer; } }
public long UploadedBytesToServer { get { return _uploadedBytesToServer; } }
public long AdditionalTimeOverhead { get { return _additionalTimeOverhead; } }
public void ResetStatistics()
{
_downloadedBytesFromServer = 0;
_downloadedProcessedBytes = 0;
_intendedUploadedBytesToServer = 0;
_uploadedBytesToServer = 0;
_additionalTimeOverhead = 0;
}
protected override async Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
//Save content headers before compressing
System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>> savedContentHeaders = new Dictionary<string, IEnumerable<string>>();
foreach (System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> keyValue in request.Content.Headers)
{
savedContentHeaders.Add(keyValue.Key, keyValue.Value);
}
//Compress request content
System.Diagnostics.Stopwatch sp1 = new System.Diagnostics.Stopwatch();
sp1.Start();
_intendedUploadedBytesToServer += request.Content.Headers.ContentLength.HasValue ? request.Content.Headers.ContentLength.Value : 0;
await request.Content.LoadIntoBufferAsync().ConfigureAwait(false);
request.Content = new HttpGZipContent(await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false), System.IO.Compression.CompressionMode.Compress);
byte[] uploadedBytes = await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
_uploadedBytesToServer += uploadedBytes.Length;
sp1.Stop();
_additionalTimeOverhead += sp1.ElapsedMilliseconds;
//Set headers
foreach (System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> keyValue in savedContentHeaders)
{
request.Content.Headers.Add(keyValue.Key, keyValue.Value);
}
request.Headers.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
request.Content.Headers.Add("Content-Encoding", "gzip");
//Execute request
System.Net.Http.HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
_downloadedBytesFromServer += response.Content.Headers.ContentLength.HasValue ? response.Content.Headers.ContentLength.Value : 0;
//Decompress response content
if (response.Content.Headers.ContentEncoding.Contains("gzip"))
{
System.Diagnostics.Stopwatch sp2 = new System.Diagnostics.Stopwatch();
sp2.Start();
await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
response.Content = new HttpGZipContent(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false), System.IO.Compression.CompressionMode.Decompress);
byte[] processedBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
_downloadedProcessedBytes += processedBytes.Length;
sp2.Stop();
_additionalTimeOverhead += sp2.ElapsedMilliseconds;
}
else
_downloadedProcessedBytes += response.Content.Headers.ContentLength.HasValue ? response.Content.Headers.ContentLength.Value : 0;
return response;
}
}
internal sealed class HttpGZipContent : System.Net.Http.HttpContent
{
private readonly byte[] _content;
private readonly System.IO.Compression.CompressionMode _compressionMode;
public HttpGZipContent(byte[] content, System.IO.Compression.CompressionMode compressionMode)
{
_compressionMode = compressionMode;
_content = content;
}
protected override async System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext context)
{
if (_compressionMode == System.IO.Compression.CompressionMode.Compress)
{
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(_content.Length))
{
using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(memoryStream, System.IO.Compression.CompressionMode.Compress))
{
zipStream.Write(_content, 0, _content.Length);
zipStream.Flush();
}
byte[] compressed = memoryStream.ToArray();
System.IO.MemoryStream copyStream = new System.IO.MemoryStream(compressed);
await copyStream.CopyToAsync(stream).ConfigureAwait(false);
}
}
else
{
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(_content, 0, _content.Length))
{
using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(memoryStream, System.IO.Compression.CompressionMode.Decompress))
{
await zipStream.CopyToAsync(stream).ConfigureAwait(false);
}
}
}
}
protected override bool TryComputeLength(out long length)
{
length = _content.Length;
return true;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
}
}
Based on my understanding, you need to implement the request decompression for your mobile app back-end. If you are using the C# backend, you could create your custom ActionFilterAttribute as follows:
public class RequestDeCompressFilter : ActionFilterAttribute
{
public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
var request = actionContext.Request;
if (request.Content.Headers.ContentEncoding.Contains("GZIP"))
{
await request.Content.LoadIntoBufferAsync().ConfigureAwait(false);
request.Content = new HttpGZipContent(await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false), System.IO.Compression.CompressionMode.Decompress);
}
//TODO: compress the response, you could follow http://www.intstrings.com/ramivemula/articles/jumpstart-47-gzipdeflate-compression-in-asp-net-mvc-application/
await base.OnActionExecutingAsync(actionContext, cancellationToken);
}
public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
//you could also compress the response here
return base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
}
}
Then, mark your action as follows:
[RequestDeCompressFilter]
public async Task<IHttpActionResult> PostMessage(Message item)
Also, you could follow HTTP Message Handlers in ASP.NET Web API to implement your HTTP message handler.

Xamarin ClientWebSocket... Can't re-connect after connection lost

I am developing Android app using ClientWebSocket.
When I start the app, everything works correctly if the server is running.
After I stop the server, the client can't be re-connected to the server and can't receive messages. I noticed that the status of clientwebsocket remains "Open" after I stop the server!
If I start the app then start the server, the client can't connect!
This is the code:
string ip = "192.168.1.142";
int port = 9000;
private const int ReceiveChunkSize = 1024;
private const int SendChunkSize = 1024;
private ClientWebSocket _ws;
private readonly Uri _uri;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private readonly CancellationToken _cancellationToken;
Preparing the client:
_ws = new ClientWebSocket ();
_ws.Options.KeepAliveInterval = TimeSpan.FromSeconds (20);
_uri = new Uri ("ws://"+ip+":"+port);
_cancellationToken = _cancellationTokenSource.Token;
Connection Method:
public async void ConnectAsync()
{
await Task.Run (() => {
try {
var tk = _ws.ConnectAsync (_uri, _cancellationTokenSource.Token);
tk.Wait(4000);
if (tk.IsCompleted) {
//CallOnConnected ();
StartListen ();
}
}
catch
{
}
});
}
Listen for income messages:
private async void StartListen()
{
var buffer = new byte[ReceiveChunkSize];
try
{
while (_ws.State == WebSocketState.Open)
{
var stringResult = new StringBuilder();
WebSocketReceiveResult result;
do
{
result = await _ws.ReceiveAsync(new ArraySegment<byte>(buffer), _cancellationToken);
if (result.MessageType == WebSocketMessageType.Close)
{
await
_ws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
//CallOnDisconnected();
}
else
{
var str = Encoding.UTF8.GetString(buffer, 0, result.Count);
stringResult.Append(str);
}
} while (!result.EndOfMessage);
//CallOnMessage(stringResult);
}
}
catch (Exception)
{
//CallOnDisconnected();
}
finally
{
_ws.Dispose();
}
}
In the MainActivity, I use a timer for re-connect:
void Try_Connect(){
if ((my_clientwebsocket.Connection_Status == System.Net.WebSockets.WebSocketState.Open)|(my_clientwebsocket.Connection_Status == System.Net.WebSockets.WebSocketState.Connecting)) {
return;
}
new Thread (new ThreadStart (delegate {
RunOnUiThread (() => {
try {
my_clientwebsocket.ConnectAsync();
} catch {
}
});
})).Start ();
}
What is the wrong?!
Thanks for help

ServerSentEvents, HttpTaskAsyncHandler and waiting

I'm trying to create a quite simple notifications system (don't want to use SignalIR or something else). I have the following testing code:
Client side:
var source = new EventSource('/notifications.axd');
source.onopen = function () {
Console.log("Connection open");
};
source.onerror = function () {
Console.log("Connection error");
};
source.onmessage = function (event) {
Console.log("Message: " + event.data);
};
Server side:
public class NotificationMessage {
public NotificationMessage() {
Id = Guid.NewGuid().ToString();
}
public string Id { get; private set; }
}
public class NotificationsHandler : HttpTaskAsyncHandler {
private const string CONTENT_TYPE = "text/event-stream";
private sealed class NotificationItem {
public ConcurrentQueue<NotificationMessage> Messages;
public CancellationTokenSource CancellationTokenSource;
}
private static readonly ConcurrentDictionary<string, NotificationItem> _tasks =
new ConcurrentDictionary<string, NotificationItem>();
public static void Notify(string hostId, string userId, NotificationMessage message) {
NotificationItem item;
if (!_tasks.TryGetValue(string.Format("{0}|{1}", hostId, userId), out item)) {
return;
}
var tokenSource = item.CancellationTokenSource;
item.Messages.Enqueue(message);
item.CancellationTokenSource = new CancellationTokenSource();
tokenSource.Cancel();
}
public override async Task ProcessRequestAsync(HttpContext context) {
HttpRequest request = context.Request;
NotificationItem item = _tasks.GetOrAdd(
string.Format("{0}|{1}", request.Url.Host, CsSession.Data.CurrentUser.Id),
k => new NotificationItem {
Messages = new ConcurrentQueue<NotificationMessage>(),
CancellationTokenSource = new CancellationTokenSource()
}
);
HttpResponse response = context.Response;
response.ContentType = CONTENT_TYPE;
response.CacheControl = "no-cache";
response.ContentEncoding = Encoding.UTF8;
response.AppendHeader("connection", "keep-alive");
response.BufferOutput = false;
bool supportsAsyncFlush = response.SupportsAsyncFlush;
bool shouldPing = true;
while (response.IsClientConnected) {
try {
NotificationMessage message = null;
if ((!item.Messages.IsEmpty && item.Messages.TryDequeue(out message)) || shouldPing) {
response.Write(string.Format("data:{0}\n\n", message == null ? "{}" : JsonMapper.Serialize(message)));
if (supportsAsyncFlush) {
await Task.Factory.FromAsync(response.BeginFlush, response.EndFlush, null);
} else {
response.Flush();
}
}
} catch (Exception) {
break;
}
var delay = Task.Delay(15000, item.CancellationTokenSource.Token);
await delay;
shouldPing = delay.Status == TaskStatus.RanToCompletion;
}
}
}
The problem is: the above doesn't works. I have two issues:
1) When the client connects, I receive an empty packet (that's ok). Then, if I don't enqueue any messages, after awaiting the Task.Delay, the loop tries to write an empty message again, but I don't know where. The response.Write line never returns (and nothing is being received on the client).
2) If I write to the queue, for some reason the connection is dropped. If I put a breakpoint on the line after the await delay, that line is never executed (while my logic says otherwise :) ). If I cancel the token, the delay task should quit, but it seems it is aborting the whole handler??

Categories