略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ZMQSocket::connect

2024-12-22

ZMQSocket::connect

(PECL zmq >= 0.5.0)

ZMQSocket::connectConnect the socket

说明

public ZMQSocket::connect(string $dsn, bool $force = false): ZMQSocket

Connect the socket to a remote endpoint. The endpoint is defined in format transport://address where transport is one of the following: inproc, ipc, tcp, pgm or epgm.

参数

dsn

The connect dsn, for example transport://address.

force

Tries to connect even if the socket has already been connected to given endpoint.

返回值

Returns the current object.

错误/异常

Throws ZMQSocketException on error.

范例

示例 #1 A ZMQContext() example

Construct a new context and allocate request socket from it

<?php
/* Server hostname */
$dsn "tcp://127.0.0.1:5555";

/* Create a socket */
$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ'my socket');

/* Get list of connected endpoints */
$endpoints $socket->getEndpoints();

/* Check if the socket is connected */
if (!in_array($dsn$endpoints['connect'])) {
    echo 
"<p>Connecting to $dsn</p>";
    
$socket->connect($dsn);
} else {
    echo 
"<p>Already connected to $dsn</p>";
}

/* Send and receive */
$socket->send("Hello!");
$message $socket->recv();

echo 
"<p>Server said: {$message}</p>";
?>
add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/zmqsocket.connect.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3