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 相关文章推荐
模仿OSO的论坛(四)
Oct 09 PHP
php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释
Mar 23 PHP
php HtmlReplace输入过滤安全函数
Jul 03 PHP
PHPUnit PHP测试框架安装方法
Mar 23 PHP
php判断变量类型常用方法
Apr 24 PHP
解析smarty 截取字符串函数 truncate的用法介绍
Jun 20 PHP
PHP语法自动检查的Vim插件
Aug 11 PHP
PHP实现文件上传和多文件上传
Dec 24 PHP
PHP中addslashes()和stripslashes()实现字符串转义和还原用法实例
Jan 07 PHP
PHP解压tar.gz格式文件的方法
Feb 14 PHP
php设计模式之观察者模式定义与用法经典示例
Sep 19 PHP
php7 list()、session及其他模块的修改实例分析
May 25 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
建立动态的WML站点(一)
2006/10/09 PHP
我用php+mysql写的留言本
2006/10/09 PHP
PHP实现多图片上传类实例
2014/07/26 PHP
PHP语法自动检查的Vim插件
2014/08/11 PHP
标准PHP的AES加密算法类
2015/03/12 PHP
PHP7移除的扩展和SAPI
2021/03/09 PHP
学习YUI.Ext第五日--做拖放Darg&amp;Drop
2007/03/10 Javascript
Tips 带三角可关闭的文字提示
2010/10/06 Javascript
在次封装easyui-Dialog插件实现代码
2010/11/14 Javascript
javascript中this做事件参数相关问题解答
2013/03/17 Javascript
js给onclick赋值传参数的两种方法
2013/11/25 Javascript
JavaScript中的包装对象介绍
2015/01/27 Javascript
javascript实现带下拉子菜单的导航菜单效果
2015/05/14 Javascript
ES6中数组array新增方法实例总结
2017/11/07 Javascript
分析javascript原型及原型链
2018/03/18 Javascript
vue element动态渲染、移除表单并添加验证的实现
2019/01/16 Javascript
Vue入门学习笔记【基本概念、对象、过滤器、指令等】
2019/04/13 Javascript
vue的注意规范之v-if 与 v-for 一起使用教程
2019/08/04 Javascript
对Layer弹窗使用及返回数据接收的实例详解
2019/09/26 Javascript
Javascript实现html转pdf高清版(提高分辨率)
2020/02/19 Javascript
浅析Python中元祖、列表和字典的区别
2016/08/17 Python
DataFrame 将某列数据转为数组的方法
2018/04/13 Python
python的schedule定时任务模块二次封装方法
2019/02/19 Python
Python高级特性 切片 迭代解析
2019/08/23 Python
python随机生成库faker库api实例详解
2019/11/28 Python
详解Ubuntu环境下部署Django+uwsgi+nginx总结
2020/04/02 Python
Python控制台实现交互式环境执行
2020/06/09 Python
一款纯css3实现的漂亮的404页面的实例教程
2014/11/27 HTML / CSS
波兰在线香水店:Perfumy.pl
2019/08/12 全球购物
葡萄牙语专业个人求职信
2013/12/10 职场文书
职业生涯规划书范文
2014/03/10 职场文书
秘书英文求职信
2014/04/16 职场文书
教师求职自荐书
2014/06/14 职场文书
简单的辞职信怎么写
2015/02/28 职场文书
个人收入证明格式
2015/06/24 职场文书
MySQL中优化SQL语句的方法(show status、explain分析服务器状态信息)
2022/04/09 MySQL