Пример #1
0
        public void Start(IEnumerable <string> urlPrefixes)
        {
            _httpsv = new WebSocketSharp.Server.HttpServer(8096, false, urlPrefixes.First());

            _httpsv.OnRequest += _httpsv_OnRequest;

            _httpsv.Start();
        }
        public void Start(IEnumerable<string> urlPrefixes)
        {
            _httpsv = new WebSocketSharp.Server.HttpServer(8096, false, urlPrefixes.First());

            _httpsv.OnRequest += _httpsv_OnRequest;

            _httpsv.Start();
        }
Пример #3
0
        private void Initialise()
        {
            _webSocketServer          = new WebSocketSharp.Server.HttpServer(_port);
            _webSocketServer.RootPath = _rootDirectory + "\\";

            _webSocketServer.AddWebSocketService <TelemetryService>("/Telemetry", () => new TelemetryService(_telemetryData));
            _webSocketServer.AddWebSocketService <HeartBeatService>("/HeartBeat", () => new HeartBeatService(_telemetryData));
            _webSocketServer.AddWebSocketService <TimingService>("/Timing", () => new TimingService(_timingData));

            _webSocketServer.Start();

            _webSocketServer.OnPost += (sender, e) =>
            {
                lock (lockObject)
                {
                    var request  = e.Request;
                    var response = e.Response;

                    DataHandlerResult result = null;

                    var uri = request.Url;

                    if (uri.Segments.Length == 2)
                    {
                        var action = uri.Segments[1];

                        string postedText = "";
                        using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
                        {
                            postedText = reader.ReadToEnd();
                        }
                        NameValueCollection postData = HttpUtility.ParseQueryString(postedText);

                        switch (action)
                        {
                        case "File":
                            result = ActionsDataHandler.ProcessFileRequest(_telemetryData, _plugins, postData);
                            break;

                        case "Track":
                            result = TrackDataHandler.ProcessRequest(_telemetryData, _plugins, postData);
                            break;

                        case "TrackRecorder":
                            result = TrackDataHandler.ProcessRequest(_telemetryData, _plugins, postData);
                            break;

                        case "Connected":
                            result = ConnectedDataHandler.ProcessConnectedRequest(_telemetryData, postData);
                            break;

                        case "HeartBeat":
                            result = HeartBeatDataHandler.ProcessConnectedRequest(_telemetryData, postData);
                            break;

                        case "Telemetry":
                            result = TelemetryDataHandler.ProcessConnectedRequest(_telemetryData, postData);
                            break;

                        case "Timing":
                            result = TimingDataHandler.ProcessConnectedRequest(_timingData, postData);
                            break;

                        case "Startup":
                            result = StartupDataHandler.ProcessStartupRequest(_telemetryData, _plugins, postData);
                            break;
                        }
                        response.StatusCode = (int)HttpStatusCode.OK;
                    }
                    else
                    {
                        result = new DataHandlerError();
                        response.StatusCode = (int)HttpStatusCode.NotFound;
                    }

                    string json = new JavaScriptSerializer().Serialize(result);
                    response.ContentType = "application/json";

                    byte[] content = Encoding.UTF8.GetBytes(json);
                    response.ContentLength64 = content.Length;

                    System.IO.Stream output = response.OutputStream;
                    output.Write(content, 0, content.Length);

                    result.Dispose();
                }
            };

            _webSocketServer.OnGet += (sender, e) =>
            {
                lock (lockObject)
                {
                    var request              = e.Request;
                    var response             = e.Response;
                    DataHandlerResult result = null;

                    string[] rawUrlArray = request.RawUrl.Split('?');
                    string   rawUrl      = rawUrlArray[0];

                    _httpServerPath = rawUrl;

                    if (string.IsNullOrEmpty(_httpServerPath) || _httpServerPath == "/")
                    {
                        foreach (string indexFile in indexFiles)
                        {
                            if (File.Exists(Path.Combine(_rootDirectory, indexFile)))
                            {
                                _httpServerPath = indexFile;
                                break;
                            }
                        }
                    }

                    byte[] content = _webSocketServer.GetFile(_httpServerPath);

                    if (content == null)
                    {
                        List <string> urlBits = rawUrl.Split('/').ToList().Where(s => !string.IsNullOrWhiteSpace(s)).ToList();

                        switch (urlBits[0])
                        {
                        case "Startup":
                            result = StartupDataHandler.ProcessStartupRequest(_telemetryData, _plugins, request.QueryString);
                            break;
                        }

                        string json = new JavaScriptSerializer().Serialize(result);
                        content = Encoding.UTF8.GetBytes(json);
                    }

                    string mime;
                    response.ContentType     = mimeTypeMappings.TryGetValue(Path.GetExtension(_httpServerPath), out mime) ? mime : "application/octet-stream";
                    response.ContentEncoding = Encoding.UTF8;

                    if (content != null)
                    {
                        int start     = 0;
                        int end       = -1;
                        int byteRange = -1;

                        if (request.Headers.GetValues("Range") != null)
                        {
                            string   rangeHeader = request.Headers.GetValues("Range")[0].Replace("bytes=", "");
                            string[] range       = rangeHeader.Split('-');
                            start = int.Parse(range[0]);
                            if (range[1].Trim().Length > 0)
                            {
                                int.TryParse(range[1], out end);
                            }
                            if (end == -1)
                            {
                                end = content.Length;
                            }

                            byteRange = end - start;
                            response.AddHeader("Accept-Ranges", "bytes");
                            response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", start, byteRange - 1, byteRange));
                            response.AddHeader("X-Content-Duration", "0.0");
                            response.AddHeader("Content-Duration", "0.0");

                            response.ContentLength64 = byteRange;
                            response.StatusCode      = (int)HttpStatusCode.PartialContent;

                            Console.WriteLine(rangeHeader);
                            Console.WriteLine(string.Format("bytes {0}-{1}/{2}", start, byteRange - 1, byteRange));
                            Console.WriteLine("Content length" + byteRange.ToString());
                            var tt = 56;
                            //http://stackoverflow.com/questions/25898922/handle-range-requests-with-httplistener
                            //http://www.stevesouders.com/blog/2013/04/21/html5-video-bytes-on-ios/
                        }
                        else
                        {
                            // response.AddHeader("cache-control", "no-store, must-revalidate, private");
                            //  response.AddHeader("Pragma", "no-cache");

                            response.ContentLength64 = content.Length;
                            response.StatusCode      = (int)HttpStatusCode.OK;

                            byteRange = content.Length;
                        }

                        System.IO.Stream output = response.OutputStream;
                        output.Write(content, start, byteRange);

                        response = null;
                    }
                    else
                    {
                        response.StatusCode = (int)HttpStatusCode.NotFound;
                    }
                }
            };
        }