简体   繁体   English

使用HTML5 EventSource将CZML流式传输到Cesium中

[英]Streaming CZML into Cesium using HTML5 EventSource

I'm currently looking into using Cesium as a way of visualising data for a personal project, and real-time updates would be a great thing to be able to do. 我目前正在研究使用Cesium作为个人项目数据可视化的方式,实时更新将是一件很棒的事情。

Reading the wiki, I found this section which outlines how dynamically updating objects in Cesium can be accomplished using the HTML EventSource API. 阅读wiki,我发现本节概述了如何使用HTML EventSource API完成动态更新Cesium中的对象。

I have written a rather simple server in Node.js that creates a text/event-stream which sends out updates of an object's position periodically. 我在Node.js中编写了一个相当简单的服务器,它创建了一个text/event-stream ,定期发送对象位置的更新。 This part works fine and I can successfully connect to and log this data to the console. 这部分工作正常,我可以成功连接并将此数据记录到控制台。

My problem lies with Cesium. 我的问题在于铯。 I've spent hours digging through the documentation (both the Github wiki and the JSDoc documentation included with the download) and I can't figure out how to get my CZML to be added to the globe. 我花了几个小时挖掘文档(Github wiki和下载中包含的JSDoc文档),我无法弄清楚如何将我的CZML添加到全球。 Using the Cesium Viewer application provided with the source code I can see how CZML files can be loaded from both local and remote resources, but I can't figure out how to modify this approach to ingest CZML packets coming in from EventSource events. 使用随源代码提供的Cesium Viewer应用程序,我可以看到如何从本地和远程资源加载CZML文件,但我无法弄清楚如何修改这种方法来摄取来自EventSource事件的CZML数据包。

A sample of my CZML packets: 我的CZML数据包示例:

{
  'id': 'myObject',
  'availability': '2014-01-15T00:00Z/2014-01-01T24:00Z',
  'point': {
    'color': {
      'rgba': [255, 255, 0, 255]
    },
    'outlineWidth': 2.0,
    'pixelSize': 3.0,
    'show': true
  },
  'position': {
    'cartesian': [0.0, -2957000.0, -840000.0, 5581000.0],
    'epoch': '2014-01-01T00:00Z',
    'interpolationAlgorithm': 'LINEAR',
    'interpolationDegree': 1
  }
}

My current approach is as follows: 我目前的做法如下:

var czmlStream;
var czmlStreamUrl = 'http://127.0.0.1:8080/czml-stream';

viewer.dataSources.add(czmlStream);

var czmlEventSource = new EventSource(czmlStreamUrl);
czmlEventSource.addEventListener('czml', function(czmlUpdate) {
  czmlStream.load(JSON.parse(czmlUpdate.data));
}, false);

Unfortunately, this doesn't work. 不幸的是,这不起作用。 I based it on how a static CZML file can be loaded: 我基于如何加载静态CZML文件:

var source;
var sourceURL = 'http://127.0.0.1/czml-static.czml';

source.loadUrl(sourceURL).then(function() {
  viewer.dataSources.add(source);
}

Does anyone know where I am going wrong, or better yet, the right way of doing this? 有谁知道我哪里出错了,或者更好的是,正确的做法是什么? I am using Cesium b24 in case that makes a difference. 我正在使用Cesium b24以防万一。 If you need any more information from me to be able to assist please ask and I'll update the question. 如果您需要我的任何其他信息以便能够提供帮助,请询问我是否会更新问题。

I have tried Googling for a solution and example code but I can't find anything except the wiki page describing how EventSource could be used. 我已经尝试使用谷歌搜索解决方案和示例代码但除了描述如何使用EventSource的维基页面之外我找不到任何东西。

I know this question is a few weeks old, but did you ever figure this out? 我知道这个问题有几个星期了,但你有没有想过这个? From the above example, the first thing I notice is that you are calling czmlStream.load instead of czmlStream.process. 从上面的例子中,我注意到的第一件事是你正在调用czmlStream.load而不是czmlStream.process。 load clears out an existing data while process doesn't. load清除现有数据,而process则不会。 For streaming, calling load results in only the last packet ever showing up. 对于流式传输,调用加载仅导致最后一个数据包出现。

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

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