该应用程序由 Next.js 运行。我有一个 useQueries 钩子:const userQueries = useQueries({ queries: user.contacts.map((contactId: string) => ({ queryKey: ['contact', conta...
该应用程序由 Next.js 运行。
我有一个 useQueries 钩子:
const userQueries = useQueries({
queries: user.contacts.map((contactId: string) => ({
queryKey: ['contact', contactId],
queryFn: () => getUserById(contactId),
}))
});
api.ts 文件中的函数“getUserById”:
export async function getUserById(userId: string) {
const supabase = createClient();
let {data} = await supabase
.from('users')
.select()
.eq('id', userId);
return data && data[0]
}
我发现当我以这种方式加载数据时,它会按顺序加载而不是并行加载。
我尝试将 api 函数设为非异步,以便每个请求不会等待前一个请求,但 Next.js 禁止在没有“async”前缀的情况下执行服务器操作。有办法解决这个问题吗?
我正在学习使用 ESP32 的 MQTT 服务器和客户端以及 UDP 协议。当代码中有两个线程时,ESP32 每秒都会重新启动,我遇到这个问题:一个用于发送,一个用于接收......
我正在学习使用 ESP32 的 MQTT 服务器和客户端以及 UDP 协议。
当代码中有两个线程时,ESP32 每秒都会重新启动,这是一个问题:一个用于发送 UDP 数据报,一个用于接收 UDP 数据报。有人能帮我解决这个问题吗?
这是我的代码
#include <WiFi.h>
#include "AsyncUDP.h"
#include <MQTT.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
TaskHandle_t thread;
WiFiClient net;
const char* ssid = "---"; //some info
const char* password = "---"; //some info
IPAddress outIp(***, ***, ***, ***); //some info
const unsigned int outPort = ----; //some info
MQTTClient mqttClient;
int port = ----; //some info
long lastMillis = 0;
String ping = "";
String address = "";
bool start = true;
AsyncUDP udp;
AsyncUDP udpThread;
AsyncUDPMessage udpMsg;
uint32_t timeConnect;
uint32_t timeOffset;
void setup() {
Serial.begin(115200);
xTaskCreatePinnedToCore(
threadLoop,
"Thread",
10000,
NULL,
1,
&thread,
0);
ConnectToWifi(ssid, password, true);
Serial.println("Connected to the Wi-Fi network");
ping = GetPing();
Serial.println(ping);
}
void loop() {
OSCMessage msg("/ping");
msg.add("Some message");
msg.send(udpMsg);
msg.empty();
udp.sendTo(udpMsg, outIp, outPort);
delay(1000);
}
void threadLoop(void* pvParameters) {
if (udpThread.listen(port)) {
String data[2];
udpThread.onPacket([](AsyncUDPPacket packet) {
//parse packet
}
if (data[0] == "***") {
address = packet.remoteIP().toString();
int addressLen = address.length() + 1;
char broker[addressLen];
address.toCharArray(broker, addressLen);
mqttClient.begin(broker, net);
mqttClient.onMessage(Received); //event subscription
char* timeEnd;
timeConnect = strtol(data[1].c_str(), &timeEnd, 0);
timeOffset = millis() / 1000ul;
start = true;
}
});
while (1) {
if (start) {
mqttClient.loop();
delay(10);
if (!mqttClient.connected()) {
ConnectedMQTT();
}
if (millis() - lastMillis > 1000) {
lastMillis = millis();
uint32_t sec = millis() / 1000ul;
uint32_t time = timeConnect + (sec - timeOffset);
mqttClient.publish(ping, String(time), true, 0);
}
}
else {
udpThread.broadcastTo("***", port);
delay(1000);
}
}
}
如果我替换这部分代码:
if (udpThread.listen(port)) {
String data[2];
udpThread.onPacket(\[\](AsyncUDPPacket packet) {
//parse packet
}
if (data[0] == "***") {
address = packet.remoteIP().toString();
int addressLen = address.length() + 1;
char broker[addressLen];
address.toCharArray(broker, addressLen);
mqttClient.begin(broker, net);
mqttClient.onMessage(Received); //event subscription
char* timeEnd;
timeConnect = strtol(data[1].c_str(), &timeEnd, 0);
timeOffset = millis() / 1000ul;
start = true;
}
});
有了这个:
char broker[] = "***, ***, ***, ***"; //some info
mqttClient.begin(broker, net);
mqttClient.onMessage(Received); //event subscription
然后 ESP32 停止重启。
此外,当我仅保留循环方法时,ESP32将停止重启:
void loop() {
OSCMessage msg("/ping");
msg.add("Some message");
msg.send(udpMsg);
msg.empty();
udp.sendTo(udpMsg, outIp, outPort);
delay(1000);
}