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中函数rand和mt_rand的区别比较
Dec 26 PHP
php学习笔记之面向对象编程
Dec 29 PHP
php计算年龄精准到年月日
Nov 17 PHP
PHP数组游标实现对数组的各种操作详解
Jan 26 PHP
jquery不支持toggle()高(新)版本的问题解决
Sep 24 PHP
PHP实现接收二进制流转换成图片的方法
Jan 10 PHP
php制作基于xml的RSS订阅源功能示例
Feb 08 PHP
php使用str_shuffle()函数生成随机字符串的方法分析
Feb 17 PHP
php实现的错误处理封装类实例
Jun 20 PHP
深入理解PHP中mt_rand()随机数的安全
Oct 12 PHP
thinkphp5.0自定义验证规则使用方法
Nov 16 PHP
Laravel5.7框架安装与使用学习笔记图文详解
Apr 02 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
dedecms模版制作使用方法
2007/04/03 PHP
php 动态多文件上传
2009/01/18 PHP
php 静态化实现代码
2009/03/20 PHP
PHP取整数函数常用的四种方法小结
2012/07/05 PHP
PHP实现简单计算器小程序
2020/08/28 PHP
Laravel自定义 封装便捷返回Json数据格式的引用方法
2019/09/29 PHP
js每次Title显示不同的名言
2008/09/25 Javascript
Jquery中增加参数与Json转换代码
2009/11/20 Javascript
javascript string字符串优化问题
2011/07/31 Javascript
Javascript访问器属性实例分析
2014/12/30 Javascript
Bootstrap每天必学之缩略图与警示窗
2015/11/29 Javascript
Ext JS动态加载JavaScript创建窗体的方法
2016/06/23 Javascript
原生js仿jquery一些常用方法(必看篇)
2016/09/20 Javascript
bootstrapfileinput实现文件自动上传
2016/11/08 Javascript
jquery点击展示与隐藏更多内容
2016/12/03 Javascript
JS作用域深度解析
2016/12/29 Javascript
Angular2库初探
2017/03/01 Javascript
基于JavaScript实现瀑布流效果
2017/03/29 Javascript
浅析Javascript中双等号(==)隐性转换机制
2017/10/27 Javascript
详解mpvue小程序中怎么引入iconfont字体图标
2018/10/01 Javascript
JS无限级导航菜单实现方法
2019/01/05 Javascript
监控Nodejs的性能实例代码
2019/07/02 NodeJs
vue如何使用外部特殊字体的操作
2020/07/30 Javascript
[57:38]2018DOTA2亚洲邀请赛3月30日 小组赛A组 OpTic VS OG
2018/03/31 DOTA
python实现监控windows服务并自动启动服务示例
2014/04/17 Python
Flask框架的学习指南之用户登录管理
2016/11/20 Python
Django在win10下的安装并创建工程
2017/11/20 Python
使用selenium模拟登录解决滑块验证问题的实现
2019/05/10 Python
Python计算一个点到所有点的欧式距离实现方法
2019/07/04 Python
意大利巧克力店:Chocolate Shop
2019/07/24 全球购物
动物科学专业毕业生的自我评价
2013/11/29 职场文书
学生实习介绍信
2014/01/15 职场文书
十八大报告观后感
2014/01/28 职场文书
幼儿园教师工作感言
2014/02/15 职场文书
人事经理岗位职责
2014/04/28 职场文书
统计专业自荐书
2014/07/06 职场文书