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 相关文章推荐
sourcesafe管理phpproj文件的补充说明(downmoon)
Apr 11 PHP
php高级编程-函数-郑阿奇
Jul 04 PHP
PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT
Mar 06 PHP
php网站判断用户是否是手机访问的方法
Nov 01 PHP
PHP防范SQL注入的具体方法详解(测试通过)
May 09 PHP
WIFI万能钥匙密码查询接口实例
Sep 28 PHP
WordPress用户登录框密码的隐藏与部分显示技巧
Dec 31 PHP
php中namespace use用法实例分析
Jan 22 PHP
利用php做服务器和web前端的界面进行交互
Oct 31 PHP
ThinkPHP实现分页功能
Apr 28 PHP
Yii2.0多文件上传实例说明
Jul 24 PHP
Laravel 在views中加载公共页面的实现代码
Oct 22 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
Zerg建筑一览
2020/03/14 星际争霸
php在项目中寻找代码的坏味道(综艺命名)
2012/07/19 PHP
WordPress主题中添加文章列表页页码导航的PHP代码实例
2015/12/22 PHP
超级退弹代码
2008/07/07 Javascript
Jquery实现图片放大镜效果的思路及代码(自写)
2013/10/18 Javascript
json属性名为什么要双引号(个人猜测)
2014/07/31 Javascript
javascript实现别踩白块儿小游戏程序
2015/11/22 Javascript
jQuery使用$获取对象后检查该对象是否存在的实现方法
2016/09/04 Javascript
新手学习前端之js模仿淘宝主页网站
2016/10/31 Javascript
微信小程序之仿微信漂流瓶实例
2016/12/09 Javascript
jQuery返回定位插件详解
2017/05/15 jQuery
bootstrap警告框示例代码分享
2017/05/17 Javascript
谈谈对vue响应式数据更新的误解
2017/08/01 Javascript
Canvas实现微信红包照片效果
2018/08/21 Javascript
基于JavaScript实现留言板功能
2020/03/16 Javascript
Vue computed 计算属性代码实例
2020/04/22 Javascript
Nuxt配置Element-UI按需引入的操作方法
2020/07/06 Javascript
vue中jsonp插件的使用方法示例
2020/09/10 Javascript
[02:05:03]完美世界DOTA2联赛循环赛 LBZS VS Matador BO2 10.28
2020/10/28 DOTA
实例解析Python的Twisted框架中Deferred对象的用法
2016/05/25 Python
python常用函数详解
2016/09/13 Python
Python 类的继承实例详解
2017/03/25 Python
Python PIL图片添加字体的例子
2019/08/22 Python
python使用gdal对shp读取,新建和更新的实例
2020/03/10 Python
解决启动django,浏览器显示“服务器拒绝访问”的问题
2020/05/13 Python
Python函数递归调用实现原理实例解析
2020/08/11 Python
求职简历中个人的自我评价
2013/12/01 职场文书
正规的求职信范文分享
2013/12/11 职场文书
安卓程序员求职信
2014/02/28 职场文书
责任书格式范文
2014/07/28 职场文书
股指期货心得体会
2014/09/13 职场文书
城管执法人员个人对照检查材料思想汇报
2014/09/29 职场文书
志愿者服务活动总结报告
2015/05/06 职场文书
导游词幽默开场白
2019/06/26 职场文书
导游词之江南园林狮子林
2019/09/16 职场文书
Android基础入门之dataBinding的简单使用教程
2022/06/21 Java/Android