PHP的消息通信机制测试实例


Posted in PHP onNovember 10, 2016

本文实例讲述了PHP的消息通信机制。分享给大家供大家参考,具体如下:

<?php error_reporting(E_ALL&~E_WARNING&~E_NOTICE);
/**
 * Example for sending and receiving Messages via the System V Message Queue
 *
 * To try this script run it synchron/asynchron twice times. One time with ?type=send and one time with ?type=receive
 *
 * @author     Thomas Eimers - Mehrkanal GmbH
 *
 * This document is distributed in the hope that it will be useful, but without any warranty;
 * without even the implied warranty of merchantability or fitness for a particular purpose.
 */
ob_implicit_flush(1);
header('Content-Type: text/plain; charset=ISO-8859-1');
echo "Start...\n";
// Create System V Message Queue. Integer value is the number of the Queue
//$queue = msg_get_queue(100379);
$mesg_key = ftok(__FILE__, 'm');
$mesg_id = msg_get_queue($mesg_key, 0666);
$queue = $mesg_id;
// Sendoptions
$serialize_needed=false; // Must the transfer data be serialized ?
$block_send=false;    // Block if Message could not be send (Queue full...) (true/false)
$msgtype_send=1;     // Any Integer above 0. It signeds every Message. So you could handle multible message
             // type in one Queue.
// Receiveoptions
$msgtype_receive=1;    // Whiche type of Message we want to receive ? (Here, the type is the same as the type we send,
             // but if you set this to 0 you receive the next Message in the Queue with any type.
$maxsize=100;       // How long is the maximal data you like to receive.
$option_receive=MSG_IPC_NOWAIT; // If there are no messages of the wanted type in the Queue continue without wating.
             // If is set to NULL wait for a Message.
// Send or receive 20 Messages
for ($i=0;$i<20;$i++) {
   sleep(1);
  ob_flush();
  flush();
 $message='Hello, This is Flandy,now is '.date("H:i:s",time());   // Transfering Data
 // This one sends
  if (isset($_GET['type'])&&$_GET['type']=='send') {
  if(msg_send($queue,$msgtype_send, $message,$serialize_needed, $block_send,$err)===true) {
   echo "The ".$i." Message has been sent, the messge is ".$message."\n";
  } else {
   var_dump($err);
  }
 // This one received
 } else {
   $queue_status=msg_stat_queue($queue);
   echo 'Get Messages in the queue: '.$queue_status['msg_qnum']."\n";
   print_r($queue_status);
   echo "\n";
  if ($queue_status['msg_qnum']>0) {
   if (msg_receive($queue,$msgtype_receive ,$msgtype_erhalten,$maxsize,$daten,$serialize_needed, $option_receive, $err)===true) {
       echo "Received data:".$daten."\n";
   } else {
       var_dump($err);
   }
  }
 }
}
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP中for循环语句的几种变型
Nov 26 PHP
PHP实现多条件查询实例代码
Jul 17 PHP
php class中public,private,protected的区别以及实例分析
Jun 18 PHP
php操作xml入门之xml标签的属性分析
Jan 23 PHP
php取得字符串首字母的方法
Mar 25 PHP
PHP实现上传文件并存进数据库的方法
Jul 16 PHP
JSON字符串传到后台PHP处理问题的解决方法
Jun 05 PHP
PHP5.5安装PHPRedis扩展及连接测试方法
Jan 22 PHP
PHP折半(二分)查找算法实例分析
May 12 PHP
php实现微信分享朋友链接功能
Feb 18 PHP
Thinkphp 框架扩展之驱动扩展实例分析
Apr 27 PHP
PHP中国际化的字符串排序和比较对象详解
Aug 23 PHP
PHP使用GD库输出汉字的方法【测试可用】
Nov 10 #PHP
Yii2框架RESTful API 格式化响应,授权认证和速率限制三部分详解
Nov 10 #PHP
PHP基于反射机制实现插件的可插拔设计详解
Nov 10 #PHP
PHP yii实现model添加默认值的方法(两种方法)
Nov 10 #PHP
PHP实现的曲线统计图表示例
Nov 10 #PHP
PHP  Yii清理缓存的实现方法
Nov 10 #PHP
PHP模拟http请求的方法详解
Nov 09 #PHP
You might like
解析php addslashes()与addclashes()函数的区别和比较
2013/06/24 PHP
PHP header()函数常用方法总结
2014/04/11 PHP
PHP中使用addslashes函数转义的安全性原理分析
2014/11/03 PHP
PHP5.6新增加的可变函数参数用法分析
2017/08/25 PHP
php layui实现前端多图上传实例
2019/07/30 PHP
JavaScript 事件的一些重要说明
2009/10/25 Javascript
Jquery调用webService远程访问出错的解决方法
2010/05/21 Javascript
由Javascript实现的页面日历
2011/11/04 Javascript
Jquery为a标签的href赋值实现代码
2013/05/03 Javascript
js中opener与parent的区别详细解析
2014/01/14 Javascript
js函数调用的方式
2014/05/06 Javascript
jQuery获取动态生成的元素示例
2014/06/15 Javascript
AngularJS入门教程之AngularJS模型
2016/04/18 Javascript
Javascript 调用 ActionScript 的简单方法
2016/09/22 Javascript
react-router JS 控制路由跳转实例
2017/06/15 Javascript
JS仿淘宝搜索框用户输入事件的实现
2017/06/19 Javascript
vue-cli配置文件——config篇
2018/01/04 Javascript
如何使用JavaScript实现无缝滚动自动播放轮播图效果
2020/08/20 Javascript
[49:29]LGD vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
python练习程序批量修改文件名
2014/01/16 Python
Python自动登录126邮箱的方法
2015/07/10 Python
Python浅复制中对象生存周期实例分析
2018/04/02 Python
Python GUI编程完整示例
2019/04/04 Python
如何通过Python实现标签云算法
2019/07/02 Python
python cv2.resize函数high和width注意事项说明
2020/07/05 Python
用python爬虫批量下载pdf的实现
2020/12/01 Python
公益广告语集锦
2014/03/13 职场文书
留学顾问岗位职责
2014/04/14 职场文书
迎国庆横幅标语
2014/10/08 职场文书
自我检讨报告
2015/01/28 职场文书
2015年七一建党节演讲稿
2015/03/19 职场文书
村级干部党员公开承诺事项
2015/05/04 职场文书
解约证明模板
2015/06/19 职场文书
MySQL创建高性能索引的全步骤
2021/05/02 MySQL
用Python创建简易网站图文教程
2021/06/11 Python
golang使用map实现去除重复数组
2022/04/14 Golang