C# ClientWebSocket retry after connection error - c#

Is there any way to know whether ClientWebSocket connection is closed/disconnected? I am using slack RTM api in C# and sometimes due to connectivity issues connection is closed from server, then I want to retry connection.
I am connecting to websocket server like this
var client = new ClientWebSocket();
client.ConnectAsync(url, token);

Related

Get Data from WebSocket using .Net ClientWebSocket

I need to collect data from a WebSocket.
If I try to test the websocket with this site: http://www.websocket.org/echo.html I can connect and send message without problem but when I try to connect to the websocket using .Net Framework libraries I get:
System.Net.WebException: Internal Server Error (500).
The code I use to connect is this:
string uri = "wss://wss.xxx.xxx";
ClientWebSocket webSocket = null;
try
{
webSocket = new ClientWebSocket();
await webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
await Task.WhenAll(Receive(webSocket), Send(webSocket));
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex);
}
If I try to connect to any other web socket, the code works well...
The difference with the other websocket is that the address starts with "wss" instead of "ws" ( think its because of secure connection).
If I test with a browser like Chrome the websocket connetion works, but not in .Net, may be I have to add some header in order to simulate a Browser Agent ?
Thanks to support
Finally After some search I find out the problem: I just had to add headers to the ClientWebSocket.
The problem is that ClientWebSocket doesn't provide methods to add specific headers and I had to make a force of ClientWebSocket.
This thread help me a lot to solve my problem:
Setting "User-Agent" HTTP header in ClientWebSocket
Using this new version of ClientWebSocket I was able to add Headers.

Socrata : An existing connection was forcibly closed by the remote host

I created an application that periodically(every 1st Sunday of the month) send request to data.cms.gov to check for pecos registered physician. The code went well, however it stopped working a few days ago. I'm getting this response "Unable to read data from transport connection. An existing connection was forcibly closed by the remote host." Who encounter this before or can somebody help with this? I use the code below for my request
string end_point = "https://data.cms.gov/resource/qcn7-gc3g.json?$$app_token=myapp_token&npi=";
string cms_uri = end_point + npi;
System.Net.WebClient cms_wc = new System.Net.WebClient();
byte[] bResponse = cms_wc.DownloadData(cms_uri);
string cms_response = System.Text.Encoding.ASCII.GetString(bResponse);
As a security upgrade, we've disabled TLS 1.0 as an allowable SSL protocol. My guess is that that's what's triggering your disconnect.
https://support.socrata.com/hc/en-us/articles/235267087
You'll need to instruct .NET to to use TLS 1.1 or 1.2. You should be able to do that by adding the following before you create your client:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
There's some more info in this issue on one of the C# .NET libraries for the SODA API.

Console app SignalR client Disconnect Timeout

I am trying to set Disconnect Timeout to higher value from the default 30s.
All examples on web are more JS oriented.
var hubConnection = new HubConnection("http://localhost:8087");
var testHubProxy = hubConnection.CreateHubProxy("TestHub");
Error: System.TimeoutException: Couldn't reconnect within the configured timeout of 00:00:30, disconnecting.
This did not work:
GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(35);
Update:
It looks like DisconnectTimeout needs to be set on the server side!?
What is the reason for disallowing different clients to have different Disconnect Timeout?
Disconnect Timeout is configured on server-side. The main reasons could be as follows:
We know the server may take some N-time units to respond so that the all clients may be well aware.
The server should be pinging the clients for connection at regular times. So the server is aware of clients connection and can manage other hubs and eradicate the expired connections from its connection pool.
The client is not supposed to set disconnect timeout because it does not know when could it shutdown e.g. the internet switched off accidentally on client side than the client is not able to tell server that I am not going to connect to you again. Yes but we have some events at client-side which tells it that it is not connected to the signalr hub anymore. Please see the reconnecting and disconnected events.
Summary:
Disconnect timeout is to inform the server that its client is not connected anymore even if it disconnects disgracefully .

Unable to connect to the remote server using ClientWebSocket

I'm trying to connect to a web socket server on localhost:9222 using this C# code:
var ws = new ClientWebSocket();
var uri = new Uri("ws://localhost:9222/X/Y/Z", UriKind.Absolute);
await ws.ConnectAsync(uri, CancellationToken.None);
On that last line, I'm getting Unable to connect to the remote server, with inner exception The remote server returned an error: (404) Not Found..
However, I have this JavaScript that connects to the same WebSocket:
var ws = new WebSocket("ws://localhost:9222/X/Y/Z");
And it works great in both IE and Chrome.
I'm not using any proxies.
Is there something I've missed to configure in the C# code?
How can I debug this?
Update:
Using WebSocket4Net instead of the .NET 4.5 ClientWebSocket, I'm getting HTTP 1.1 500: Internal Server Error. However, I'm not sure how I can debug this either.

Closing A SOAP Connection

I've made soap connection to a server and the server doesn't seem to be dropping that connection, in netstat the status of the connection is listed as CLOSE WAIT.
I'm told that the client that created the soap connection has to send a command to the server to close the connection. Can anyone tell me the correct way to do this in C#? Below is an example piece of code.
SOAPServer.Service Soap = new SOAPServer.Service(); // SOAPServer is a web reference
Soap.Timeout = 30000;
string[] SOAPReturnResult = Soap.DepotData(100, "Test");
Soap.Dispose();
Wrap it in a using block.
using (SOAPServer.Service Soap = new SOAPServer.Service())
{
Soap.Timeout = 30000;
string[] SOAPReturnResult = Soap.DepotData(100, "Test");
}
Note that this only marks the connection as closed. It might still show up as open via netstat even for a little time after the connection has been closed.
CLOSE WAIT state means that the server received a TCP-FIN from your client (i.e. a passive close), the server has to close its socket (send TCP-FIN to the client) in order to get the server socket out of CLOSE WAIT state. So this may not necessary be a problem on the client side but on the server side where the server socket does not get closed correctly.
What does it look like on the client side? FIN-WAIT-1 or FIN-WAIT-2 could indicate this may be the issue.

Categories