Exemple #1
0
        public static UpgradeResponse read(System.IO.Stream stream)
        {
            UpgradeResponse response  = null;
            String          delimiter = "  ";

            char[] delimiters = delimiter.ToCharArray();

            String delimiter2 = ":  ";

            char[] delimiters2 = delimiter2.ToCharArray();

            StreamReader reader = new StreamReader(stream);

            for (String line; (line = reader.ReadLine()) != null;)
            {
                if (String.IsNullOrEmpty(line))
                {
                    break;
                }
                if (response == null)
                {
                    String[] status = line.Split(delimiters, 3);
                    response = new UpgradeResponse(status[0], Int32.Parse(status[1]), status[2]);
                }
                else
                {
                    String[] header = line.Split(delimiters2, 2);
                    response.setHeader(header[0].Trim(), header[1].Trim());
                }
            }

            if (response == null)
            {
                throw new IOException("no HTTP response");
            }

            return(response);
        }
Exemple #2
0
        public void Run()
        {
            try
            {
                socket = new Windows.Networking.Sockets.StreamSocket();
                UpgradeRequest request = null;
                try
                {
                    // connect to the eftlServer
                    if (String.Compare("ws", uri.Scheme, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        socket.ConnectAsync(remoteHostName, getPort().ToString(), Windows.Networking.Sockets.SocketProtectionLevel.PlainSocket).AsTask().Wait(5000);
                    }
                    else if (String.Compare("wss", uri.Scheme, StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        socket.ConnectAsync(remoteHostName, getPort().ToString(), Windows.Networking.Sockets.SocketProtectionLevel.Ssl).AsTask().Wait(5000);
                    }

                    Windows.Networking.Sockets.SocketProtectionLevel l = socket.Information.ProtectionLevel;
                    Console.WriteLine("ProtectionLevel = " + l);

                    // send HTTP upgrade request
                    request = new UpgradeRequest(uri, protocols);
                    DataWriter writer = new DataWriter(socket.OutputStream);

                    String s = request.toString();
                    writer.WriteString(s);

                    // Call StoreAsync method to store the data to a backing stream
                    try
                    {
                        writer.StoreAsync().AsTask().Wait();
                        writer.FlushAsync().AsTask().Wait();
                    }
                    catch (Exception e) {
                        Console.WriteLine(e.StackTrace);
                    }
                    writer.DetachStream();
                }
                catch (Exception e)
                {
                    Exception exp = new Exception("failed to connect" + ((e.InnerException != null) ? e.InnerException.Message : ""));
                    notifyError(exp);
                }

                byte[]       buffer      = new byte[32768];
                IInputStream inputStream = socket.InputStream;

                try {
                    inputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length, InputStreamOptions.Partial).AsTask().Wait();
                    System.IO.Stream stream = new System.IO.MemoryStream(buffer);

                    // read HTTP upgrade response
                    UpgradeResponse response = UpgradeResponse.read(stream);
                    response.validate(request);

                    // get the agreed upon protocol
                    protocol = response.getProtocol();
                }
                catch (Exception e)
                {
                    notifyError(e);
                }

                // notify listener
                notifyOpen();

                // dispatch frames
                dispatch();
            }
            catch (Exception e)
            {
                notifyError(e);
            }
            finally
            {
                socket.Dispose();
            }
        }