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编程最快明白》第五讲:php目录、文件操作
Nov 01 PHP
IP攻击升级,程序改进以对付新的攻击
Nov 23 PHP
解析在zend Farmework下如何创立一个FORM表单
Jun 28 PHP
PHP输出当前进程所有变量/常量/模块/函数/类的示例
Nov 07 PHP
Thinkphp调用Image类生成缩略图的方法
Mar 07 PHP
Joomla开启SEF的方法
May 04 PHP
golang与php实现计算两个经纬度之间距离的方法
Jul 22 PHP
yii的入口文件index.php中为什么会有这两句
Aug 04 PHP
php 浮点数比较方法详解
May 05 PHP
laravel手动创建数组分页的实现代码
Jun 07 PHP
基于laravel where的高级使用方法
Oct 10 PHP
解决Laravel自定义类引入和命名空间的问题
Oct 15 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分页函数
2006/10/09 PHP
PHP内核探索:变量概述
2014/01/30 PHP
显示youtube视频缩略图和Vimeo视频缩略图代码分享
2014/02/13 PHP
php设计模式之命令模式使用示例
2014/03/02 PHP
PHP CodeIgniter框架的工作原理研究
2015/03/30 PHP
PHP实现bitmap位图排序与求交集的方法
2016/07/28 PHP
基于php(Thinkphp)+jquery 实现ajax多选反选不选删除数据功能
2017/02/24 PHP
Laravel使用原生sql语句并调用的方法
2019/10/09 PHP
js类后台管理菜单类-MenuSwitch
2007/09/12 Javascript
jquery 回车事件实现代码
2011/08/23 Javascript
JavaScript1.6数组新特性介绍以及JQuery的几个工具方法
2013/12/06 Javascript
javascript中全局对象的isNaN()方法使用介绍
2013/12/19 Javascript
简单实现限制uploadify上传个数
2015/11/16 Javascript
jquery获取select选中值的方法分析
2015/12/22 Javascript
Angularjs使用过滤器完成排序功能
2017/09/20 Javascript
React Native中TabBarIOS的简单使用方法示例
2017/10/13 Javascript
利用Angular2 + Ionic3开发IOS应用实例教程
2018/01/15 Javascript
node.js基于dgram数据报模块创建UDP服务器和客户端操作示例
2020/02/12 Javascript
JavaScript仿京东秒杀倒计时
2020/03/17 Javascript
原生JS实现天气预报
2020/06/16 Javascript
[42:04]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#3Secret VS OG第一局
2016/03/03 DOTA
python解析xml模块封装代码
2014/02/07 Python
Python中装饰器高级用法详解
2017/12/25 Python
CSS3弹性盒模型flex box快速入门心得(必看篇)
2016/05/24 HTML / CSS
HTML5 video循环播放多个视频的方法步骤
2020/08/06 HTML / CSS
娇韵诗法国官网:Clarins法国
2019/01/29 全球购物
Notino芬兰:购买香水和化妆品
2019/04/15 全球购物
大专会计自我鉴定
2014/02/06 职场文书
期末评语大全
2014/05/04 职场文书
党员年终个人总结
2015/02/14 职场文书
毕业论文致谢部分怎么写
2015/05/14 职场文书
婚庆开业庆典主持词
2015/06/30 职场文书
2016年公司新年寄语
2015/08/17 职场文书
人生哲理妙语30条:淡写流年,笑过人生
2019/09/04 职场文书
python 使用Tensorflow训练BP神经网络实现鸢尾花分类
2021/05/12 Python
详解Flutter自定义应用程序内键盘的实现方法
2022/06/14 Java/Android