Coder Social home page Coder Social logo

pingmantools / system.net.websockets.client.managed Goto Github PK

View Code? Open in Web Editor NEW
43.0 6.0 13.0 80 KB

Microsoft's managed implementation of System.Net.WebSockets.ClientWebSocket tweaked for use on Windows 7 and .NET 4.5

License: MIT License

C# 100.00%
websockets websocket-client websocketsharp clientwebsocket

system.net.websockets.client.managed's Introduction

System.Net.WebSockets.Client.Managed

Microsoft's managed implementation of System.Net.WebSockets.ClientWebSocket tweaked for use on Windows 7 and .NET 4.5


From Microsoft's ClientWebSocket documentation

Some of the classes and class elements in the System.Net.WebSockets namespace are supported on Windows 7, Windows Vista SP2, and Windows Server 2008. However, the only public implementations of client and server WebSockets are supported on Windows 8 and Windows Server 2012. The class elements in the System.Net.WebSockets namespace that are supported on Windows 7, Windows Vista SP2, and Windows Server 2008 are abstract class elements. This allows an application developer to inherit and extend these abstract class classes and class elements with an actual implementation of client WebSockets.

In other words: on a Windows 7 machine calling new System.Net.WebSockets.ClientWebSocket() throws a PlatformNotSupportedException.

Thankfully Microsoft did implement that abstract class in managed code for use on non-Windows systems! But its only available for .NET 4.6+

This project is the managed System.Net.WebSockets.Client code with a few tweaks to work on .NET 4.5.

The code was taken from the CoreFX release/2.0.0 branch on Nov 28th, 2017:


Most the tweaks required are in the added files NET45Shims.cs and SR.cs, with a few changes to the original source when extensions methods could not be leveraged (NET46-only properties and statics).

The only actual NET 4.6+ features used were some Task helpers (Task.FromException, Task.FromCanceled, Task.CompletedTask) and the Socket.ConnectAsync task. Microsoft could easily fix these and provide an official nuget package like this to support Win7 and .NET 4.5.

Install

Nuget package as System.Net.WebSockets.Client.Managed

PM> Install-Package System.Net.WebSockets.Client.Managed

Usage

System.Net.WebSockets.SystemClientWebSocket class has some helpers for easily creating a ClientWebSocket that will work on the current system.

// Creates a ClientWebSocket that works for this platform. Uses System.Net.WebSockets.ClientWebSocket if supported or System.Net.WebSockets.Managed.ClientWebSocket if not.
public static WebSocket SystemClientWebSocket.CreateClientWebSocket() { ... }

// Creates and connects a ClientWebSocket that works for this platform. Uses System.Net.WebSockets.ClientWebSocket if supported or System.Net.WebSockets.Managed.ClientWebSocket if not.
public static async Task<WebSocket> ConnectAsync(Uri uri, CancellationToken cancellationToken)

If you know you want a managed instance than use new System.Net.WebSockets.Managed.ClientWebSocket() rather than new System.Net.WebSockets.ClientWebSocket()

system.net.websockets.client.managed's People

Contributors

zone117x avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

system.net.websockets.client.managed's Issues

Unable to establish websocket connection with IP address.

we are trying to establish a socket connection with DNS. we can able to establish a connection successfully,
same way if we are trying to establish a connection with the Ip address we are getting errors like below

occuredSystem.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at System.Net.WebSockets.Managed.WebSocketHandle.d__24.MoveNext()
at System.Net.WebSockets.Managed.WebSocketHandle.d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at System.Net.WebSockets.Managed.ClientWebSocket.d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at TestSocketCommunication.Program.EstablishingConnection() in C:\Users\310257816\Source\Repos\TestWebSocketClient\TestSockeCommunication\Program.cs:line 129

we have added the ceritifcatevalidationcallback like this
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };

still we are getting above error. team kindly suggest is there any way to overcome this issue. we are using System.Net.WebSockets.Client.Managed library.

v1.0.9 does not run on windows 7

When called ConnectAsync, it raises ArgumentNullException.
It is because ClientWebSocket.Options.Cookies is null.
On v1.0.4, there was no problem on ConnectAsync.

How should we set ClientWebSocket options?

I bet CreateClientWebSocket returns a WebSocket because both Managed.ClientWebSocket and ClientWebSocket inherits from WebSocket.

The problem is that it hides the Options property. If I want to set the KeepAliveInterval should I do something like this?

var client = SystemClientWebSocket.CreateClientWebSocket();
if (client is System.Net.WebSockets.Managed.ClientWebSocket managed)
{
    managed.Options.KeepAliveInterval = TimeSpan.FromSeconds(0);
    await managed.ConnectAsync(uri, cancellationToken);
}
else
{
    var coreSocket = client as ClientWebSocket;
    coreSocket.Options.KeepAliveInterval = TimeSpan.FromSeconds(0);
    await coreSocket.ConnectAsync(uri, cancellationToken);
}

If think one option would be passing the Options as an argument to the ConnectAsync method.

The connection keeps dropping

I use this library to continuously receive data from a Websocket server and the library keep crashing with the following error:

{"The I/O operation has been aborted because of either a thread exit or an application request"}
The native error code is 995

I use a Python client with the server and it works fine.

unable to ignore certificate check

I am in the development of creating a client app to connect to web socket server in windows 7 environment both ws and wss, and I'd like to ignore the certificate check during development.

following code works, with System.Net.WebSockets.ClientWebSocket
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

however when using System.Net.WebSockets.Manage.ClientWebSocket, its doesn't work and couldn't connect to websocket server.

Any extra configuration has to be done in this managed version?

Sign assembly

You cannot use this package in projects with signed assemblies, so is there any chance of getting this package with signed assemblies without building this project on our own?

Is there any way I can contribute to the solution of this problem?

Make SR class Internal

Public System.SR class unnecessarily causes conflicts in projects that have dependency on this library and define their own SR class.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.