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 session应用实例 登录验证
Mar 16 PHP
PHP session有效期问题
Apr 26 PHP
php函数array_merge用法一例(合并同类数组)
Feb 03 PHP
php json_encode值中大括号与花括号区别
Sep 30 PHP
php ImageMagick windows下安装教程
Jan 26 PHP
php字符串分割函数用法实例
Mar 17 PHP
PHP 前加at符合@的作用解析
Jul 31 PHP
php页面,mysql数据库转utf-8乱码,utf-8编码问题总结
Aug 27 PHP
非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程
Apr 12 PHP
PHP的时间戳与具体时间转化的简单实现
Jun 13 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
Mar 30 PHP
win10下 php安装seaslog扩展的详细步骤
Dec 04 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垃圾回收机制引用计数器概念分析
2013/06/24 PHP
php获取发送给用户的header信息的方法
2015/03/16 PHP
Thinkphp关闭缓存的方法
2015/06/26 PHP
PHP在线书签系统分享
2016/01/04 PHP
PHP处理数组和XML之间的互相转换
2016/06/02 PHP
IE bug table元素的innerHTML
2010/01/11 Javascript
自己动手制作jquery插件之自动添加删除行的实现
2011/10/13 Javascript
JS打印gridview实现原理及代码
2013/02/05 Javascript
js中parseInt函数浅谈
2013/07/31 Javascript
JAVASCRIPT函数作用域和提前声明 分享
2013/08/22 Javascript
javascript判断chrome浏览器的方法
2014/03/26 Javascript
JavaScript使用setTimeout实现延迟弹出警告框的方法
2015/04/07 Javascript
JavaScript中字面量与函数的基本使用知识
2015/10/20 Javascript
Nodejs中 npm常用命令详解
2016/07/04 NodeJs
详解vue+vuex+koa2开发环境搭建及示例开发
2018/01/22 Javascript
使用vue-router与v-if实现tab切换遇到的问题及解决方法
2018/09/07 Javascript
vue中的计算属性实例详解
2018/09/19 Javascript
vue实现弹框遮罩点击其他区域弹框关闭及v-if与v-show的区别介绍
2018/09/29 Javascript
js校验开始时间和结束时间
2020/05/26 Javascript
详解在Python程序中解析并修改XML内容的方法
2015/11/16 Python
python开发简易版在线音乐播放器
2017/03/03 Python
Python连接SQLServer2000的方法详解
2017/04/19 Python
python设置值及NaN值处理方法
2018/07/03 Python
Python模拟自动存取款机的查询、存取款、修改密码等操作
2018/09/02 Python
在Pycharm terminal中字体大小设置的方法
2019/01/16 Python
python实现随机加减法生成器
2020/02/24 Python
详解HTML5布局和HTML5标签
2020/10/26 HTML / CSS
美国的Eastbay旗下的运动款子品牌:Final-Score
2018/01/01 全球购物
研究生自荐信
2013/10/09 职场文书
财务管理专业求职信
2014/06/11 职场文书
毕业生找工作自荐书
2014/06/30 职场文书
销售团队获奖感言
2014/08/14 职场文书
企业法人代表授权委托书
2014/10/02 职场文书
2015年教学工作总结
2015/04/02 职场文书
市场部岗位职责范本
2015/04/15 职场文书
pandas中pd.groupby()的用法详解
2022/06/16 Python