【发布时间】:2018-02-21 22:28:24
【问题描述】:
我目前面临一个问题。如果我错了,请纠正我。在我的项目中,我有一个 NodeJS 服务器,它连接了 mongoDB 数据库和一个 android 应用程序。
有 4 个安卓设备连接到它,当服务器发布列表时,它通过Socket.IO 向所有安卓设备发送一个发出调用。 android 接收到发出调用的那一刻,它调用 API 来获取已发布的列表。这里请求调用(4 个请求)同时到达服务器。
这里是设备发生的意外分配。从某种意义上说,出乎意料的是,有一些参数应该基于这些参数分配给设备。当只有一台设备在线时,此方案完美运行,但当来自多台设备的并发请求到达时,有时会出现相关问题。
是否可以一次处理一个并发请求?
欢迎讨论/建议。 执行流程在这里
flowController.on('START', function () {
// Uploading and storing the binary data as excel file
});
flowController.on('1', function () {
// Validation to check if file contain required sheet with name and all and convert the file into JSON format
});
flowController.on('2', function () {
// Replacing the keys from the JSON to match the required keys
});
flowController.on('3', function () {
// Massive validation of records & seperating the records as per the activity
});
// Activity 1
flowController.on('4', function () {
// Insert records of Activity 1
});
// Activity 2
flowController.on('5', function () {
// Insert records of Activity 2
});
// Activity 3
flowController.on('6', function () {
// Insert records of Activity 3
});
// Activity 3
flowController.on('END', function () {
// End
});
【问题讨论】:
-
你能不能先得到新的发布列表(服务器端),然后通过服务器端列表向你的安卓设备发出通用发射?这样,只需要一个 API 请求,每个 android 设备都可以接收更新的列表。不过,这可能是错误的策略,因为我可能不完全了解您的确切需求,而且我可能会忽略一些东西。您的服务器可能会发送:
io.emit('updatePublishedList', publishedList),然后是socket.on('updatePublishedList', function(publishedList) { /* do stuff here with list */ } -
分享一些代码。
-
@explorer 我不能分享完整的代码,因为它太大了,但我可以分享调用的框架。这里应该添加的感觉是,当第一个请求已经被访问并且当前处于执行周期内时,锁定第二个请求的一段代码的执行
-
好的。由于 NodeJS 是单线程的,任何请求都不能覆盖另一个请求数据,即:publishedList。我想您所指的问题是事件
4,5,6。如果这是正确的并且根据我能够理解的内容,我认为现有代码流中没有任何问题。