ClientWebSocketFactory.cs 1.9 KB

1234567891011121314151617181920212223242526272829
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.IO;
  7. using System.Net.WebSockets;
  8. // In Win8 (and above), a client web socket can simply be created in 2 steps:
  9. // 1. create a HttpWebRequest with the Uri = "ws://server_address"
  10. // 2. create a client WebSocket with WebSocket.CreateClientWebSocket(stream_requested_from_the_HttpWebRequest)
  11. // On pre-Win8, the WebSocket.CreateClientWebSocket method doesn't work, so users needs to provide a factory for step #2.
  12. // WCF will internally create the HttpWebRequest from step #1 and will call the web socket factory for step #2.
  13. // A factory can also be used in Win8 (and above), if the user desires to use his own WebSocket implementation.
  14. public abstract class ClientWebSocketFactory
  15. {
  16. // Provides the web socket version, to be used as the required http header "Sec-WebSocket-Version".
  17. // When creating the HttpWebRequest from step #1, the web socket header is not initialized.
  18. public abstract string WebSocketVersion { get; }
  19. // Provides the client WebSocket for step #2. WCF creates the HttpWebRequest in step #1, and passes the HttpWebResponse stream
  20. // to this method. The 'settings' argument can optionally be used. On Win8 (and above), the WebSocket.CreateClientWebSocket method
  21. // requires other arguments (in addition to the Stream) that can be obtained from 'settings'. Since the WebSocket.CreateClientWebSocket
  22. // finds this argument to be enough to create a client WebSocket (on Win8, and post Win8 due to backward compatibility requirements),
  23. // we estimate that implementors of a custom web socket factory will find it enough too.
  24. public abstract WebSocket CreateWebSocket(Stream connection, WebSocketTransportSettings settings);
  25. }
  26. }