我有多个客户端向服务器发送消息。当没有超过5秒的新消息时,我试图检测到这种情况。这是我的代码(我删除了许多不相关的行):
ERROR TypeError: Cannot assign to read only property 'list' of object '#<HTMLInputElement>'
问题是在最后一条消息(休息开始时)之后,当前时间和最后一条消息的时间之间的差异仅被检查一次,等于0秒。然后服务器似乎只是等待这行中的消息:
listen(sockfd, 5000);
time_t new_msg;
time(&new_msg);
while (true) {
time_t current;
time(¤t);
//if more than 5 seconds passed since the last message:
if (difftime(current, new_msg) > 5) {
//do something
}
client = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (client < 0) {
cout << "Error on accepting" << endl;
continue;
}
n = read(client, buffer, read_len - 1);
if (n < 0) cout << "Error on reading from the socket" << endl;
else if (n == 0) continue;
time(&new_msg); //save time for the last message
//if everything is ok, do something with the message
}
如何重新检查时间?或者有没有更简单的方法来检查服务器空闲多长时间?
更新
我在client = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
之前创建了以下循环:
accept
它看起来不干净,但至少有效。
答案 0 :(得分:1)
你能试试吗?
on_message !test
in test function
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\indit\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 223, in _run_event
yield from coro(*args, **kwargs)
File "bot.py", line 15, in on_message
await test(author, message)
File "bot.py", line 21, in test
await client.send_message(message.channel, 'Hi %s, i heard you.' % author)
AttributeError: 'Client' object has no attribute 'send_message'
另一种可能性是使用fcntl(sockfd, F_SETFL, O_NONBLOCK);
listen(sockfd, 5000);
time_t new_msg;
time(&new_msg);
while (true) {
time_t current;
...
或select()
代替poll()
。