-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.php
67 lines (55 loc) · 2.01 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
use voniersa\twitch\livechat\ChannelCollection;
use voniersa\twitch\livechat\ChannelName;
use voniersa\twitch\livechat\ConnectionVerifier;
use voniersa\twitch\livechat\IncorrectDataProvidedException;
use voniersa\twitch\livechat\IrcConnector;
use voniersa\twitch\livechat\Message;
use voniersa\twitch\livechat\MessageObserver;
use voniersa\twitch\livechat\Password;
use voniersa\twitch\livechat\SocketConnectionException;
use voniersa\twitch\livechat\TwitchChatAdapter;
use voniersa\twitch\livechat\UserName;
require "./vendor/autoload.php";
class ExampleObserver implements MessageObserver
{
public function update(Message $message): void
{
echo sprintf(
"%s:%s: \"%s\"\n",
$message->getChannelName(),
$message->getUserName(),
$message
);
}
}
$adapter = new TwitchChatAdapter(new IrcConnector(), new ConnectionVerifier());
try {
$connection = $adapter->openConnection(
UserName::fromString($argv[1]),
Password::fromString($argv[2]),
ChannelCollection::fromArray([ChannelName::fromString($argv[3])])
);
$connection->attach(new ExampleObserver());
$adapter->writeMessage($connection, ChannelName::fromString($argv[3]), "Start");
$messages = $adapter->readLiveChat($connection, 30);
$messageIterator = $messages->getIterator();
echo PHP_EOL;
echo "These are all messages sent in the last 30 seconds:";
echo PHP_EOL;
while ($messageIterator->valid()) {
$messageIterator->current();
echo sprintf(
"%s:%s: \"%s\"\n",
$messageIterator->current()->getChannelName(),
$messageIterator->current()->getUserName(),
$messageIterator->current()
);
$messageIterator->next();
}
$adapter->writeMessage($connection, ChannelName::fromString($argv[3]), "End");
$adapter->closeConnection($connection);
} catch (IncorrectDataProvidedException|SocketConnectionException $e) {
echo $e->getMessage();
}