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 21 PHP
PHP 判断变量类型实现代码
Oct 23 PHP
php中存储用户ID和密码到mysql数据库的方法
Feb 06 PHP
基于PHP+Ajax实现表单验证的详解
Jun 25 PHP
如何使用php判断服务器是否是HTTPS连接
Jul 05 PHP
PHP5.2中PDO的简单使用方法
Mar 25 PHP
PHP strcmp()和strcasecmp()的区别实例
Nov 05 PHP
yii2中dropDownList实现二级和三级联动写法
Apr 26 PHP
PHP从数组中删除元素的四种方法实例
May 12 PHP
windows下的WAMP环境搭建图文教程(推荐)
Jul 27 PHP
PHP中的self关键字详解
Jun 23 PHP
宝塔面板在NGINX环境中TP5.1如何运行?
Mar 09 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 win下Socket方式发邮件类
2009/08/21 PHP
php把数据表导出为Excel表的最简单、最快的方法(不用插件)
2014/05/10 PHP
麻雀虽小五脏俱全 Dojo自定义控件应用
2010/09/04 Javascript
iframe的onload在Chrome/Opera中执行两次Bug的解决方法
2011/03/17 Javascript
jQuery制作的别致导航有阴影背景高亮模式窗口
2014/04/15 Javascript
AspNet中使用JQuery boxy插件的确认框
2015/05/20 Javascript
jQuery中 delegate使用的问题
2015/07/03 Javascript
javascript解决IE6下hover问题的方法
2015/07/28 Javascript
如何用angularjs制作一个完整的表格
2016/01/21 Javascript
轻松掌握JavaScript中的Math object数学对象
2016/05/26 Javascript
Javascript中的数组常用方法解析
2016/06/17 Javascript
JavaScript中关键字 in 的使用方法详解
2016/10/17 Javascript
实现点击下箭头变上箭头来回切换的两种方法【推荐】
2016/12/14 Javascript
JavaScript 巧学巧用
2017/05/23 Javascript
详解Node项目部署到云服务器上
2017/07/12 Javascript
AngularJs ng-change事件/指令的用法小结
2017/11/01 Javascript
为什么使用koa2搭建微信第三方公众平台的原因
2018/05/16 Javascript
vue自定义移动端touch事件之点击、滑动、长按事件
2018/07/10 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
2018/11/28 Javascript
微信小程序mpvue点击按钮获取button值的方法
2019/05/29 Javascript
pm2启动ssr失败的解决方法
2019/06/29 Javascript
微信小程序使用npm包的方法步骤
2019/08/13 Javascript
基于原生js实现判断元素是否有指定class名
2020/07/11 Javascript
python实现爬虫统计学校BBS男女比例(一)
2015/12/31 Python
Python操作SQLite数据库的方法详解【导入,创建,游标,增删改查等】
2017/07/11 Python
tensorflow学习笔记之简单的神经网络训练和测试
2018/04/15 Python
python初步实现word2vec操作
2020/06/09 Python
python 常见的反爬虫策略
2020/09/27 Python
Django windows使用Apache实现部署流程解析
2020/10/12 Python
HTML5的结构和语义(1):前言
2008/10/17 HTML / CSS
挪威手表购物网站:Klokker
2016/09/19 全球购物
琳达·法罗眼镜英国官网:Linda Farrow英国
2021/01/19 全球购物
2014年医院党建工作总结
2014/12/20 职场文书
房产销售员2015年终工作总结
2015/10/22 职场文书
Mysql 性能监控及调优
2021/04/06 MySQL
Windows 11上手初体验:任务栏和开始菜单等迎来大改
2021/11/21 数码科技