rapsspider/cake_ratchet

CakePHP 3.X 的 Ratchet 插件

安装: 30

依赖: 0

建议者: 0

安全性: 0

星标: 2

关注者: 2

分支: 1

类型:插件

dev-master 2015-07-08 21:26 UTC

This package is not auto-updated.

Last update: 2024-10-02 09:16:13 UTC


README

要求

ZeroMQ : http://www.zeromq.org/

安装

composer require rapsspider/cake_ratchet

配置

在您的 config\app.php 文件末尾添加此内容

/**
 * Ratchet configuration
 */
'CakeRatchet' => [
    'Server' => [
        'host' => '0.0.0.0',
        'port' => 8080
    ],
    'ZMQServer' => [
        'host' => '127.0.0.1',
        'port' => 5555
    ],
    'JSHelper' => true
]

在您的 config\bootstrap.php 文件中添加此内容

Plugin::load('CakeRatchet', ['bootstrap' => true]);

您可能需要在您的 vendors/cakephp-plugins.php 文件中添加此内容

...
'plugins' => [
    ...
    'CakeRatchet' => $baseDir . '/vendor/rapsspider/cake_ratchet/',
    ...
]

启动

首先,您需要启动服务器,为此,只需在 cakephp 文件夹中运行此命令:.\bin\cake ratchet start

示例

服务器

namespace App\Controller;
use (@TODO)\Pusher;

public class MyController {

    public function index() {
        Pusher::send('my_topic','my_message');
    }
    
}

客户端

如果激活了 JSHelper,则将有一个函数可用

<script>
var onConnect = function(connection) {
    connection.subscribe('my_topic', function(topic, data) {
        // This is where you would add the new article to the DOM (beyond the scope of this tutorial)
        console.log('New article published to category "' + topic + '" : ');
        console.log(data);
    });
};

var onClose = function(connection) {

};

CakeRatchet.connection(onConnect, onClose); 
</script>

否则,您可以这样做

<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
	var conn = new ab.Session('ws://:8080',
		function() {
			conn.subscribe('my_topic', function(topic, data) {
				// This is where you would add the new article to the DOM (beyond the scope of this tutorial)
				console.log('New article published to category "' + topic + '" : ');
				console.log(data);
			});
			console.log('Connexion réussie');
		},
		function() {
			console.warn('WebSocket connection closed');
		},
		{'skipSubprotocolCheck': true}
	);
</script>