javascript - Socket and node.js send -
I am trying to update a location on a Google map using socket.io and node.js There are 2 different ways to update the map. Probably some basic problems like I'm new to this.
1) Using an API call that looks like this:
Epistap ('/ location', function request, response) {
Var obj = request.body; // var obj = {gps_latitude: '59.33535 ', GPS_Hurding:' 0.0 ', GPS_timestamp:' 13 97132411524 ', GPSSpeed:' 0.0 ',' Device_id: 'oscar', gps_longitude: '17 .999672099999998 '}; // Insert space (OBJ); console.log ("Connected Client:" + map_clients.length); (Var i = 0; i & lt; map_clients.length; I ++) for {var client = map_cleller [i]; Var jsonString = JSON.stringify ({type: 'gps', data: ozzy}); console.log (jsonString); client.send (jsonString);} response. (200, {"content-type": "text / plain"}); reaction. Type ("OK"); Response.end ();}); 2) Using the socket from the client, does not it work? The server code looks like this:
io.sockets.on ("connection", function (client) {map_clients.push (client); // update status client.on ('status', Function (data) {var obj = {gps_latitude: '59.33535 ', GPS_Admission:' 0.0 ', GPS_timestamp:' 13 97132411524 ', GPS_SPID:' 0.0 ', Device_ID:' Oscars', GPS_Lange: '17. 996672099999998' }; var JasonString = Encrypt JSON ({Type: 'GPS', Data: OJG}); console.log (jsonString); client.send (jsonString);}); client .on ('disconnect', function) {Map_clients.splice (map_clients Inde XOf (customer), 1);})}); I can see that it sends data to the console:
debug - webbook writing3 ::: {"type": "GPS", "data" : "gps_latitude": "5933335", "gps_heading": "0.0", "gps_timestamp": "1397132411524", "gps_speed": "0.0", "DEVICE_ID": "oscar", "gps_longitude": "17.999672099999998"}}
but map updates are not available. I have this code on the map client side, but when I try to update the tpo above the socket code then the trigger is not triggered.
socket = io.connect (socket_url); Socket.on ('message', function (d) {console.log ("update position"); var parsedObj = JSON.parse (d); if (parsedObj.type === 'GPS') {var gps = parsedObj .data; $ ('# message'). Attachments (formatGPSHTMLOutput (GPS)); Process GPS (GPS);}});
You must use
client.emit (Instead of Client.end if you use client.on ('position'), then 'position', {your: "jsondata"}) < / Pre> callback)
On the server side, you also need to use
socket.one ('condition', callback) on the customer side
Comments
Post a Comment