Book/stomp-Phpdoc专题
Stomp Client
目录
- 简介
- 安装/配置
- 范例
- Stomp 函数
- stomp_connect_error — Returns a string description of the last connect error
- stomp_version — Gets the current stomp extension version
- Stomp — The Stomp class
- Stomp::abort — Rolls back a transaction in progress
- Stomp::ack — Acknowledges consumption of a message
- Stomp::begin — Starts a transaction
- Stomp::commit — Commits a transaction in progress
- Stomp::__construct — 打开一个连接
- Stomp::__destruct — Closes stomp connection
- Stomp::error — Gets the last stomp error
- Stomp::getReadTimeout — Gets read timeout
- Stomp::getSessionId — Gets the current stomp session ID
- Stomp::hasFrame — Indicates whether or not there is a frame ready to read
- Stomp::readFrame — Reads the next frame
- Stomp::send — Sends a message
- Stomp::setReadTimeout — Sets read timeout
- Stomp::subscribe — Registers to listen to a given destination
- Stomp::unsubscribe — Removes an existing subscription
- StompFrame — The StompFrame class
- StompFrame::__construct — Constructor
- StompException — The StompException
class
- StompException::getDetails — Get exception details
简介
Represents a connection between PHP and a Stomp compliant Message Broker.
类摘要
Stomp
class Stomp {
/* 方法 */
public bool
abort ( <span
class="type">string $transaction_id [, <span
class="methodparam">array $headers ]
)
public bool
ack ( <span
class="type">mixed $msg [, <span
class="methodparam">array $headers ]
)
public bool
begin ( <span
class="type">string $transaction_id [, <span
class="methodparam">array $headers ]
)
public bool
commit ( <span
class="type">string $transaction_id [, <span
class="methodparam">array $headers ]
)
public <span
class="methodname">__construct ([ <span
class="methodparam">string $broker<span
class="initializer"> =
ini_get("stomp.default_broker_uri") [, <span
class="methodparam">string $username
[, string
$password [, <span
class="type">array $headers ]]]] )
public bool __destruct ( <span class="methodparam">void )
public string error ( <span class="methodparam">void )
public array getReadTimeout ( <span class="methodparam">void )
public <span class="type">stringfalse <span class="methodname">getSessionId ( <span class="methodparam">void )
public bool hasFrame ( <span class="methodparam">void )
public <span
class="type">stompframe readFrame
([ string
$class_name = "stompFrame" ]
)
public bool
send ( <span
class="type">string $destination , <span
class="methodparam">mixed $msg [,
array
$headers ] )
public void
setReadTimeout ( <span
class="methodparam">int $seconds [,
int
$microseconds ] )
public bool
subscribe ( <span
class="methodparam">string
$destination [, <span
class="type">array $headers ] )
public bool
unsubscribe ( <span
class="methodparam">string
$destination [, <span
class="type">array $headers ] )
}
Stomp::abort
stomp_abort
Rolls back a transaction in progress
说明
面向对象风格 (method):
public bool
Stomp::abort ( <span
class="methodparam">string
$transaction_id [, <span
class="type">array $headers ] )
过程化风格:
bool <span
class="methodname">stomp_abort ( <span
class="type">resource $link , <span
class="methodparam">string
$transaction_id [, <span
class="type">array $headers ] )
Rolls back a transaction in progress.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
transaction_id
The transaction to abort.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
成功时返回 true, 或者在失败时返回 false。
注释
小贴士
Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
/* begin a transaction */
$stomp->begin('t1');
/* send a message to the queue */
$stomp->send('/queue/foo', 'bar', array('transaction' => 't1'));
/* rollback */
$stomp->abort('t1');
/* close connection */
unset($stomp);
?>
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('tcp://localhost:61613');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
/* begin a transaction */
stomp_begin($link, 't1');
/* send a message to the queue 'foo' */
stomp_send($link, '/queue/foo', 'bar', array('transaction' => 't1'));
/* rollback */
stomp_abort($link, 't1');
/* close connection */
stomp_close($link);
?>
Stomp::ack
stomp_ack
Acknowledges consumption of a message
说明
面向对象风格 (method):
public bool
Stomp::ack ( <span
class="methodparam">mixed $msg [,
array
$headers ] )
过程化风格:
bool <span
class="methodname">stomp_ack ( <span
class="type">resource $link , <span
class="methodparam">mixed $msg [,
array
$headers ] )
Acknowledges consumption of a message from a subscription using client acknowledgment.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
msg
The message/messageId to be acknowledged.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
成功时返回 true, 或者在失败时返回 false。
注释
Note:
A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.
小贴士
Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.
范例
示例 #1 面向对象风格
<?php
$queue = '/queue/foo';
$msg = 'bar';
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
/* send a message to the queue 'foo' */
$stomp->send($queue, $msg);
/* subscribe to messages from the queue 'foo' */
$stomp->subscribe($queue);
/* read a frame */
$frame = $stomp->readFrame();
if ($frame->body === $msg) {
/* acknowledge that the frame was received */
$stomp->ack($frame);
}
/* remove the subscription */
$stomp->unsubscribe($queue);
/* close connection */
unset($stomp);
?>
示例 #2 过程化风格
<?php
$queue = '/queue/foo';
$msg = 'bar';
/* connection */
$link = stomp_connect('ssl://localhost:61612');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
/* begin a transaction */
stomp_begin($link, 't1');
/* send a message to the queue 'foo' */
stomp_send($link, $queue, $msg, array('transaction' => 't1'));
/* commit a transaction */
stomp_commit($link, 't1');
/* subscribe to messages from the queue 'foo' */
stomp_subscribe($link, $queue);
/* read a frame */
$frame = stomp_read_frame($link);
if ($frame['body'] === $msg) {
/* acknowledge that the frame was received */
stomp_ack($link, $frame['headers']['message-id']);
}
/* remove the subscription */
stomp_unsubscribe($link, $queue);
/* close connection */
stomp_close($link);
?>
Stomp::begin
stomp_begin
Starts a transaction
说明
面向对象风格 (method):
public bool
Stomp::begin ( <span
class="methodparam">string
$transaction_id [, <span
class="type">array $headers ] )
过程化风格:
bool <span
class="methodname">stomp_begin ( <span
class="type">resource $link , <span
class="methodparam">string
$transaction_id [, <span
class="type">array $headers ] )
Starts a transaction.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
transaction_id
The transaction id.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
成功时返回 true, 或者在失败时返回 false。
注释
小贴士
Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.
范例
See stomp_commit or <span class="function">stomp_abort.
Stomp::commit
stomp_commit
Commits a transaction in progress
说明
面向对象风格 (method):
public bool
Stomp::commit ( <span
class="methodparam">string
$transaction_id [, <span
class="type">array $headers ] )
过程化风格:
bool <span
class="methodname">stomp_commit ( <span
class="methodparam">resource $link ,
string
$transaction_id [, <span
class="type">array $headers ] )
Commits a transaction in progress.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
transaction_id
The transaction id.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
成功时返回 true, 或者在失败时返回 false。
注释
小贴士
Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
/* begin a transaction */
$stomp->begin('t1');
/* send a message to the queue */
$stomp->send('/queue/foo', 'bar', array('transaction' => 't1'));
/* commit */
$stomp->commit('t1');
/* close connection */
unset($stomp);
?>
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('tcp://localhost:61613');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
/* begin a transaction */
stomp_begin($link, 't1');
/* send a message to the queue 'foo' */
stomp_send($link, '/queue/foo', 'bar', array('transaction' => 't1'));
/* commit */
stomp_commit($link, 't1');
/* close connection */
stomp_close($link);
?>
Stomp::__construct
stomp_connect
打开一个连接
说明
面向对象风格 (constructor):
public <span
class="methodname">Stomp::__construct ([ <span
class="methodparam">string $broker<span
class="initializer"> =
ini_get("stomp.default_broker_uri") [, <span
class="methodparam">string $username
[, string
$password [, <span
class="type">array $headers ]]]] )
过程化风格:
resource <span
class="methodname">stomp_connect ([ <span
class="methodparam">string $broker<span
class="initializer"> =
ini_get("stomp.default_broker_uri") [, <span
class="methodparam">string $username
[, string
$password [, <span
class="type">array $headers ]]]] )
打开一个到兼容stomp通讯协议的消息代理服务器的连接.
参数
broker
代理服务器的统一资源标识符(URI)。
username
用户名.
password
密码.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
Note:
A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.
更新日志
| 版本 | 说明 |
|---|---|
| 1.0.1 | 增加了headers 参数 |
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
/* close connection */
unset($stomp);
?>
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('ssl://localhost:61612');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
/* close connection */
stomp_close($link);
?>
Stomp::__destruct
stomp_close
Closes stomp connection
说明
面向对象风格 (destructor):
public bool Stomp::__destruct ( <span class="methodparam">void )
过程化风格:
bool <span
class="methodname">stomp_close ( <span
class="type">resource $link )
Closes a previously opened connection.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
返回值
成功时返回 true, 或者在失败时返回 false。
范例
See stomp_connect.
Stomp::error
stomp_error
Gets the last stomp error
说明
面向对象风格 (method):
public string Stomp::error ( <span class="methodparam">void )
过程化风格:
string <span
class="methodname">stomp_error ( <span
class="type">resource $link )
Gets the last stomp error.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
返回值
Returns an error string or false if no error occurred.
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
var_dump($stomp->error());
if (!$stomp->abort('unknown-transaction', array('receipt' => 'foo'))) {
var_dump($stomp->error());
}
/* close connection */
unset($stomp);
?>
以上例程的输出类似于:
bool(false)
string(43) "Invalid transaction id: unknown-transaction"
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('ssl://localhost:61612');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
var_dump(stomp_error($link));
if (!stomp_abort($link, 'unknown-transaction', array('receipt' => 'foo'))) {
var_dump(stomp_error($link));
}
/* close connection */
stomp_close($link);
?>
以上例程的输出类似于:
bool(false)
string(43) "Invalid transaction id: unknown-transaction"
Stomp::getReadTimeout
stomp_get_read_timeout
Gets read timeout
说明
面向对象风格 (method):
public array Stomp::getReadTimeout ( <span class="methodparam">void )
过程化风格:
array <span
class="methodname">stomp_get_read_timeout ( <span
class="methodparam">resource $link )
Gets read timeout
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
返回值
Returns an array with 2 elements: sec and usec.
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
var_dump($stomp->getReadTimeout());
/* close connection */
unset($stomp);
?>
以上例程的输出类似于:
array(2) {
["sec"]=>
int(2)
["usec"]=>
int(0)
}
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('ssl://localhost:61612');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
var_dump(stomp_get_read_timeout($link));
/* close connection */
stomp_close($link);
?>
以上例程的输出类似于:
array(2) {
["sec"]=>
int(2)
["usec"]=>
int(0)
}
Stomp::getSessionId
stomp_get_session_id
Gets the current stomp session ID
说明
面向对象风格 (method):
public <span class="type">stringfalse <span class="methodname">Stomp::getSessionId ( <span class="methodparam">void )
过程化风格:
string<span
class="type">false <span
class="methodname">stomp_get_session_id ( <span
class="methodparam">resource $link )
Gets the current stomp session ID.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
返回值
string session id on success 或者在失败时返回
false.
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
var_dump($stomp->getSessionId());
/* close connection */
unset($stomp);
?>
以上例程的输出类似于:
string(35) "ID:php.net-52873-1257291895530-4:14"
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('ssl://localhost:61612');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
var_dump(stomp_get_session_id($link));
/* close connection */
stomp_close($link);
?>
以上例程的输出类似于:
string(35) "ID:php.net-52873-1257291895530-4:14"
Stomp::hasFrame
stomp_has_frame
Indicates whether or not there is a frame ready to read
说明
面向对象风格 (method):
public bool Stomp::hasFrame ( <span class="methodparam">void )
过程化风格:
bool <span
class="methodname">stomp_has_frame ( <span
class="methodparam">resource $link )
Indicates whether or not there is a frame ready to read.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
返回值
Returns true if a frame is ready to read, or false
otherwise.
Stomp::readFrame
stomp_read_frame
Reads the next frame
说明
面向对象风格 (method):
public <span
class="type">stompframe <span
class="methodname">Stomp::readFrame ([ <span
class="methodparam">string $class_name<span
class="initializer"> = "stompFrame" ] )
过程化风格:
array <span
class="methodname">stomp_read_frame ( <span
class="methodparam">resource $link )
Reads the next frame. It is possible to instantiate an object of a specific class, and pass parameters to that class's constructor.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
class_name
The name of the class to instantiate. If not specified, a stompFrame
object is returned.
返回值
Note:
A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.
更新日志
| 版本 | 说明 |
|---|---|
| Stomp 0.4.0 | class_name parameter was added. |
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
/* subscribe to messages from the queue 'foo' */
$stomp->subscribe('/queue/foo');
/* read a frame */
var_dump($stomp->readFrame());
/* close connection */
unset($stomp);
?>
以上例程的输出类似于:
object(StompFrame)#2 (3) {
["command"]=>
string(7) "MESSAGE"
["headers"]=>
array(5) {
["message-id"]=>
string(41) "ID:php.net-55293-1257226743606-4:2:-1:1:1"
["destination"]=>
string(10) "/queue/foo"
["timestamp"]=>
string(13) "1257226805828"
["expires"]=>
string(1) "0"
["priority"]=>
string(1) "0"
}
["body"]=>
string(3) "bar"
}
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('ssl://localhost:61612');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
/* subscribe to messages from the queue 'foo' */
stomp_subscribe($link, '/queue/foo');
/* read a frame */
$frame = stomp_read_frame($link);
/* close connection */
stomp_close($link);
?>
以上例程的输出类似于:
array(3) {
["command"]=>
string(7) "MESSAGE"
["body"]=>
string(3) "bar"
["headers"]=>
array(6) {
["transaction"]=>
string(2) "t1"
["message-id"]=>
string(41) "ID:php.net-55293-1257226743606-4:3:-1:1:1"
["destination"]=>
string(10) "/queue/foo"
["timestamp"]=>
string(13) "1257227037059"
["expires"]=>
string(1) "0"
["priority"]=>
string(1) "0"
}
}
Stomp::send
stomp_send
Sends a message
说明
面向对象风格 (method):
public bool
Stomp::send ( <span
class="methodparam">string
$destination , <span
class="type">mixed $msg [, <span
class="methodparam">array $headers ]
)
过程化风格:
bool <span
class="methodname">stomp_send ( <span
class="type">resource $link , <span
class="methodparam">string
$destination , <span
class="type">mixed $msg [, <span
class="methodparam">array $headers ]
)
Sends a message to the Message Broker.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
destination
Where to send the message
msg
Message to send.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
成功时返回 true, 或者在失败时返回 false。
注释
Note:
A transaction header may be specified, indicating that the message acknowledgment should be part of the named transaction.
小贴士
Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.
范例
See stomp_ack.
Stomp::setReadTimeout
stomp_set_read_timeout
Sets read timeout
说明
面向对象风格 (method):
public void
Stomp::setReadTimeout ( <span
class="methodparam">int $seconds [,
int
$microseconds ] )
过程化风格:
void <span
class="methodname">stomp_set_read_timeout ( <span
class="methodparam">resource $link ,
int
$seconds [, <span
class="type">int $microseconds ] )
Sets read timeout.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
seconds
The seconds part of the timeout to be set.
microseconds
The microseconds part of the timeout to be set.
范例
示例 #1 面向对象风格
<?php
/* connection */
try {
$stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}
$stomp->setReadTimeout(10);
/* close connection */
unset($stomp);
?>
示例 #2 过程化风格
<?php
/* connection */
$link = stomp_connect('ssl://localhost:61612');
/* check connection */
if (!$link) {
die('Connection failed: ' . stomp_connect_error());
}
stomp_set_read_timeout($link, 10);
/* close connection */
stomp_close($link);
?>
Stomp::subscribe
stomp_subscribe
Registers to listen to a given destination
说明
面向对象风格 (method):
public bool
Stomp::subscribe ( <span
class="methodparam">string
$destination [, <span
class="type">array $headers ] )
过程化风格:
bool <span
class="methodname">stomp_subscribe ( <span
class="methodparam">resource $link ,
string
$destination [, <span
class="type">array $headers ] )
Registers to listen to a given destination.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
destination
Destination to subscribe to.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
成功时返回 true, 或者在失败时返回 false。
注释
小贴士
Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.
范例
See stomp_ack.
Stomp::unsubscribe
stomp_unsubscribe
Removes an existing subscription
说明
面向对象风格 (method):
public bool
Stomp::unsubscribe ( <span
class="methodparam">string
$destination [, <span
class="type">array $headers ] )
过程化风格:
bool <span
class="methodname">stomp_unsubscribe ( <span
class="methodparam">resource $link ,
string
$destination [, <span
class="type">array $headers ] )
Removes an existing subscription.
参数
link
仅对过程化样式:由 stomp_connect 返回的
stomp 连接标识符。
destination
Subscription to remove.
headers
关联数组包含附加的头信息(例如: receipt)。
返回值
成功时返回 true, 或者在失败时返回 false。
注释
小贴士
Stomp is inherently asynchronous. Synchronous communication can be implemented adding a receipt header. This will cause methods to not return anything until the server has acknowledged receipt of the message or until read timeout was reached.
范例
See stomp_ack.
简介
Represents a message which was sent or received from a Stomp compliant Message Broker.
类摘要
StompFrame
class StompFrame {
/* 属性 */
public $command ;
public $headers ;
public $body ;
/* 方法 */
__construct ([ <span
class="methodparam">string $command
[, array
$headers [, <span
class="type">string $body ]]] )
}
属性
command
Frame command.
headers
Frame headers (array).
body
Frame body.
StompFrame::__construct
Constructor
说明
StompFrame::__construct ([ <span
class="methodparam">string $command
[, array
$headers [, <span
class="type">string $body ]]] )
Constructor.
参数
command
Frame command
headers
Frame headers (array).
body
Frame body.
简介
Represents an error raised by the stomp extension. See Exceptions for more information about Exceptions in PHP.
类摘要
StompException
class StompException <span class="ooclass"> extends Exception {
/* 继承的方法 */
final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )
final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )
final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )
final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )
final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )
final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )
public string Exception::__toString ( <span class="methodparam">void )
final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )
/* 方法 */
public string getDetails ( <span class="methodparam">void )
}
StompException::getDetails
Get exception details
说明
public string StompException::getDetails ( <span class="methodparam">void )
Get exception details.
返回值
string containing the error details.