C# websocket
Hello everyone! I require some help with C# websockets. While developing my bot I came across an error which closes the socket after 90-100 seconds, but only in C#... I have tried javascript in browser and nodejs along with python, which all worked perfectly fine. Yes I have tried the C# code sample and I need it in C#. The exception I am receiving after 90-100 seconds is The remote party closed the WebSocket connection without completing the close handshake.
So I'm wondering if the error is on my end or the server end. My .NET version is 4.6. Any suggestions would be appreciated!private ClientWebSocket ws = new ClientWebSocket(); private Uri uri = new Uri("wss://ws.binaryws.com/websockets/v3"); public async Task SendRequest(string data) { while (this.ws.State == WebSocketState.Connecting) { }; if (this.ws.State != WebSocketState.Open) { throw new Exception("Connection is not open."); } var reqAsBytes = Encoding.UTF8.GetBytes(data); var ticksRequest = new ArraySegment<byte>(reqAsBytes); await this.ws.SendAsync(ticksRequest, WebSocketMessageType.Text, true, CancellationToken.None); Console.WriteLine("The request has been sent: "); Console.WriteLine(data); Console.WriteLine("\r\n \r\n"); } public async Task StartListen() { WebSocketReceiveResult result; while (this.ws.State == WebSocketState.Open) { var buffer = new ArraySegment<byte>(new byte[1024]); try { result = await this.ws.ReceiveAsync(new ArraySegment<byte>(buffer.Array), CancellationToken.None); if (result.MessageType == WebSocketMessageType.Close) { Console.WriteLine("Connection Closed!"); break; } else { var str = Encoding.UTF8.GetString(buffer.Array, 0, result.Count); Console.WriteLine("Received Data at: " + DateTime.Now); Console.WriteLine(str); Console.WriteLine("\r\n"); } } catch (Exception e) { Console.WriteLine(e.Message); } } } public async Task Connect() { Console.WriteLine("Prepare to connect to: " + this.uri.ToString()); Console.WriteLine("\r\n"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; await ws.ConnectAsync(uri, CancellationToken.None); Console.WriteLine("The connection is established!"); Console.WriteLine("\r\n"); } static void Main(string[] args) { string data = "{\"subscribe\": 1, \"proposal\": 1,\"amount\": \"25\", \"basis\": \"payout\", \"contract_type\": \"CALL\", \"currency\": \"USD\", \"duration\": \"120\", \"duration_unit\": \"s\", \"symbol\": \"frxEURUSD\"}"; var bws = new BinaryWS(); bws.Connect().Wait(); bws.SendRequest(data).Wait(); bws.StartListen(); Console.ReadLine(); } }
Comments
Hello. to keep your connection alive send a Ping before that time has been elapsed.
Regards.
Hi!
try use this.
WebSocket4Net automatically send Ping because by default EnableAutoSendPing is enabled. But Is a good idea to use some websocket library is comfortable to work with.
Regards
I´m facing the exact same problem, and even sending a Ping every 30 seconds websocket connection still times out after about 200 seconds from my first message. It seems Ping has no effect in closing connection open in C#. Funny thing is, as Saxo noted above, Java has no problem with it. It seems the API Ping is not enough to keep the underlying IP connection open in C#.
I´m curious about how Saxo fixed it. Any suggestion?
I have ended up using what Dimon suggested. WebSocket4Net
Thank you very much for your feedback! Gonna try that.
We have just hit the same issue with System.Net.WebSockets and have got to the bottom of it.
The problem is caused by the WebSocket connection being handled by ServicePointManager which causes a MaxIdleTime property with a default of 100 seconds to be applied to the underlying connection. But it appears that there is a .Net framework bug which doesn't recognise the WebSockets traffic, so the socket is treated as idle after the initial HTTP GET request used in establishing the connection.
I posted more details about this here.
this app can download at https://drive.google.com/open?id=0B8Pa0AHp6qgNQWRPZzl5TVZzdkk
Contact Details
alsbinary@gmail.com
Alsbinary, you're just going to continue to embed advertisements about the robot in all forum topics? this robot is implemented in a binary bot, what is the meaning of your robot?