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中$_REQUEST、$_POST、$_GET的区别和联系小结
Nov 23 PHP
关于UEditor编辑器远程图片上传失败的解决办法
Aug 31 PHP
PHP中使用glob函数实现一句话删除某个目录下的所有文件
Jul 22 PHP
phpmyadmin中禁止外网使用的方法
Nov 04 PHP
浅析php工厂模式
Nov 25 PHP
php提示Warning:mysql_fetch_array() expects的解决方法
Dec 16 PHP
举例讲解PHP面对对象编程的多态
Aug 12 PHP
通过PHP自带的服务器来查看正则匹配结果的方法
Dec 24 PHP
利用PHPStorm如何开发Laravel应用详解
Aug 30 PHP
对于Laravel 5.5核心架构的深入理解
Feb 22 PHP
php curl批处理实现可控并发异步操作示例
May 09 PHP
laravel-admin 管理平台获取当前登陆用户信息的例子
Oct 08 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
从网上搜到的phpwind 0day的代码
2006/12/07 PHP
php discuz 主题表和回帖表的设计
2009/03/13 PHP
sphinx增量索引的一个问题
2011/06/14 PHP
使用git迁移Laravel项目至新开发环境的步骤详解
2020/04/06 PHP
arguments对象
2006/11/20 Javascript
用tip解决Ext列宽度不够的问题
2008/12/13 Javascript
javascript 字符 Escape,encodeURI,encodeURIComponent
2009/07/09 Javascript
jQuery 源码分析笔记(5) jQuery.support
2011/06/19 Javascript
jQuery EasyUI API 中文文档 - ComboBox组合框
2011/10/07 Javascript
分享一个精简的vue.js 图片lazyload插件实例
2017/03/13 Javascript
12个非常有用的JavaScript技巧
2017/05/17 Javascript
create-react-app构建项目慢的解决方法
2018/03/14 Javascript
微信小程序收藏功能的实现代码
2018/06/12 Javascript
详解vue axios二次封装
2018/07/22 Javascript
Vue动态组件与异步组件实例详解
2019/02/23 Javascript
JavaScript语句错误throw、try及catch实例解析
2020/08/18 Javascript
[33:42]LGD vs OG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python 执行字符串表达式函数(eval exec execfile)
2014/08/11 Python
详尽讲述用Python的Django框架测试驱动开发的教程
2015/04/22 Python
利用python实现xml与数据库读取转换的方法
2017/06/17 Python
关于django 数据库迁移(migrate)应该知道的一些事
2018/05/27 Python
Python拼接微信好友头像大图的实现方法
2018/08/01 Python
python 利用for循环 保存多个图像或者文件的实例
2018/11/09 Python
对python中if语句的真假判断实例详解
2019/02/18 Python
python高斯分布概率密度函数的使用详解
2019/07/10 Python
Django model 中设置联合约束和联合索引的方法
2019/08/06 Python
Python简易计算器制作方法代码详解
2019/10/31 Python
Python实现我的世界小游戏源代码
2021/03/02 Python
自荐信格式范文
2013/10/07 职场文书
公司综合部的成员自我评价分享
2013/11/05 职场文书
cf收人广告词大全
2014/03/14 职场文书
2014年审计人员工作总结
2014/12/19 职场文书
原告代理词范文
2015/05/25 职场文书
2016年小学“公民道德宣传日”活动总结
2016/04/01 职场文书
2016年“9.22”世界无车日活动小结
2016/04/05 职场文书
向Spring IOC 容器动态注册bean实现方式
2022/07/15 Java/Android