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+MYSQL的文章管理系统(一)
Oct 09 PHP
MySQL修改密码方法总结
Mar 25 PHP
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
Aug 12 PHP
获取URL文件名后缀
Oct 24 PHP
PHP @ at 记号的作用示例介绍
Oct 10 PHP
10条php编程小技巧
Jul 07 PHP
php的api数据接口书写实例(推荐)
Sep 22 PHP
PHP+jquery+CSS制作头像登录窗(仿QQ登陆)
Oct 20 PHP
PHPTree――php快速生成无限级分类
Mar 30 PHP
YII分模块加载路由的实现方法
Oct 01 PHP
PHP的PDO预定义常量讲解
Jan 24 PHP
PHP实现的策略模式示例
Mar 20 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下实现伪 url 的超简单方法[转]
2007/09/24 PHP
第五章 php数组操作
2011/12/30 PHP
PHP实现递归的三种方法
2020/07/04 PHP
刷新时清空文本框内容的js代码
2007/04/23 Javascript
JQuery autocomplete 使用手册
2010/04/01 Javascript
转义字符(\)对JavaScript中JSON.parse的影响概述
2013/07/17 Javascript
关于jquery的多个选择器的使用示例
2013/10/18 Javascript
js插件设置innerHTML时在IE8下提示“未知运行时错误”解决方法
2015/04/25 Javascript
jQuery插件Zclip实现完美兼容个浏览器点击复制内容到剪贴板
2015/04/30 Javascript
基于javascript实现九九乘法表
2016/03/27 Javascript
关于Vue.js一些问题和思考学习笔记(2)
2016/12/02 Javascript
js 博客内容进度插件详解
2017/02/19 Javascript
bootstrap时间控件daterangepicker使用方法及各种小bug修复
2017/10/25 Javascript
VUE2.0+ElementUI2.0表格el-table循环动态列渲染的写法详解
2018/11/30 Javascript
JS使用setInterval计时器实现挑战10秒
2020/11/08 Javascript
[52:10]LGD vs Optic Supermajor小组赛D组胜者组决赛 BO3 第二场 6.3
2018/06/04 DOTA
简单介绍Python中的try和finally和with方法
2015/05/05 Python
基于Python Shell获取hostname和fqdn释疑
2016/01/25 Python
Python字典及字典基本操作方法详解
2018/01/30 Python
Win7 64位下python3.6.5安装配置图文教程
2020/10/27 Python
使用matlab 判断两个矩阵是否相等的实例
2020/05/11 Python
Python urllib2运行过程原理解析
2020/06/04 Python
Python如何设置指定窗口为前台活动窗口
2020/08/12 Python
python 如何利用argparse解析命令行参数
2020/09/11 Python
如何使用python写截屏小工具
2020/09/29 Python
Python xmltodict模块安装及代码实例
2020/10/05 Python
input file上传文件样式支持html5的浏览器解决方案
2012/11/14 HTML / CSS
eBay澳大利亚站:eBay.com.au
2018/02/02 全球购物
大学毕业感言
2014/01/10 职场文书
上课玩手机检讨书
2014/02/08 职场文书
一帮一活动总结
2014/05/08 职场文书
财务会计实训报告
2014/11/05 职场文书
高校教师个人总结
2015/02/10 职场文书
用人单位聘用意向书
2015/05/11 职场文书
Golang二维切片初始化的实现
2021/04/08 Golang
能让Python提速超40倍的神器Cython详解
2021/06/24 Python