您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis組件 -連接
发布时间:2021-12-23 22:46:52编辑:雪饮阅读()
鏈接就很簡單了,這裏主要是演示下帶回調和不帶回調這兩種方式。
實例:
<?php
use Workerman\Worker;
use Workerman\Redis\Client;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:6161');
$worker->onWorkerStart = function() {
global $redis;
// 省略回调
$redis = new Client('redis://127.0.0.1:6379');
// 带回调
$redis = new Client('redis://127.0.0.1:6379', [
'connect_timeout' => 10 // 设置连接超时10秒,不设置則默认為5秒
], function ($success) use($redis) {
// 连接结果回调
if (!$success) echo $redis->error();
});
};
$worker->onMessage = function(TcpConnection $connection, $data) {
global $redis;
$redis->set('key', 'hello world');
// 设置回调函数判断set调用结果
$redis->set('key', 'value', function ($result, $redis) {
var_dump($result); // true
});
// 回调函数都是可选测参数,这里省略了回调函数
$redis->set('key1', 'value1');
// 回调函数可以嵌套
$redis->get('key', function ($result, $redis){
$redis->set('key2', 'value2', function ($result) {
var_dump($result);
});
});
};
Worker::runAll();
實例運行並有客戶端發來消息時:
[root@localhost www.fpm.com]# /usr/local/php734/bin/php channelServer.php start
Workerman[channelServer.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root none http://0.0.0.0:6161 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
bool(true)
bool(true)
bool(true)
bool(true)
关键字词:workerman,redis,組件,連接
相关文章
- workerman-redis組件 - 回調函數
- workerman-redis組件
- workerman-mysql組件-其它數據庫類=medoo的數據庫類的
- workerman-mysql组件-其它数据库类使用-thinkphp(thin
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm