postgresql 如何在实时 GPS 跟踪应用程序中使用 node.js?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20012977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:11:36  来源:igfitidea点击:

How to use node.js in a real time gps tracking application?

node.jsgoogle-mapspostgresqlgps

提问by Rupert

I am trying to understand how node.js will fit in my scenario.

我试图了解 node.js 如何适应我的场景。

Currently, a mobile device sends gps coordinates to a Rails backend thru POST. The mobile device sends it every minute.

目前,移动设备通过 POST 将 GPS 坐标发送到 Rails 后端。移动设备每分钟发送一次。

POST http://127.0.0.1:3000/location/

My RubyOnRails web app can display the history of locations on a map, no problem with this.

我的 RubyOnRails Web 应用程序可以在地图上显示位置的历史记录,这没有问题。

But my experience in nodejs is still in its infancy and I would like to understand how to display a map for real time GPS updates as they come in.

但是我在 nodejs 方面的经验仍处于起步阶段,我想了解如何显示地图以获取实时 GPS 更新。

Thanks in advance for reading this..

提前感谢您阅读本文..

回答by hgoebl

Use Socket.IO.

使用Socket.IO

Most tutorials show some kind of chat application. In your case the communication is only uni-directional. And your device is not connected through WebSockets, but only POSTs new coordinates without a channel back (device doesn't subscribe to events from server).

大多数教程都展示了某种聊天应用程序。在您的情况下,通信只是单向的。并且您的设备不是通过 WebSockets 连接的,而是仅在没有通道的情况下发布新坐标(设备不订阅来自服务器的事件)。

Your web-page (showing a Google Maps control) connects to your Node.js server through socket.io and gets new coordinates pushed from your server (socket.emit). You have to remember somehow which "new-coordinate" events from devices have to be published to which listening web-clients.

您的网页(显示 Google 地图控件)通过 socket.io 连接到您的 Node.js 服务器,并从您的服务器 ( socket.emit)获取新坐标。您必须以某种方式记住来自设备的哪些“新坐标”事件必须发布到哪些侦听网络客户端。

And of course you need an initial list of recent coordinates for your web-page if you want to show not only new coordinates of a device. You could deliver them over sockets as well, but as you will need some kind of authentication maybe a REST call is clearer for first population of devices GPS-track.

当然,如果您不仅想显示设备的新坐标,还需要一个网页最近坐标的初始列表。您也可以通过套接字传递它们,但是由于您需要某种身份验证,因此对于第一批 GPS 跟踪设备,REST 调用可能更清晰。

So when a device updates its location, you only have to find connected listeners for that device and emit("new-coordinate", {lat: ..., lng: ..., deviceId: ...}events to the maps. In the web-page, you receive the events and handle them like this:

因此,当设备更新其位置时,您只需找到该设备的连接侦听器和emit("new-coordinate", {lat: ..., lng: ..., deviceId: ...}地图的事件。在网页中,您接收事件并像这样处理它们:

<script src="/socket.io/socket.io.js" type="text/javascript"></script>
<script type="text/javascript">

$(function () {

    if (typeof io === 'undefined') {
        return; // socket.io not loaded
    }
    var socket = io.connect();
    socket.on('new-coordinate', function (data) {
        console.log('arrived', data);

        $(document).trigger('new-coordinate', data);

        // or just directly update your map ...

    });

});

</script>

Edit

编辑

As your web-page and the new-coordinate POSTs are delivered through RoR, you have to trigger the events from Ruby to node.js server-side. Basically you could call your node.js app from Ruby via REST, but there are other options like Redispub/sub or dnode ruby.

由于您的网页和新坐标 POST 是通过 RoR 传递的,因此您必须触发从 Ruby 到 node.js 服务器端的事件。基本上,您可以通过 REST 从 Ruby 调用您的 node.js 应用程序,但还有其他选项,例如Redispub/sub 或dnode ruby

(comment) I'd migrate the RoR app to node.js ;-)(/comment)

(评论)我会将 RoR 应用程序迁移到 node.js ;-)(/comment)