所以这是我遇到的一个非常有趣的问题。我一心想弄清楚如何将 websocket 客户端集成到 Laravel 5.5 中,以允许在我的应用程序和Discord Gateway之间通过 websocket 进行通信。我通过 Composer 构建了一个新的 Laravel 应用程序并需要这个库,它构建在Ratchet PHP之上。
我正在尝试在我使用的这个机器人之后构建一个 PHP Discord Bot,但在它使用的主要依赖项也被终止后被放弃。
我已经想出了如何添加一个计时器来发送这样的心跳
$app->addTimer(x, function ($thing) use ($etc) {});
在我的应用程序收到来自 Discord 的事件之前,这非常有效。然后同步丢失,而不是以hello 事件的确定间隔发送心跳,我的应用程序开始每 3-9 秒发送一次,有时一次发送 2 或 3 个。这是出于调试原因从控制台输出的一些输出,但它显示了我的问题:
"Sending Heartbeat - 41 - 2017-12-31 05:26:42"
"Sending Heartbeat - 41 - 2017-12-31 05:26:42"
Illuminate\Support\Collection {#679
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
Illuminate\Support\Collection {#679
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:26:51"
Illuminate\Support\Collection {#602
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:26:55"
Illuminate\Support\Collection {#695
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:26:56"
Illuminate\Support\Collection {#688
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
Illuminate\Support\Collection {#688
#items: array:4 [
"t" => "TYPING_START"
"s" => 7
"op" => 0
"d" => array:3 [
"user_id" => "277968564827324416"
"timestamp" => 1514698064
"channel_id" => "394991263344230411"
]
]
}
Illuminate\Support\Collection {#688
#items: array:4 [
"t" => "MESSAGE_CREATE"
"s" => 8
"op" => 0
"d" => array:15 [
"type" => 0
"tts" => false
"timestamp" => "2017-12-31T05:27:47.057000+00:00"
"pinned" => false
"nonce" => "396897209817235456"
"mentions" => []
"mention_roles" => []
"mention_everyone" => false
"id" => "396897202448105494"
"embeds" => []
"edited_timestamp" => null
"content" => "!about"
"channel_id" => "394991263344230411"
"author" => array:4 [
"username" => "David Davaham"
"id" => "277968564827324416"
"discriminator" => "2471"
"avatar" => "0c27e1bed49121e8aaf3f284d6b74e55"
]
"attachments" => []
]
]
}
Illuminate\Support\Collection {#688
#items: array:4 [
"t" => "MESSAGE_CREATE"
"s" => 9
"op" => 0
"d" => array:15 [
"type" => 0
"tts" => false
"timestamp" => "2017-12-31T05:27:49.382000+00:00"
"pinned" => false
"nonce" => null
"mentions" => array:1 [
0 => array:4 [
"username" => "David Davaham"
"id" => "277968564827324416"
"discriminator" => "2471"
"avatar" => "0c27e1bed49121e8aaf3f284d6b74e55"
]
]
"mention_roles" => []
"mention_everyone" => false
"id" => "396897212199862276"
"embeds" => []
"edited_timestamp" => null
"content" => "<@!277968564827324416> Unfortunately That is not a command I recognize. Please try again. Reply with `!help` for a list of commands"
"channel_id" => "394991263344230411"
"author" => array:5 [
"username" => "Claire Underwood (Dev)"
"id" => "394988052360986635"
"discriminator" => "8397"
"bot" => true
"avatar" => null
]
"attachments" => []
]
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:12"
Illuminate\Support\Collection {#616
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:23"
"Sending Heartbeat - 41 - 2017-12-31 05:27:23"
Illuminate\Support\Collection {#622
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
Illuminate\Support\Collection {#622
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:32"
Illuminate\Support\Collection {#652
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:36"
Illuminate\Support\Collection {#667
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:37"
Illuminate\Support\Collection {#661
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:46"
Illuminate\Support\Collection {#663
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:49"
Illuminate\Support\Collection {#660
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:51"
Illuminate\Support\Collection {#656
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:27:53"
Illuminate\Support\Collection {#698
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:28:04"
"Sending Heartbeat - 41 - 2017-12-31 05:28:04"
Illuminate\Support\Collection {#701
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
Illuminate\Support\Collection {#701
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:28:13"
Illuminate\Support\Collection {#706
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:28:17"
Illuminate\Support\Collection {#707
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
"Sending Heartbeat - 41 - 2017-12-31 05:28:18"
Illuminate\Support\Collection {#709
#items: array:4 [
"t" => null
"s" => null
"op" => 11
"d" => null
]
}
这是代码:
$conn->on('message', function(MessageInterface $msg) use ($conn, $socket, $loop) {
$message = collect(json_decode($msg, true));
dump($message);
if ($message->has('s') && $message->get('s') !== null) {
$this->seq = $message->get('s');
}
if (!$this->is_ready) {
if (!$message->has('op')) {
$conn->close();
}
if ($message->get('op') == 0) {
if ($message->get('t') === "READY" || $message->get('t') === "GUILD_CREATE") {
$this->is_ready = true;
$this->seq = $message->get('s');
}
}
if ($message->get('op') == 10) {
$this->connOpened = Carbon::now();
$this->heartbeat = (int)floor($message->get('d')['heartbeat_interval'] / 1000);
$socket->sendIdentify();
sleep(1);
}
if ($message->get('op') == 11) {
$now = Carbon::now()->timestamp;
if (!$this->heartbeatACK) {
$this->heartbeatACK = true;
}
}
}
if ($this->is_ready) {
if ($message->get('op') == 0) {
if ($message->get('t') === "MESSAGE_CREATE") {
$trigger = config('discord.message.trigger');
$data = $message->get('d');
$msgContent = $data['content'];
if (starts_with($msgContent, $trigger)) {
ProcessMessage::dispatch($data);
}
}
}
$now = Carbon::now();
$loop->addTimer($this->heartbeat, function ($x) use ($now, $conn, $socket) {
dump("Sending Heartbeat - " . $this->heartbeat . " - " .$now->toDateTimeString());
$payload = collect([
'op' => 1,
'd' => (int)$this->seq,
]);
$conn->send($payload->toJson());
});
}
});
有没有人知道可以更好地管理心跳,或者这只是我需要忍受的事情?
另外,有人对我的做法有什么建议或批评吗?我找不到任何关于如何执行此操作的可靠文档,所以我在进行过程中将其拼凑起来。