2010-01-14 56 views
50

Me gustaría usar WebSockets en mi Windows Forms o aplicación WPF. ¿Existe un control .NET que sea compatible con WebSockets implementado? ¿O hay algún proyecto de código abierto comenzado al respecto?¿Hay un cliente WebSocket implementado para .NET?

Una solución de código abierto para un cliente Java compatible con WebSockets también podría ayudarme.

Respuesta

20

Ahora, SuperWebSocket también incluye una aplicación cliente WebSocket SuperWebSocket Project Homepage

Otras implementaciones de cliente .NET incluyen:

+1

No puedo ver nada en SuperWebSocket sobre la implementación de un cliente? ¿Todavía está ahí? –

+0

En SourceCode/mainline/Client –

+8

El cliente en SuperWebSocket se ha separado en WebSocket4Net: http://websocket4net.codeplex.com/ –

1

es un protocolo bastante simple. hay una implementación java here que no debería ser demasiado difícil de traducir a C#. si logro hacerlo, lo publicaré aquí ...

24

Aquí hay un primer paso rápido para portar ese código de Java a C#. No es compatible con el modo SSL y solo se ha probado muy poco, pero es un comienzo.

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 

public class WebSocket 
{ 
    private Uri mUrl; 
    private TcpClient mClient; 
    private NetworkStream mStream; 
    private bool mHandshakeComplete; 
    private Dictionary<string, string> mHeaders; 

    public WebSocket(Uri url) 
    { 
     mUrl = url; 

     string protocol = mUrl.Scheme; 
     if (!protocol.Equals("ws") && !protocol.Equals("wss")) 
      throw new ArgumentException("Unsupported protocol: " + protocol); 
    } 

    public void SetHeaders(Dictionary<string, string> headers) 
    { 
     mHeaders = headers; 
    } 

    public void Connect() 
    { 
     string host = mUrl.DnsSafeHost; 
     string path = mUrl.PathAndQuery; 
     string origin = "http://" + host; 

     mClient = CreateSocket(mUrl); 
     mStream = mClient.GetStream(); 

     int port = ((IPEndPoint)mClient.Client.RemoteEndPoint).Port; 
     if (port != 80) 
      host = host + ":" + port; 

     StringBuilder extraHeaders = new StringBuilder(); 
     if (mHeaders != null) 
     { 
      foreach (KeyValuePair<string, string> header in mHeaders) 
       extraHeaders.Append(header.Key + ": " + header.Value + "\r\n"); 
     } 

     string request = "GET " + path + " HTTP/1.1\r\n" + 
         "Upgrade: WebSocket\r\n" + 
         "Connection: Upgrade\r\n" + 
         "Host: " + host + "\r\n" + 
         "Origin: " + origin + "\r\n" + 
         extraHeaders.ToString() + "\r\n"; 
     byte[] sendBuffer = Encoding.UTF8.GetBytes(request); 

     mStream.Write(sendBuffer, 0, sendBuffer.Length); 

     StreamReader reader = new StreamReader(mStream); 
     { 
      string header = reader.ReadLine(); 
      if (!header.Equals("HTTP/1.1 101 Web Socket Protocol Handshake")) 
       throw new IOException("Invalid handshake response"); 

      header = reader.ReadLine(); 
      if (!header.Equals("Upgrade: WebSocket")) 
       throw new IOException("Invalid handshake response"); 

      header = reader.ReadLine(); 
      if (!header.Equals("Connection: Upgrade")) 
       throw new IOException("Invalid handshake response"); 
     } 

     mHandshakeComplete = true; 
    } 

    public void Send(string str) 
    { 
     if (!mHandshakeComplete) 
      throw new InvalidOperationException("Handshake not complete"); 

     byte[] sendBuffer = Encoding.UTF8.GetBytes(str); 

     mStream.WriteByte(0x00); 
     mStream.Write(sendBuffer, 0, sendBuffer.Length); 
     mStream.WriteByte(0xff); 
     mStream.Flush(); 
    } 

    public string Recv() 
    { 
     if (!mHandshakeComplete) 
      throw new InvalidOperationException("Handshake not complete"); 

     StringBuilder recvBuffer = new StringBuilder(); 

     BinaryReader reader = new BinaryReader(mStream); 
     byte b = reader.ReadByte(); 
     if ((b & 0x80) == 0x80) 
     { 
      // Skip data frame 
      int len = 0; 
      do 
      { 
       b = (byte)(reader.ReadByte() & 0x7f); 
       len += b * 128; 
      } while ((b & 0x80) != 0x80); 

      for (int i = 0; i < len; i++) 
       reader.ReadByte(); 
     } 

     while (true) 
     { 
      b = reader.ReadByte(); 
      if (b == 0xff) 
       break; 

      recvBuffer.Append(b);   
     } 

     return recvBuffer.ToString(); 
    } 

    public void Close() 
    { 
     mStream.Dispose(); 
     mClient.Close(); 
     mStream = null; 
     mClient = null; 
    } 

    private static TcpClient CreateSocket(Uri url) 
    { 
     string scheme = url.Scheme; 
     string host = url.DnsSafeHost; 

     int port = url.Port; 
     if (port <= 0) 
     { 
      if (scheme.Equals("wss")) 
       port = 443; 
      else if (scheme.Equals("ws")) 
       port = 80; 
      else 
       throw new ArgumentException("Unsupported scheme"); 
     } 

     if (scheme.Equals("wss")) 
      throw new NotImplementedException("SSL support not implemented yet"); 
     else 
      return new TcpClient(host, port); 
    } 
} 
+6

Tenga en cuenta que StringBuilder.Append (byte) no hace lo que se Creo que sí. Añade la representación textual del byte al buffer. Crear una lista de bytes y luego convertir ese byte [] a una cadena usando Encoding.UTF8.GetString (bytes) funciona mejor. –

+1

+1 funcionó para mí. Terminé modificando 'mStream = new SslStream (mClient.GetStream());' y agregando 'mStream.AuthenicateAsClient (host);', y eso era todo lo que se necesitaba para el soporte de SSL. – primo

1

Recientemente los puentes y los Laboratorios Centro de Interoperabilidad dio a conocer un prototipo de aplicación (en código administrado) de dos borradores de la especificación del protocolo WebSockets:

proyecto-hixie-thewebsocketprotocol-75 y proyecto-hixie -thewebsocketprotocol-76

El prototipo se puede encontrar en HTML5 Labs. Puse en this blog post toda la información que encontré (hasta ahora) y fragmentos de código sobre cómo se puede hacer esto usando WCF.

1

Si desea algo un poco más liviano, consulte un servidor C# que un amigo y yo lanzamos: https://github.com/Olivine-Labs/Alchemy-Websockets

Soporta Websockets de vainilla y websockets de flash. Fue creado para nuestro juego en línea con un enfoque en la escalabilidad y la eficiencia.

10

La compatibilidad con WebSockets es coming in .NET 4.5. Los enlaces también contienen un ejemplo usando la clase System.Net.WebSockets.WebSocket.

+2

De los documentos "las únicas implementaciones públicas de WebSockets de cliente y servidor son compatibles con Windows 8 y Windows Server 2012", es decir. no funcionará con versiones anteriores de Windows. –

0

También hay Alquimia. http://olivinelabs.com/Alchemy-Websockets/ que es bastante genial.

+0

y que no es un cliente WebSocket – Shaddix

+0

Sí lo es. Hay una lib del cliente WebSocket con él. ¡Mirar de nuevo! – Rushino

+0

No lo veo como una lib en realidad. Sin embargo, hay una [clase WebSocketClient] (https://github.com/Olivine-Labs/Alchemy-Websockets/blob/master/src/Alchemy/WebSocketClient.cs) – Shaddix

4

otra opción: XSockets.Net, tiene implementar servidor y el cliente.

puede instalar el servidor por:

PM> Install-Package XSockets 

o instalar el cliente por:

PM> Install-Package XSockets.Client 

versión actual es: 3.0.4

Cuestiones relacionadas