-
Notifications
You must be signed in to change notification settings - Fork 6
/
custom-messages.php
135 lines (113 loc) · 3.97 KB
/
custom-messages.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* This file is part of the Stomp package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require __DIR__ . '/../vendor/autoload.php';
use Stomp\Client;
use Stomp\Network\Connection;
use Stomp\StatefulStomp;
use Stomp\Transport\Message;
/**
* Class MyCustomMessage
*
* - Will be used for send and receive type safe messages.
* - It's important that the `body` property is up to date when the message is going to be transmitted.
* Overriding `__toString` assures that.
* - If you're only into receiving a type safe message, you should only extend from Frame. It also simplifies the constructor.
*/
class MyCustomMessage extends Message
{
/**
* @var string
*/
private $user;
/**
* @var DateTime
*/
private $time;
/**
* MyCustomMessage constructor.
* @param string $user
* @param DateTime $time
*/
public function __construct($user, DateTime $time)
{
$this->user = $user;
$this->time = $time;
parent::__construct(
$this->generateBody(),
['content-type' => 'text/MyCustomMessage'] // we build our message resolver based on a custom content-type
);
}
/**
* @return string
*/
public function getUser()
{
return $this->user;
}
/**
* @return DateTime
*/
public function getTime()
{
return $this->time;
}
/**
* Updates the current message body, so that it can be used for transmission.
*
* @return string
*/
private function generateBody()
{
return $this->user . '|' . $this->time->getTimestamp() . '|' . $this->time->getTimezone()->getName();
}
/**
* Update the message string representation, as this is what is going to be transmitted.
*
* @return string
*/
public function __toString()
{
// we just update the body property and leave the default logic
$this->body = $this->generateBody();
return parent::__toString();
}
}
// setup a connection
$connection = new Connection('tcp://127.0.0.1:61010');
// the frame parser is part of the connection logic, it makes uses of a FrameFactory
// we inject a custom frame resolver inside that factory
$connection->getParser()->getFactory()->registerResolver(
function ($command, array $headers, $body) {
// we get the current command, the frame headers and the content
// if we see our specific content-type this resolver takes over and returns a specific frame instance
if ($command === 'MESSAGE' && isset($headers['content-type']) && $headers['content-type'] == 'text/MyCustomMessage') {
if (preg_match('/^(.+)\|(\d+)\|(.+)$/', $body, $matches)) {
$date = DateTime::createFromFormat('U', intval($matches[2]), new DateTimeZone($matches[3]));
$date->setTimezone(new DateTimeZone($matches[3]));
$user = $matches[1];
return new MyCustomMessage($user, $date);
}
}
// not needed - just to clarify that another (or the default) resolver will be used than
return null;
}
);
// all clients (even the simple client) will make use of our frame resolver
$stomp = new StatefulStomp(new Client($connection));
$stomp->subscribe('/queue/examples');
// send a custom message (the resolver is not used for this, the message itself contains the logic for transmitting)
$stomp->send(
'/queue/examples',
new MyCustomMessage('grumpy stompy', new DateTime('yesterday 18:00', new DateTimeZone('Africa/Windhoek')))
);
// here our resolver will be used to initialize a new instance of "MyCustomMessage" otherwise we would receive only "Message"
$message = $stomp->read();
/** @var $message MyCustomMessage */
echo get_class($message), PHP_EOL;
echo sprintf('Message from %s (%s)', $message->getUser(), $message->getTime()->format('Y-m-d H:i:s e'));
$stomp->unsubscribe();