2016-04-18 20 views
0

私はMEANスタックを使用してWebアプリケーションを開発しています。私のノードサーバーでは、リモートデバイスが10秒ほどの間隔でGPSデータを定期的に送信し続けるPOSTルート(/api/gps)があります。私はこのPOST要求を、投稿体をとり、タイムスタンプ、トリプティック、緯度、経度などの詳細を私のMonngoDBコレクションに保存することによってサービスします。私は立ち往生していますどこ は今ここにある - 私はルート持っている - MapDisplayCtrl- /displaymapdisplaymap.jadeでレンダリングし、コントローラに関連付けられています。ここでは、デバイスからの最近ポストされたGPSデータの地図を表示する必要があります。では、/ api/gpsに新しいPOSTデータを取得するとすぐにマップを更新するにはどうすればよいですか?NodeJS、AngularJS - サーバーへのイベント公開とリスニング

app.jsは、私は私が必要とするすべてのNodeJS側のイベント・エミッターとAngularJS側のリスナーですが、私にはないと思います

var GPSData = mongoose.model('GPSData', gpsSchema); 
app.post('/api/particle', function(req, res) { 
    GPSData.create({ 
       tripid : req.body.tripId, 
       latitude: req.body.latitude, 
       longitude: req.body.longitude, 
       timestamp: Date.now() 
      }, function(err, doc) { 
       if(err) { 
        res.send(err); 
       } else { 
        res.send(doc); 
        //here is where I want to publish an event say, 'SendMapData' 
        //that sends co-ordinates to the controller 
       } 
      }); 
    }); 

MapDisplayCtrl.jsが

var myApp = angular.module('myapp'); 

myApp.controller('MapDisplayCtrl', function($http, $scope) { 
    //listen to the 'SendMapData' event and get the co-ordinates 
    //draw a google map using the co-ordinates 
}); 

をファイルファイルそれを実装する方法を知っている。私はあなたが答えるために十分な情報を与えてくれることを願っています。これ以上情報が必要な場合は、私はそれを提供します。前もって感謝します!

答えて

2

socket.ioを使用して、サーバ&クライアント間でリアルタイムに通信することができます。

サーバー:

//here is where I want to publish an event say, 'SendMapData' 
//that sends co-ordinates to the controller 
socket.emit('SendMapData', data); 

コントローラー:ドキュメントについては

//listen to the 'SendMapData' event and get the co-ordinates 
//draw a google map using the co-ordinates 
socket.on('SendMapData', function(serverData) { 
    //do what you want with data received from server. 
}); 

socket.io(ない最高のマニュアルを参照して、それが役立ちます)チェック

EDIT
socket.ioを使用するためには、角度のあるアプリの中で、私はbuを持っている "角の道"になるILD(SOに触発されていないすべて自分で、)このためのサービス:

MYAPP.factory('socket', ['$timeout', function($timeout){ 
    var socketMethods = {}, 
     socketio; 

    //fn: connect 
    //desc: connect to server 
    socketMethods.connect = function(server){ 
     if(! angular.isUndefined(socketio)) return; 

     var ioUrl = 'ws://'+ server.fullAddr; 
     var ioManager = {transports: ['websocket', 'polling'], reconnectionAttempts: Infinity}; 
     socketio = io(ioUrl, ioManager); 
    } 

    //fn: on 
    //desc: receive data from server 
    socketMethods.on = function(eventName, callback){ 
     //check if event is already registered; 'connect' registered by default, ignore it. 
     if(socketio.hasListeners(eventName) && eventName != 'connect') return; 
     socketio.on(eventName, function(serverData, respondToServer){ 
      $timeout(function(){ 
       callback.call(socketio, serverData, respondToServer); 
      }); 
     }); 
    } 

    //fn: emit 
    //desc: send data to server 
    socketMethods.emit = function(eventName, data, callback){ 
     socketio.emit(eventName, data, function(serverResponse){ 
      $timeout(function(){ 
       if(callback) callback.call(socketio, serverResponse); 
      }); 
     }); 
    } 

    return socketMethods; 
}]); 

を...そして、あなたは、あなたのコントローラで「ソケット」を注入ソケット接続をオープンし、イベントを発行/聴くことができます。

関連する問題