PHP - IRC Bot 脚本挂起

发布于 2024-10-03 15:21:27 字数 1617 浏览 10 评论 0原文

我的 IRC Bot 脚本有问题,我已将其实施到我的 Curl 传输方法中。

我有一个问题,一旦 IRC 机器人向 IRC 频道发送消息,脚本末尾的所有“回显”都不会显示,并且页面会挂起。整个阿帕奇挂了。

<?php

$ircServer = "///";
$ircPort = "6667";
$ircChannel = "#bots";

set_time_limit(0);

$msg = $_POST['msg'];
$paper = $_POST['paper'];
$sizzor = $_POST['sizzor'];
$hand = $_POST['hand'];

$ircSocket = fsockopen($ircServer, $ircPort, $eN, $eS);

if ($ircSocket)
{

    fwrite($ircSocket, "USER Lost rawr.test lol :code\n");
    fwrite($ircSocket, "NICK Rawr" . rand() . "\n");
    fwrite($ircSocket, "JOIN " . $ircChannel . "\n");

ignore_user_abort(TRUE); // Noob Close down page

    fwrite($ircSocket, "PRIVMSG " . $ircChannel . " :" . $msg . "\n");

    while(1)
    {
        while($data = fgets($ircSocket, 128))
        {
            echo nl2br($data);
            flush();

            // Separate all data
            $exData = explode(' ', $data);

            // Send PONG back to the server
            if($exData[0] == "PING")
            {
                fwrite($ircSocket, "PONG ".$exData[1]."\n");
            }
}
    echo $eS . ": " . $eN;
}
}
?>
if ($bootcontents == 'success') {     
 echo '<center><marquee behavior="alternate" direction="left">Spinning xxx at ' . $power . '% power.</marquee></center>';

这部分在脚本过程中不会显示:

if ($bootcontents == 'success') { 
    echo '<center><marquee behavior="alternate" direction="left">Spinning xxx at ' . $power . '% power.</marquee></center>';

页面只是挂起,如果我将 exit(); 函数添加到顶部附近,则整个“echo”信息不会显示。

请有人帮忙。

I am having problem with my IRC Bot script, I have implemented it into my Curl transfer method.

I have a problem, once the IRC bot sends a message to the IRC channel, all of the "echo" at the end of the script does not show and the page hangs. The whole Apache hangs.

<?php

$ircServer = "///";
$ircPort = "6667";
$ircChannel = "#bots";

set_time_limit(0);

$msg = $_POST['msg'];
$paper = $_POST['paper'];
$sizzor = $_POST['sizzor'];
$hand = $_POST['hand'];

$ircSocket = fsockopen($ircServer, $ircPort, $eN, $eS);

if ($ircSocket)
{

    fwrite($ircSocket, "USER Lost rawr.test lol :code\n");
    fwrite($ircSocket, "NICK Rawr" . rand() . "\n");
    fwrite($ircSocket, "JOIN " . $ircChannel . "\n");

ignore_user_abort(TRUE); // Noob Close down page

    fwrite($ircSocket, "PRIVMSG " . $ircChannel . " :" . $msg . "\n");

    while(1)
    {
        while($data = fgets($ircSocket, 128))
        {
            echo nl2br($data);
            flush();

            // Separate all data
            $exData = explode(' ', $data);

            // Send PONG back to the server
            if($exData[0] == "PING")
            {
                fwrite($ircSocket, "PONG ".$exData[1]."\n");
            }
}
    echo $eS . ": " . $eN;
}
}
?>
if ($bootcontents == 'success') {     
 echo '<center><marquee behavior="alternate" direction="left">Spinning xxx at ' . $power . '% power.</marquee></center>';

This part does not show during the script:

if ($bootcontents == 'success') { 
    echo '<center><marquee behavior="alternate" direction="left">Spinning xxx at ' . $power . '% power.</marquee></center>';

The page just hangs, if I add the exit(); function onto near the top the whole "echo" info does not show.

Please can someone help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜唯美灬不弃 2024-10-10 15:21:27

您正在创建一个无限循环:

while (1)
// ...

该循环永远无法完成,因为您没有使用 exit 语句(如 break)。因此,无限循环之后的代码永远不会被执行。
此外,它是一个繁忙的循环(使用大量的CPU资源),所以整个apache(和计算机)将挂起。

You are creating an infinite loop:

while (1)
// ...

This loop can never finish, since you did not use an exit statement (like break). Therefore the code after the infinite loop is never executed.
Furthermore is it a busy loop (using a lot of CPU resources), so the whole apache (and computer) will hang.

倚栏听风 2024-10-10 15:21:27

您在 标记之外留下了一些行,因此它们之外的任何内容都将被视为纯文本。您可以将结束 ?> 标记进一步向下移动来修复它:

        [this is the while(1) closing bracket]
        }
    // code past this line will never run, see below for details
    echo $eS . ": " . $eN;
    }
}
if ($bootcontents == 'success') {
  echo '<center><marquee behavior="alternate" direction="left">Spinning xxx at ' . $power . '% power.</marquee></center>';
}
?> <!-- closing tag goes here -->

页面无论如何都会无法正常工作,因为缺少 while(1) 循环退出条件:

while(1) {
    while($data = fgets($ircSocket, 128)) {
      // ...
    }
}

内部 while 完成后,您的脚本将继续循环,最终陷入空的无限循环(如果未配置为检测并杀死这种类型,这将挂起服务器)漏洞)。

最后一点,PHP 可能不是完成这项工作的最佳工具:使用独立的应用程序会更好。

You're leaving some lines out of the <?php ?> tags, so whatever is outside them will be treated as plain text. You fix it moving the closing ?> tags further down:

        [this is the while(1) closing bracket]
        }
    // code past this line will never run, see below for details
    echo $eS . ": " . $eN;
    }
}
if ($bootcontents == 'success') {
  echo '<center><marquee behavior="alternate" direction="left">Spinning xxx at ' . $power . '% power.</marquee></center>';
}
?> <!-- closing tag goes here -->

The page would anyway not work properly because the while(1) loop is missing an exit condition:

while(1) {
    while($data = fgets($ircSocket, 128)) {
      // ...
    }
}

After the inner while finishes, your script keeps looping, ending up trapped in an empty, infinite loop (which would hang the server up, if it's not configured to detect and kill this kind of loophole).

On a final notice, PHP isn't probably the best tool for the job: you would be much better off with a stand-alone application.

森林迷了鹿 2024-10-10 15:21:27
while($data = fgets($ircSocket, 128))

这部分会阻止脚本运行,直到它接收到数据,如果不知何故你没有通过该套接字获取数据...那么你就被困在那里...永远...哈哈,好吧,卡住直到 PHP 脚本超时。

如果该部分没有捕获,您仍然陷入 while 循环中,因此无法运行代码中回显内容的部分......所以 apfelbox 和 Alex 都是正确的,只是没有完全解释...

为了拥有无限循环但也能够在外部运行代码,您需要捕获要捕获和运行代码的“事件”。您想要捕获的所有事件都需要位于 while 循环内,或者至少从 while 循环分派到一个可以解析服务器输入并正确响应的函数。

更好的方法是利用观察者模式。

我真的不会用 PHP 制作 IRC 机器人,即使您通过命令行运行它...... PHP 并不意味着作为长时间运行的应用程序运行。

while($data = fgets($ircSocket, 128))

This part blocks the script running until it receives data, and if somehow you're not getting data through that socket... well you're stuck there... forever... lol ok, stuck until the PHP script times out.

If that part doesn't catch, you're still stuck inside the while loop and so there is no way of ever running the part of your code that echos stuff out... so both apfelbox and Alex are correct, just not explained fully...

In order to have a infinite loop but also be able to run code outside, you would need to catch the "event" in which you want to capture and run code. All the events you want to capture would need to sit inside the while loop, or at least dispatched from the while loop to a function that would parse the input from server and respond correctly.

An even better way to do this is to utilise the observer pattern.

I really wouldn't make an IRC bot with PHP, even if you run it via commandline... PHP isn't meant to run as a long-running application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文