websocket-sharp Getting data result from client - websocket-sharp

protected override void OnMessage (MessageEventArgs e)
{
Send("Testing");
//Console.writeline("result data getting from client");
}
Hi,
I'm trying to do get a data while sending message. On above i added a row which is getting data from m client. Right now i can only send message. What i am want is for example i will send two numbers and sum them on client side and return that result on above that line i just wrote. //Console.writeline("result data getting from client");

use this code
protected override void OnMessage(MessageEventArgs e)
{
Console.WriteLine(e.Data);
Send("Received");
}

Related

Why ActivityIndicator changes state after entire method is completed?

I would like to show ActivityIndicator object after user tap the login button on page. Unfortunately there is small problem to do that because it seems like ActivityIndicator change state after entire method is completed. This is code I wrote so far:
private void Login(object sender, EventArgs ev)
{
BusyIndicator.IsVisible = true; //<- here I want to show indicator
try
{
//some input validation, connection opening etc
ConnectionHandler.OpenConnection(ServerIP, "dmg", false);
}
catch (Exception e)
{
Logging.Error(e.Message, "Connection", e);
}
}
When I set breakpoint after BusyIndicator.IsVisible = true; there is absolutely no change in app. However I noticed that when method is completed then indicator is shown. Is this a correct behavior of this control?
Why I need this? Because field validation and connecting with server takes some time and I need to show to user that something happens in background. Login function takes ~1 sec so indicator show and hide quickly I can't even see any change.
How can I show indicator immediately after user tap a button?
Your problem is that Login() method is being executed in the UI thread. So, despite setting BusyIndicator.IsVisible = true;, the thread continues tio execute the method to get data, so the UI does not respond.
Solution, run the OpenConnection in a different thread:
private async void Login(object sender, EventArgs ev)
{
BusyIndicator.IsVisible = true; //<- here I want to show indicator
try
{
//some input validation, connection opening etc
await Task.Run(() => { ConnectionHandler.OpenConnection(ServerIP, "dmg", false);});
}
catch (Exception e)
{
Logging.Error(e.Message, "Connection", e);
}
}

Independent thread in Asp.net 4.5

protected void Button1_OnClick(object sender, EventArgs e)
{
FineTuneDB();// Long Task running in db
SendSMStoAllClients();// Using Twolio API to send sms to all client long task
lblText.Text = "Button click is completed our system threads working on your request";
}
Is this possible that on button click I can response to client and independent long task going on separately.
If you don't care about whether task is completed or not, you call FineTuneDB method like this.
Action fineTuneDB = FineTuneDB;
fineTuneDB.BeginInvoke(null, null);
Asynchronous Method Invocation
Updated:
Action<int, string> fineTuneDB = FineTuneDB;
fineTuneDB.BeginInvoke((int)Session["id"],
Session["name"].ToString(), null, null);
// Your method will be like this
public void FineTuneDB(int id, string)
{
}

How to programmatically set recipient of Elmah email

I've got this method here, I wanted to go e.Mail.To = MAC, but apparently it's a read only property, which leaves me completely stumped on how I can programmatically set the recipients. Basically I want to change the to address based on my deployment level (live/test/dev) I also want to dispose() (not send) the email for dev/test modes.
Is there another way round this?
public static void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
if (!GlobalHelper.IsLiveMode)
{
e.Mail.Dispose();
}
else
{
MailAddressCollection MAC = new MailAddressCollection();
MAC.Add("A");
}
Following snippet will solve your problem -
public static void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
if (!GlobalHelper.IsLiveMode)
{
e.Mail.Dispose();
}
else
{
MailAddressCollection MAC = new MailAddressCollection();
MAC.Add("A#XYZ.COM");
MAC.Add("B#XYZ.COM");
e.Mail.To.Clear(); // Clears any existing mail addresses if you want to
e.Mail.To.Add(MAC.ToString()); // To contains A#XYZ.COM & B#XYZ.COM
}
}

Logging raw and compressed HTTP responses in ASP.NET & IIS7

Along the lines of this question I want to create a HttpModule that will do some custom logging of requests and responses for us. Using the code in the most popular answer for that question I've got a HttpModule up and running which does indeed work:
class PortalTrafficModule : IHttpModule
{
public void Dispose()
{
// Do Nothing
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
// Create and attach the OutputFilterStream to the Response and store it for later retrieval.
OutputFilterStream filter = new OutputFilterStream(context.Response.Filter);
context.Response.Filter = filter;
context.Items.Add("OutputFilter", filter);
// TODO: If required the request headers and content could be recorded here
}
private void context_EndRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
OutputFilterStream filter = context.Items["OutputFilter"] as OutputFilterStream;
if (filter != null)
{
// TODO: Log here - for now just debug.
Debug.WriteLine("{0},{1},{2}",
context.Response.Status,
context.Request.Path,
filter.ReadStream().Length);
}
}
}
(note that the OutputFilterStream class referred to in the code is in the referenced question).
However, the responses seem to be missing some HTTP headers that I see in Fiddler (like "Date") and more importantly, when I turn on compression the responses I'm logging aren't compressed whereas what I see in Fiddler is.
So my question - is it possible to log the compressed content or is this happening in a subsequent step my module can't hook in to?
For the record, I've also tried handling the PreSendRequestContent event and the response is still uncompressed.
Hi although I cannot answer your questions directly I have had cause to do similar things in the past and have found the following resource extremely helpful and enlightening. In the end I managed to acheive what I needed for raw soap headers by configuring the System.Diagnostics node within web config and create a trace log of traffic. I understand that your needs may be more granular than that however I believe this resource may still help.
http://msdn.microsoft.com/en-us/library/ms731859
Of particular interest may be the message log configuration and viewing message logs links from the one above.

Regarding implementation/usage of Background Worker

Ok, I've poked around and can't find a suitable answer to my question. I have a rather complicated code base that uses a Timer to initiate the creation of background worker to transmit something on regular intervals.
I am running into an issue when it calls ReportProgress:
"This operation has already had OperationCompleted called on it and further calls are illegal."
Stack Trace:
at System.ComponentModel.AsyncOperation.VerifyNotCompleted()
at System.ComponentModel.AsyncOperation.Post(SendOrPostCallback d, Object arg)
at System.ComponentModel.BackgroundWorker.ReportProgress(Int32 percentProgress, Object userState)
at NAME.TX.Work(Object sender, DoWorkEventArgs e) in file.cs:line 68
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
My question is relatively simple, the function called when the timer goes off creates a new background worker, but is it actually a new background worker?
My code (simplified because its very bulky):
//THIS IS THE FUNCTION CALLED BY TIMER
public void TransmitMSG(MSG msg)
{
//Initialze _tx
this.m_thread = new BackgroundWorker();
m_thread.WorkerReportsProgress = true;
//Initialize all events used by _tx
m_thread.DoWork += new DoWorkEventHandler(this.Work);
m_thread.ProgressChanged += new ProgressChangedEventHandler(this.Report);
m_thread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.Complete);
//Run the backgroundworker
m_thread.RunWorkerAsync(msg);
}
//THIS IS THE WORK FUNCTION
public override void Work(object sender, DoWorkEventArgs e)
{
//This code is in the TX Thread
//The sender should be the a BackgroundWorker
BackgroundWorker _tx = sender as BackgroundWorker;
//Check the Argument
if (e.Argument != null && e.Argument.GetType() == typeof(MSG))
{
//Transmit the Argument Message
m_THING.Transmit((MSG)e.MSG)
//Report progress according to the result
_tx.ReportProgress(CONSTANTS.PROGRESS.SUCCESS_TX, (MSG)e.Argument);
}
}
The Work function is an override because there is a higher handling class that allows for transmit and recieve, these two inherit from the same class so I can have common ReportProgress and Close methods.

Resources