连接到Twitch IRC聊天

时间:2015-07-18 13:41:27

标签: php sockets irc twitch

您好我已经尝试连接到Twitch IRC聊天,所以我可以尝试制作一个简单的聊天机器人,但我正在努力使其工作。

获取错误:http://puu.sh/j3HwK/173a0388fb.png

这是代码:

  #define height 170
  #define width 70
  #define linewidth 5

- (void)drawRect:(CGRect)rect {

  UIBezierPath *leftPath = [[UIBezierPath alloc] init];

  [[UIColor blueColor] setStroke];
  leftPath.lineCapStyle = kCGLineCapSquare;
  leftPath.lineJoinStyle = kCGLineJoinBevel;
  leftPath.lineWidth = linewidth;

  [leftPath moveToPoint:CGPointMake(0, 0)];
  [leftPath addLineToPoint:CGPointMake(width, 0)];
  [leftPath moveToPoint:CGPointMake(0, 0)];

  [leftPath addLineToPoint:CGPointMake(0, height)];
  [leftPath addLineToPoint:CGPointMake(width, height)];

  [leftPath stroke];
}

所以基本上我不能让它连接到Twitch IRC,如果有人可以帮助我,我将不胜感激! :)

2 个答案:

答案 0 :(得分:1)

我知道几年前有人问过这个问题,但今天也许可以帮助别人:

<?php    
set_time_limit(0);
ini_set('display_errors', 'on');
$server = array();    

function IRCBot()
{
    function IRC()
    {
        $server global;
        $config = array(
                'server'  => 'ssl://irc.chat.twitch.tv', 
                'port'    => 6697, 
                'channel' => '#twitch_channel',
                'nick'    => strtolower('twitch_username'), 
                'pass'    => 'oauth:twitch_oauth_token' //http://twitchapps.com/tmi/
        );

        $server['connect'] = @fsockopen($config['server'], $config['port']);

        if($server['connect'])
        {
            echo "[<] Starting connection with user: " . $config['nick'];
            SendData('CAP REQ :twitch.tv/tags');
            SendData('CAP REQ :twitch.tv/commands');
            SendData('CAP REQ :twitch.tv/membership');
            SendData("PASS " . $config['pass']);
            SendData("NICK " . $config['nick']);
            SendData("USER " . $config['nick'] . "  1 1 :" . $config['nick']);
            SendData("JOIN " . $config['channel']);

            while(!feof($server['connect']))
            {
               $server['READ_BUFFER'] = fgets($server['connect'], 1024);
               echo "[>] " . $server['READ_BUFFER'];
               flush();
            }
        }
    }

    function SendData($cmd)
    {
        global $server;
        @fwrite($server['connect'], $cmd . "\r\n");
        echo "[<] $cmd \r\n";
    }

    IRC();

}

IRCBot();

除了要发送到twitch的命令格式外,要注意的最重要的事情是,fdata函数上的SendData函数不发送strlen “ \ r \ n” 附加到命令字符串。

答案 1 :(得分:0)

IRC()函数中,您已声明$server,但未将其标记为全局变量,因此SendData(...)中无法使用它。

SendData(...)然后查看$server的全局版本,发现它没有名为connect的数组元素,所以返回null,这是你的错误所在来自。

global $server;之前添加$server = array();,然后重试。

作为第二点,我认为行返回需要\r\n而不是\n\r,但我不确定 - 它将取决于IRC服务器的严格程度。< / p>

相关问题