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 相关文章推荐
IIS下配置Php+Mysql+zend的图文教程
Dec 08 PHP
10条PHP高级技巧[修正版]
Aug 02 PHP
hadoop常见错误以及处理方法详解
Jun 19 PHP
利用php下载xls文件(自己动手写的)
Apr 18 PHP
Yii框架中 find findAll 查找出制定的字段的方法对比
Sep 10 PHP
最新版本PHP 7 vs HHVM 多角度比较
Feb 14 PHP
Thinkphp和onethink实现微信支付插件
Apr 13 PHP
Thinkphp 空操作、空控制器、命名空间(详解)
May 05 PHP
Yii 2.0自带的验证码使用经验分享
Jun 19 PHP
thinkPHP3.2.3结合Laypage实现的分页功能示例
May 28 PHP
PDO::inTransaction讲解
Jan 28 PHP
PHP 超级全局变量相关总结
Jun 30 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
总集篇&特番节目先行播出!《SAO Alicization War of Underworld》第2季度TV动画4月25日放送!
2020/03/06 日漫
[原创]PHP中通过ADODB库实现调用Access数据库之修正版本
2006/12/31 PHP
PHP自动识别字符集并完成转码详解
2013/08/02 PHP
PHP将XML转数组过程详解
2013/11/13 PHP
php过滤html中的其他网站链接的方法(域名白名单功能)
2014/04/24 PHP
PHP callback函数使用方法和注意事项
2015/01/23 PHP
PHP7.0连接DB操作实例分析【基于mysqli】
2019/09/26 PHP
jQuery使用手册之 事件处理
2007/03/24 Javascript
用JQUERY增删元素的代码
2012/02/14 Javascript
JS中的异常处理方法分享
2013/12/22 Javascript
结合JQ1.9通过js正则判断各种浏览器版本的方法
2013/12/30 Javascript
纯JavaScript实现的兼容各浏览器的添加和移除事件封装
2015/03/28 Javascript
javascript动态添加删除tabs标签的方法
2015/07/06 Javascript
AngularJS 过滤与排序详解及实例代码
2016/09/14 Javascript
JavaScript实现的鼠标响应颜色渐变效果完整实例
2017/02/18 Javascript
jQuery插件Echarts实现的双轴图效果示例【附demo源码下载】
2017/03/04 Javascript
jquery 手势密码插件
2017/03/17 Javascript
探索webpack模块及webpack3新特性
2017/09/18 Javascript
vue中如何使用ztree
2018/02/06 Javascript
vue调试工具vue-devtools安装及使用方法
2018/11/07 Javascript
使用jquery的cookie实现登录页记住用户名和密码的方法
2019/03/13 jQuery
微信小程序的线程架构【推荐】
2019/05/14 Javascript
JavaScrip数组去重操作实例小结
2019/06/20 Javascript
Javascript实现html转pdf高清版(提高分辨率)
2020/02/19 Javascript
python查找第k小元素代码分享
2013/12/18 Python
python 为什么说eval要慎用
2019/03/26 Python
Python使用Turtle库绘制一棵西兰花
2019/11/23 Python
浅谈Python中range与Numpy中arange的比较
2020/03/11 Python
SQL中where和having的区别
2012/06/17 面试题
创业计划书六个要素
2013/12/26 职场文书
品德评语大全
2014/05/05 职场文书
对照四风自我剖析材料
2014/10/07 职场文书
弘扬焦裕禄精神践行三严三实心得体会
2014/10/13 职场文书
事业单位年度考核个人总结
2015/02/12 职场文书
教师党员个人自我评价
2015/03/04 职场文书
2015年小学体育教师工作总结
2015/10/23 职场文书