【发布时间】:2020-06-24 18:15:19
【问题描述】:
我以前从未使用过 Socket.IO,所以这对我来说很新。
我在 react 中有一个出租车应用程序,它将通过 Socket.IO 将新订单作为事件发送,但我没有看到任何错误或响应。
服务器端是这样的:
client.on('newOrder', function(data){
socketController.newOrder(data, io, client);
});
newOrder: function(data, io, client) {
order.create(data, io, client, function(response){});
}
Order.prototype.create = function (data, io, client, callback) {
console.log("ORDER DATA = " + JSON.stringify(data));
let luggage = 0;
if (typeof data.luggage !== 'undefined')
luggage = data.luggage;
let insertData = {
client_id: data.id_client,
start_address: data.origin.name,
start_lat: data.origin.latitude,
start_lng: data.origin.longitude,
end_address: data.destination.name,
end_lat: data.destination.latitude,
end_lng: data.destination.longitude,
options: JSON.stringify(data.options),
car_options: JSON.stringify(data.car_options),
car_type: data.car_type,
passengers: data.passengers,
luggage: luggage,
payment_method: data.payment_method,
profile_id: data.profile,
profile_type: data.profile_type
};
if (data.schedule_time != null && data.schedule_time.length > 0) {
insertData.schedule_time = data.schedule_time
}
db.query("INSERT INTO orders SET ?", insertData,
function (err, results, fields) {
if (err) {
console.log("Order.create [ERROR: " + err + "]");
return callback({ error: true, message: err });
}
data.id_order = results.insertId;
const timeInsert = {
order_id: data.id_order,
new: new Date().toISOString().slice(0, 19).replace('T', ' ')
};
db.query("INSERT INTO orders_timestamps SET ?", timeInsert, function (err, results, fields) { });
client.emit("orderSaved", { "id_order": data.id_order });
getOrder(data.id_order, function (order) {
sendOrdersToDriver(data, order, io, function (data) {
console.log("Order.create [SUCCESS]");
return callback(data);
});
});
}
);
};
客户端是这样的
const data = {
id_client: user.id,
car_type: "executive",
car_options: [],
passengers: 1,
luggage: 2,
payment_method: "cash",
options: [],
origin: { name: pickUp, latitude: pickUpCoordinates.lat, longitude: pickUpCoordinates.lng },
destination: { name: dropOff, latitude: dropOffCoordinates.lat, longitude: dropOffCoordinates.lng },
schedule_time: new Date(),
profile: "",
profile_type: "owner"
}
socket.emit("newOrder", data, function (response) {
console.log('emit response', response);
});
【问题讨论】: