开始休息从嵌入式系统流式传输 [英] Starting Rest Streaming from an embedded system

查看:194
本文介绍了开始休息从嵌入式系统流式传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是相当有限的嵌入式系统,所以我不能使用任何库,而且我自己构建HTTP请求。我可以用轮询来处理这些统计数据,但是我正在尝试打开Rest Streaming。

I'm using a fairly limited embedded system so I can't use any of the libraries and I'm on my own building HTTP requests. I'm able to handle the stats pretty well with polling but I'm trying to turn on Rest Streaming

Nest网站将您引导到Firebase站点,指导您W3C网站和我所有的所有这一切都是包括您的请求中的标题:Accept:text / event-stream。

The Nest site directs you to the Firebase site which directs you to the W3C site and all I get through all of that is to 'include the header: Accept: text/event-stream in your request'.

如果我发送请求(重定向后):

If I send the request (after the redirect):

GET /?auth=[auth code] HTTP/1.1
Host: firebase-apiserver02-tah01-iad01.dapi.production.nest.com:9553

我将完整的结构作为JSON。如果我发送:

I get the full structure as JSON. If I send:

GET /?auth=[auth code] HTTP/1.1
Host: firebase-apiserver02-tah01-iad01.dapi.production.nest.com:9553
Accept: text/event-stream
Connection: keep-alive

我得到200 OK响应,但没有别的。连接不关闭,但没有后续。我在正确的轨道上吗?似乎这应该是一个更大的交易,这一定是因为我没有什么,没有线索下一步去哪里。

I get a 200 OK response but nothing else. The connection doesn't close but nothing comes after. Am I on the right track here? It seems like this should be a bigger deal, and it must be because I got nothing and no clue where to go next.

有人使用Rest Streaming with Nest(没有图书馆)?

Is anybody using Rest Streaming with Nest (without the libraries)?

推荐答案

audiotech,
看起来nest-firebase正在使用WebSockets将更新发送回客户。
我正在C#WindowsForms工作,但是我发现对你有用。

audiotech, It looks like nest-firebase is using WebSockets to send updates back to the client. I am working in C# WindowsForms, but what I found will be useful to you.

我可以使用这个获取我的结构信息:

I can get my structure info using this:

    HttpWebRequest myStructHttpWebRequest=(HttpWebRequest)WebRequest.Create("https://developer-api.nest.com/structures/?auth=" + AccessToken);
myStructHttpWebRequest.Method = "GET";
myStructHttpWebRequest.MaximumAutomaticRedirections=3;
myStructHttpWebRequest.AllowAutoRedirect=true;
myStructHttpWebRequest.Accept = "text/event-stream; application/json";
myStructHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
myStructHttpWebRequest.KeepAlive = false;
using(HttpWebResponse myHttpWebResponse = (HttpWebResponse) myStructHttpWebRequest.GetResponseAsync())
{
    if (null != myHttpWebResponse)
    {
        // Store the response.
        Stream sData = myHttpWebResponse.GetResponseStream(); 
        StreamReader readStream = new StreamReader (sData, Encoding.UTF8);
        string data = readStream.ReadToEnd();
        readStream.Close();
        Debug.WriteLine("Response Structure stream received: " + data);
        success = deserializeStructure(data);
    }
}

我将上面的代码更改为:myStructHttpWebRequest.AllowAutoRedirect =假;
并得到重定向地址路径:

I changed the code above to: myStructHttpWebRequest.AllowAutoRedirect=false; and got the redirect address path:

if (HttpStatusCode.TemporaryRedirect == myHttpWebResponse.StatusCode)
{
    string[] redirect = myHttpWebResponse.Headers.GetValues("Location");
    path = redirect[0];
}

我使用此功能启动与此新路径的网络套接字连接,并收到此从消防服务器返回的消息:
{\t\:\c\,\d\:{\t\:\h \ \ d\:{\ ts\:1406745104997,\ v\:\ 5\,\ h\:\ firebase-apiserver01-tah01-i​​ad01.dapi.production.nest.com:9553\\",\\"s\\":\session-819323581\}}}

I start a web socket connection to this new path with this function and receive this message back from the firebase server: "{\"t\":\"c\",\"d\":{\"t\":\"h\",\"d\":{\"ts\":1406745104997,\"v\":\"5\",\"h\":\"firebase-apiserver01-tah01-iad01.dapi.production.nest.com:9553\",\"s\":\"session-819323581\"}}}"

这是我的WebSocket代码:

Here is my WebSocket code:

        private async void startNestWebSocketConnection(string path)
    {
        try
        {
            listener = new ClientWebSocket();
            if (path.StartsWith("https:"))
            {
                path = "wss:" + path.Substring(6) + ":" + COMMUNICATION_PORT.ToString();
            }
            await listener.ConnectAsync(new Uri(path),CancellationToken.None);
            var buffer = new byte[1024 * 4];
            Debug.WriteLine("ClientWebSocket connected " + listener.State.ToString());
            while (true)
            {
                ArraySegment<byte> segment = new ArraySegment<byte>(buffer);

                WebSocketReceiveResult result = await listener.ReceiveAsync(segment, CancellationToken.None);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    Debug.WriteLine("ClientWebSocket CLOSING");
                    await listener.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "Closing ClientWebSocket", CancellationToken.None);
                    return;
                }

                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        await listener.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "That's too long", CancellationToken.None);
                        return;
                    }

                    segment = new ArraySegment<byte>(buffer, count, buffer.Length - count);
                    result = await listener.ReceiveAsync(segment, CancellationToken.None);
                    count += result.Count;
                    Debug.WriteLine("ClientWebSocket getting " + count.ToString());
                }

                string message = Encoding.UTF8.GetString(buffer, 0, count);
                Debug.WriteLine(">" + message);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("startNestWebSocketConnection path=" + path + " Exception=" + ex.ToString());
        }
    }

我不知道回复是什么意思,但它显示时间戳和会话ID!当我做更多的测试时,我会让你知道更多。

I don't know what the reply means yet, but it shows a time stamp, and a session id! I will let you know more when I do more testing.

这篇关于开始休息从嵌入式系统流式传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆