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变量作用域以及地址引用问题
Dec 27 PHP
ThinkPHP文件缓存类代码分享
Apr 22 PHP
php获取指定范围内最接近数的方法
Jun 02 PHP
Zend Framework动作助手Url用法详解
Mar 05 PHP
Yii2实现log输出到file及database的方法
Nov 12 PHP
php、mysql查询当天,查询本周,查询本月的数据实例(字段是时间戳)
Feb 04 PHP
浅谈PHP的反射API
Feb 26 PHP
PHP数组去重的更快实现方式分析
May 09 PHP
PHP面向对象类型约束用法分析
Jun 12 PHP
自定义Laravel (monolog)日志位置,并增加请求ID的实现
Oct 17 PHP
laravel框架中表单请求类型和CSRF防护实例分析
Nov 23 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小偷程序)
2014/08/23 PHP
PHP进行批量任务处理不超时的解决方法
2016/07/11 PHP
Yii2.0多文件上传实例说明
2017/07/24 PHP
LNMP部署laravel以及xhprof安装使用教程
2017/09/14 PHP
laravel框架select2多选插件初始化默认选中项操作示例
2020/02/18 PHP
改写一个简单的菜单 弹性大小
2010/12/02 Javascript
iframe调用父页面函数示例详解
2014/07/17 Javascript
使用jQuery管理选择结果
2015/01/20 Javascript
jQuery关键词说明插件cluetip使用指南
2015/04/21 Javascript
javascript实现动态表头及表列的展现方法
2015/07/14 Javascript
简单介绍jsonp 使用小结
2016/01/27 Javascript
jQuery时间验证和转换为标准格式的时间格式
2017/03/06 Javascript
Javascript 详解封装from表单数据为json串进行ajax提交
2017/03/29 Javascript
基于JavaScript实现无限加载瀑布流
2017/07/21 Javascript
详解Webpack + ES6 最新环境搭建与配置
2018/06/04 Javascript
Vue实现点击箭头上下移动效果
2020/06/11 Javascript
[01:00:26]Ti4主赛事胜者组第一天 EG vs NEWBEE 1
2014/07/19 DOTA
[01:21]DOTA2 新英雄 森海飞霞
2020/12/18 DOTA
Python自定义scrapy中间模块避免重复采集的方法
2015/04/07 Python
使用Python获取并处理IP的类型及格式方法
2018/11/01 Python
windows系统中Python多版本与jupyter notebook使用虚拟环境的过程
2019/05/15 Python
Python实现时间序列可视化的方法
2019/08/06 Python
给你一面国旗 教你用python画中国国旗
2019/09/24 Python
opencv resize图片为正方形尺寸的实现方法
2019/12/26 Python
基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)
2020/04/29 Python
Python3中小括号()、中括号[]、花括号{}的区别详解
2020/11/15 Python
意大利制造的西装、衬衫和针对男士量身定制的服装:Lanieri
2018/04/08 全球购物
阿迪达斯希腊官方网上商店:adidas希腊
2019/04/06 全球购物
FC-Moto美国:欧洲最大的摩托车服装和头盔商店之一
2019/08/24 全球购物
银行自荐信范文
2013/10/07 职场文书
经验交流材料格式
2014/12/30 职场文书
开业庆典嘉宾致辞
2015/08/01 职场文书
信息技术国培研修日志
2015/11/13 职场文书
教师培训学习心得体会
2016/01/21 职场文书
创业计划书之青年旅馆
2019/09/23 职场文书
详解Spring Security中的HttpBasic登录验证模式
2022/03/17 Java/Android