Plot.ly数据流在随机时间段后失败

时间:2016-04-21 14:26:03

标签: c# networkstream http-streaming

我为Plot.ly Streaming API实现了简单的rest客户端,它每5秒发送一次温度测量值。一切正常,但在某些时候,过程只是停止,新的点不会出现。这可能在开始后15分钟或1小时后发生。 Tcp连接保持打开状态,基础流仍然可写,但新的点只是没有显示在图表上。我根据经验想出Content-Length标题对它的影响 - 如果它很大 - 一切都适用于孤独时期。

我的客户端是用C#编写的。基本上我写的是打开的NetworkStream原始Http请求:

private static string GetStringRequest(string host, string token, string data)
{
        var sb = new StringBuilder()
            .AppendLine($"POST http://{host}/ HTTP/1.1")
            .AppendLine($"Host: {host}")
            .AppendLine("Connection: keep-alive")
            .AppendLine("Content-Type: application/json")
            .AppendLine("Content-Length: " + Int64.MaxValue)
            .AppendLine("Cache-Control: no-cache")
            .AppendLine($"plotly-streamtoken: {token}")
            .AppendLine("")
            .AppendLine("")
            .Append(data);

        return sb.ToString();
}

// this is called every 5 seconds
public void WriteMeasurement(float value, DateTime dateTimeUtc)
{
        var data = GetData(...);
        var request = GetStringRequest(Host, _token, data);

        _writer.Write(request); // network stream writer
}

0 个答案:

没有答案
相关问题