Ejemplo n.º 1
0
        private string CalculateWebSocketAccept(string openingHandshake)
        {
            string key           = NetworkPackageSniffer.GetWebSocketKey(openingHandshake);
            SHA1   sha1Encrypter = SHA1.Create();

            byte[] acceptKeyBytes = Encoding.UTF8.GetBytes(key + WebSocketServerMagicString);
            byte[] hash           = sha1Encrypter.ComputeHash(acceptKeyBytes);
            return(Convert.ToBase64String(hash));
        }
Ejemplo n.º 2
0
        private void OnRead(IAsyncResult ar)
        {
            var client = (TcpClient)ar.AsyncState;
            var stream = client.GetStream();
            int read   = stream.EndRead(ar);

            string request = Encoding.UTF8.GetString(_buffer, 0, read);

            try
            {
                if (NetworkPackageSniffer.IsOpeningHandshake(request))
                {
                    string key      = CalculateWebSocketAccept(request);
                    byte[] response = NetworkMessage.GetClosingHandshake(key);
                    stream.Write(response, 0, response.Length);
                }
                else
                {
                    var frame = new WebSocketFrame(_buffer);
                    if (!frame.IsMasked)
                    {
                        throw new ProtocolException("Payload has to be masked.", WebSocketStatusCode.ProtocolError);
                    }

                    object result = null;

                    if (frame.OpCode == 1)
                    {
                        result = Encoding.UTF8.GetString(frame.ApplicationData);
                    }
                    else if (frame.OpCode == 2)
                    {
                        result = BitConverter.ToSingle(frame.ApplicationData, 0);
                    }
                    else if (frame.OpCode == 8)
                    {
                        // Closing handshake
                        SendClosingHandshake(stream);

                        // Cleanup
                        _clients.Remove(client.Client.RemoteEndPoint.ToString());
                        client.Close();
                    }

                    Debug.WriteLine(result);

                    var response = new byte[2 + frame.ApplicationData.Length];
                    int byte1    = frame.OpCode == 1 ? 129 : 130;
                    int byte2    = frame.ApplicationData.Length;

                    response[0] = (byte)byte1;
                    response[1] = (byte)byte2;

                    for (var i = 0; i < frame.ApplicationData.Length; i++)
                    {
                        response[i + 2] = frame.ApplicationData[i];
                    }

                    foreach (KeyValuePair <string, TcpClient> kvp in _clients)
                    {
                        TcpClient c = kvp.Value;

                        if (client.Client.RemoteEndPoint.ToString() == c.Client.RemoteEndPoint.ToString())
                        {
                            continue;
                        }

                        NetworkStream clientStream = c.GetStream();
                        clientStream.Write(response, 0, response.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                SendClosingHandshake(stream, WebSocketStatusCode.ProtocolError);
                _clients.Remove(client.Client.RemoteEndPoint.ToString());
                client.Close();
            }
        }