简体   繁体   English

如何在Windows Phone 8中通过Hammock使用Firebase REST流?

[英]How to use Firebase REST Streaming with Hammock in Windows Phone 8?

I'm working with the Nest API, which supports REST Streaming via Firebase. 我正在使用Nest API,后者通过Firebase支持REST流。 I have REST working, however I cannot get it to stream correctly. 我可以使用REST,但是无法正确传输。 This is very important for my app, and REST just isn't effective for what I want to do. 这对我的应用程序非常重要,而REST对于我想做的事情无效。

I'm using Hammock for the requests, and here's the code: 我正在使用Hammock进行请求,下面是代码:

public class NestAPI
{
    private RestClient client { get; set; }

    public NestAPI()
    {
        this.client = new RestClient();
        this.client.Authority = "https://developer-api.nest.com/";
        this.client.HasElevatedPermissions = true;
    }

    public void BeginStreaming()
    {
        RestRequest request = new RestRequest();
        request.AddParameter("auth", App.accessToken);
        request.RetryPolicy = new RetryPolicy() { RetryCount = 3 };
        //Enables streaming
        //request.AddHeader("Accept", "text/event-stream");
        //request.StreamOptions = new StreamOptions() { Duration = new TimeSpan(96, 0, 0), ResultsPerCallback = 1 };
        this.client.BeginRequest<object>(request, new RestCallback<object>(this.StreamCompletedEvent));
    }

    private void StreamCompletedEvent(RestRequest request, RestResponse<object> response, object userState)
    {
        //TO DO: check for errors first
        string json = response.Content;
    }

    public void EndStreaming()
    {
        this.client.CancelStreaming();
    }
}

This code works and does return JSON, however I can't seem to enable streaming. 这段代码可以正常工作并返回JSON,但是我似乎无法启用流式传输。 When I uncomment the lines below "Enables streaming", the callback event never fires. 当我取消注释“启用流”下面的行时,回调事件将永远不会触发。 It's important to note that authentication is done using the uri parameter, "auth". 重要的是要注意,认证是使用uri参数“ auth”完成的。

Unfortunately, there doesn't seem to be Firebase libraries available, and REST is my only option. 不幸的是,似乎没有可用的Firebase库,而REST是我唯一的选择。 I want to know when JSON properties change and want to set different values while streaming. 我想知道JSON属性何时更改,并想在流传输时设置不同的值。

I'm not familiar with Hammock, but can you make sure that it's set to follow redirects? 我不熟悉Hammock,但是您可以确保它设置为遵循重定向吗? The streaming endpoint typically issues HTTP 307 to get inform the client of the correct server to connect to. 流端点通常发出HTTP 307,以通知客户端要连接的正确服务器。

I've never used Hammock, but looking through source code (briefly) it appears you need to set it up as a streaming request with StreamOptions. 我从来没有使用过Hammock,但是(简要地)查看源代码似乎需要使用StreamOptions将其设置为流请求。 Twitter has some open source that uses this here https://github.com/camertron/twitter-windows/blob/master/Source/Twitter/Classes/API/Streaming/UserStream.cs . Twitter的一些开放源代码在这里https://github.com/camertron/twitter-windows/blob/master/Source/Twitter/Classes/API/Streaming/UserStream.cs使用

The way you have Hammock configured here it's waiting for an entire request to complete before calling your callback. 您在此处配置Hammock的方式是在调用回调之前等待整个请求完成。 This will (almost) never happen with a streaming request as the server keeps the connection open to push new results. 由于服务器保持连接打开以推送新结果,因此流请求几乎不会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM