通过voryx Throuway WAMP消息传递系统使用php发送消息

通过voryx Throuway WAMP消息传递系统使用php发送消息,php,zeromq,autobahn,wamp-protocol,thruway,Php,Zeromq,Autobahn,Wamp Protocol,Thruway,我正在尝试构建一个通知消息系统。我正在使用服务器示例。我想在服务器上完成任务后将通知推送到用户的浏览器。这需要使用PHP来完成,我找不到一个教程显示这一点。所有教程似乎都显示了在PHP服务器作为管理器运行时要发送和接收的tavendo/AutobahnJS脚本 是否可以使用php脚本向订阅者发送消息?Astro 这实际上非常简单,可以通过两种不同的方式实现。我们设计了Throuway客户端来模拟AutobahnJS客户端,因此大多数简单的示例都将直接翻译 我假设您希望从一个网站(不是一个长时间运

我正在尝试构建一个通知消息系统。我正在使用服务器示例。我想在服务器上完成任务后将通知推送到用户的浏览器。这需要使用PHP来完成,我找不到一个教程显示这一点。所有教程似乎都显示了在PHP服务器作为管理器运行时要发送和接收的tavendo/AutobahnJS脚本

是否可以使用php脚本向订阅者发送消息?

Astro

这实际上非常简单,可以通过两种不同的方式实现。我们设计了Throuway客户端来模拟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();
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();
javascript客户端(使用AutobahnJS)将如下所示:

$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();
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();

我还为javascript端创建了一个,为PHP端创建了一个。

不确定您想拉什么,但似乎Ajax是解决问题的方法?@Naruto Ajax不是实时的哦,天哪,$connection->close();为什么我以前没有得到呢。非常感谢。我喜欢你们模仿高速公路的方式。再次感谢!可运行链接似乎不再起作用。我可以一次一个成功地运行这段代码,但我希望保持套接字打开,并从PHP端快速地钩住它。这是怎么回事?@JoeLeonard你知道怎么保持连接畅通吗?我即将面临同样的挑战,任何帮助都将不胜感激。我最终安装了ZeroMQ。我发现使用这种方法的服务器推送速度太慢了。使用ZeroMQ,我可以在8毫秒内从服务器推送大约1000条消息。