Socket and node.js send
Socket and node.js send
我正在尝试使用socket.io和node.js更新谷歌地图上的位置我有两种不同的方式来更新地图。可能是一些基本问题,因为我是新手。
1) 使用API调用,其工作方式如下:
app.post('/location',函数(请求,响应){
var obj = request.body;
//var obj = { gps_latitude: '59.33535', gps_heading: '0.0', gps_timestamp: '1397132411524', gps_speed: '0.0', device_id: 'oscar', gps_longitude: '17.999672099999998' };
//insertLocation(obj);
console.log("Connected clients: " + map_clients.length);
for(var i=0; i < map_clients.length; i++){
var client = map_clients[i];
var jsonString = JSON.stringify({ type:'gps', data:obj});
console.log(jsonString);
client.send(jsonString);
}
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("OK");
response.end();
});
2) 从客户端使用套接字更新这不起作用吗?服务器代码看起来像:
io.sockets.on("connection", function(client){
map_clients.push(client);
//Update position
client.on('position', function (data) {
var obj = { gps_latitude: '59.33535', gps_heading: '0.0', gps_timestamp: '1397132411524', gps_speed: '0.0', device_id: 'oscar', gps_longitude: '17.999672099999998' };
var jsonString = JSON.stringify({ type:'gps', data:obj});
console.log(jsonString);
client.send(jsonString);
});
client.on('disconnect', function(){
map_clients.splice(map_clients.indexOf(client), 1);
})
});
我可以看到它在控制台中发送数据:
debug-websocket写入3:::{"type":"gps","data":{
但是地图没有更新。在地图客户端,我有这个代码,但当我尝试通过上面的套接字代码进行tpo更新时,它从未被触发。
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;
$('#messages').append(formatGPSHTMLOutput(gps));
processGPS(gps);
}
});
您应该使用
client.emit('position', {your:"jsondata"})
而不是client.send,如果您使用
client.on('position', callback)
在服务器端,您还需要使用
socket.on('position', callback)
在客户端
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 使用agility.js进行页面布局和合成
- 如何将async.whilst调用的结果发送到和express.js res.send()
- Meteor.js email.send 在尝试发送给用户数组时不起作用
- 节点.js如果我使用 process.send(),请等待回调/响应
- Express.js 中 res.send 和 res.json 之间的区别
- Express(node.js)-res.send只发送部分响应
- VS2015 Cordova Sms插件Sms.Send不'无法在Index.JS(ondeviceReady)
- 如何在Node.js中使用res.send()
- express.js-如何拦截response.send()/response.json()
- 如何拦截send()或render()在Node.js express路由
- Node.js curl: (55) Send failure: Broken pipe error
- 在Analytics.js中是否可以在ga('send','pageview')之后使用
- Modal Notification Appear On Form Send, Foundation/Abide.js
- Node . js检查mysql表中是否存在数据.—TypeError: res.send不是函数
- 使用Facebook JS SDK中的send对话框获取用户ID
- Socket and node.js send
- Node.js,Express:如何使用客户端处理res.send 值
- 为什么节点js的res.send不工作
- Angular JS from rs.send()