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

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

Related

SSIS Script Task using client.GetAsync(url) not waiting for response

I have an API call using client.GetAsync(url) within a SSIS script task but for some reason its not waiting for response from API and jumping back to the entry point for the script task which is public void Main(). Done some reading and found out that it might result in a deadlock for some reason but tried all the variations I could find to get it to work but with no luck. Something else that I don't understand is the exact same code is running on a webpage and that works perfect and waits for response from the api and continuing the flow.
Script Task entry point
The response here for payload is: ID =5, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
Here if in debug mode and moving the process back to go through the process again I noticed there 2 threads one current executing and the old one with the response I was expecting on the first call but not too sure what this means.
public void Main() {
// TODO: Add your code here
try {
PackageDetails packageInfo = new PackageDetails {
PackageNumber = 1234567891, Env = "Development", UserName = "USER"
};
var payload = API.ListHeadByPackAsync(packageInfo);
//var test = GetResponse();
Dts.TaskResult = (int) ScriptResults.Success;
} catch (Exception ex) {
System.Console.Write(ex.Message);
Dts.TaskResult = (int) ScriptResults.Failure;
}
}
API Call
public static class API {
public static async Task<PackageDetails> ListHeadByPackAsync(PackageDetails package) {
PackageDetails packageInfo = new PackageDetails();
try {
using(var client = new ApiClient(requestUrl, authToken)) {
var response = await client.GetAsync(); //-> not waiting for response
}
} catch (Exception err) {
switch (err.Message) {
//TODO:
}
}
return packageInfo;
}
}
Client
public class ApiClient: IDisposable {
private readonly TimeSpan _timeout;
private HttpClient _httpClient;
private HttpClientHandler _httpClientHandler;
private readonly string _baseUrl;
private readonly string _credentials;
//private const string MediaTypeXml = "application/csv";
public ApiClient(string baseUrl, string authToken, TimeSpan ? timeout = null) {
_baseUrl = baseUrl;
_credentials = Base64Encode(authToken);
_timeout = timeout ?? TimeSpan.FromSeconds(90);
}
public async Task < string > GetAsync() {
EnsureHttpClientCreated();
using(var response = await _httpClient.GetAsync(_baseUrl).ConfigureAwait(continueOnCapturedContext: false))
//-> after executing above line it will go straight to public void Main(), dose not wait for response
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
public void Dispose() {
_httpClientHandler?.Dispose();
_httpClient?.Dispose();
}
private void CreateHttpClient() {
_httpClientHandler = new HttpClientHandler {
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
};
_httpClient = new HttpClient(_httpClientHandler, false) {
Timeout = _timeout
};
if (!string.IsNullOrWhiteSpace(_baseUrl)) {
_httpClient.BaseAddress = new Uri(_baseUrl);
}
_httpClient.DefaultRequestHeaders.Add("Authorization", "Basic" + " " + _credentials);
}
private void EnsureHttpClientCreated() {
if (_httpClient == null) {
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
CreateHttpClient();
}
}
public static string Base64Encode(string token) {
var tokenBytes = System.Text.Encoding.UTF8.GetBytes(token);
return System.Convert.ToBase64String(tokenBytes);
}
}

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

Programmatically trigger listener.GetContext()

Is it possible to trigger the below code by using a trigger URL?
As opposed to triggering by visiting the URL in the browser.
var context = listener.GetContext();
Something like this?
var triggerURL = "http://www.google.ie/";
var request = (HttpWebRequest)WebRequest.Create(triggerURL);
Or is it possible to use a do while loop? I.E do create trigger while get context
Instead of using listener.GetContext(), I was able to satisfy my requirement by using listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener) and listener.EndGetContext(result), utilising the Asynchronous call, GetAsync.
public static string RunServerAsync(Action<string> triggerPost)
{
var triggerURL = "";
CommonCode(ref triggerURL);
if (listener.IsListening)
{
triggerPost(triggerURL);
}
while (listener.IsListening)
{
var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
context.AsyncWaitHandle.WaitOne(20000, true); //Stop listening after 20 seconds (20 * 1000).
listener.Close();
}
return plateString;
}
private static async void TriggerURL(string url)
{
var r = await DownloadPage(url);
}
static async Task<string> DownloadPage(string url)
{
using (var client = new HttpClient())
{
using (var r = await client.GetAsync(new Uri(url)))
{
if (r.IsSuccessStatusCode)
{
string result = await r.Content.ReadAsStringAsync();
return result;
}
else
{
return r.StatusCode.ToString();
}
}
}
}
private static void ListenerCallback(IAsyncResult result)
{
try
{
HttpListener listener = (HttpListener)result.AsyncState;
// Use EndGetContext to complete the asynchronous operation.
HttpListenerContext context = listener.EndGetContext(result);
if (context != null)
{
plateString = ProcessRequest(context);
}
else
{
plateString = "No response received!";
}
}
catch (Exception ex)
{
NLogManager.LogException(ex);
}
}

How I can get the AspNetWebSocketContext from the current context?

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.

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