[英]sending messages using php with voryx Thruway WAMP messaging system
我正在嘗試構建通知消息系統。 我使用SimpleWsServer.php服務器示例。 我想在服務器上完成任務時將通知推送到用戶的瀏覽器。 這需要使用PHP完成,我無法找到顯示它的教程。 當PHP服務器作為管理器運行時,所有教程似乎都顯示了發送和接收的tavendo / AutobahnJS腳本。
是否可以使用php腳本向訂閱者發送消息?
天文,
這實際上非常簡單,可以通過幾種不同的方式完成。 我們設計了Thruway客戶端來模仿AutobahnJS客戶端,因此大多數簡單示例都將直接翻譯。
我假設你想從一個網站發布(不是一個長期運行的PHP腳本)。
在您的PHP網站中,您需要執行以下操作:
$connection = new \Thruway\Connection(
[
"realm" => 'com.example.astro',
"url" => 'ws://demo.thruway.ws:9090', //You can use this demo server or replace it with your router's IP
]
);
$connection->on('open', function (\Thruway\ClientSession $session) use ($connection) {
//publish an event
$session->publish('com.example.hello', ['Hello, world from PHP!!!'], [], ["acknowledge" => true])->then(
function () use ($connection) {
$connection->close(); //You must close the connection or this will hang
echo "Publish Acknowledged!\n";
},
function ($error) {
// publish failed
echo "Publish Error {$error}\n";
}
);
});
$connection->open();
而javascript客戶端(使用AutobahnJS)將如下所示:
var connection = new autobahn.Connection({
url: 'ws://demo.thruway.ws:9090', //You can use this demo server or replace it with your router's IP
realm: 'com.example.astro'
});
connection.onopen = function (session) {
//subscribe to a topic
function onevent(args) {
console.log("Someone published this to 'com.example.hello': ", args);
}
session.subscribe('com.example.hello', onevent).then(
function (subscription) {
console.log("subscription info", subscription);
},
function (error) {
console.log("subscription error", error);
}
);
};
connection.open();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.