socket 消息队列服务器与php客户端通信 30
socket消息队列服务器与php客户端通信classSocket{//消息头private$head=array();//消息体private$body=array()...
socket 消息队列服务器与php客户端通信
class Socket {
//消息头
private $head = array();
//消息体
private $body = array();
private static $obj = null;
public function __construct() {
}
/**
* 单例入口
* @return Socket
*/
public static function getInstance(){
if(is_null(self::$obj)){
self::$obj = new Socket();
}
return self::$obj;
}
/**
* 设置消息头信息
* @param unknown_type $msgid
* @param unknown_type $uid
* @param unknown_type $rid
* @param unknown_type $type
*/
public function setHead($msgid, $uid, $rid, $transmode) {
$head = array(
"msgid"=>$msgid,
"uid"=>$uid,
"rid"=>$rid,
"transmode"=>$transmode
);
$this->head[] = $head;
}
/**
* 设置消息包体
* @param unknown_type $body
*/
public function setBody($body) {
$this->body[] = $body;
}
/**
* 构造消息
*/
public function getMsg() {
$msg = "";
if (!empty($this->head)) {
foreach ($this->head as $key => $val) {
if ($this->body[$key] != "") {
$arr = array(
"msghead" => $val,
"msgbody" => $this->body[$key]
);
$msg .= json_encode($arr). "\r\n";
}
}
}
return $msg;
}
/**
* 发送消息
* @param unknown_type $ip
* @param unknown_type $port
*/
public function send(){
$gateway = ConfigHelper::getInstance()->setConfig("config")->getItem("gateway");
$ip = $gateway[0]['ip'];
$port = $gateway[0]['port'];
$msg = $this->getMsg();
log($msg, "socket");
if (($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
log("socket create error");
exit();
}
if (socket_connect($socket, $ip, $port) === false) {
log("socket connect error.");
exit();
}
if (socket_write($socket,$msg, strlen($msg)) == false) {
log("socket write error.");
exit();
}
socket_close($socket);
}
} 展开
class Socket {
//消息头
private $head = array();
//消息体
private $body = array();
private static $obj = null;
public function __construct() {
}
/**
* 单例入口
* @return Socket
*/
public static function getInstance(){
if(is_null(self::$obj)){
self::$obj = new Socket();
}
return self::$obj;
}
/**
* 设置消息头信息
* @param unknown_type $msgid
* @param unknown_type $uid
* @param unknown_type $rid
* @param unknown_type $type
*/
public function setHead($msgid, $uid, $rid, $transmode) {
$head = array(
"msgid"=>$msgid,
"uid"=>$uid,
"rid"=>$rid,
"transmode"=>$transmode
);
$this->head[] = $head;
}
/**
* 设置消息包体
* @param unknown_type $body
*/
public function setBody($body) {
$this->body[] = $body;
}
/**
* 构造消息
*/
public function getMsg() {
$msg = "";
if (!empty($this->head)) {
foreach ($this->head as $key => $val) {
if ($this->body[$key] != "") {
$arr = array(
"msghead" => $val,
"msgbody" => $this->body[$key]
);
$msg .= json_encode($arr). "\r\n";
}
}
}
return $msg;
}
/**
* 发送消息
* @param unknown_type $ip
* @param unknown_type $port
*/
public function send(){
$gateway = ConfigHelper::getInstance()->setConfig("config")->getItem("gateway");
$ip = $gateway[0]['ip'];
$port = $gateway[0]['port'];
$msg = $this->getMsg();
log($msg, "socket");
if (($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
log("socket create error");
exit();
}
if (socket_connect($socket, $ip, $port) === false) {
log("socket connect error.");
exit();
}
if (socket_write($socket,$msg, strlen($msg)) == false) {
log("socket write error.");
exit();
}
socket_close($socket);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询