棘轮/ Websockets:订阅对象的客户数量是多少?

时间:2015-06-18 13:39:50

标签: php websocket ratchet phpwebsocket

我想知道有多少客户实际订阅了聊天室/会话。

更确切地说,我只想知道是否有超过1个客户端。 (聊天室实际上是两个用户之间的私密对话)。

一次只有一个聊天室/私人对话(每个用户)。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <CreateAndSendEnvelope xmlns="http://www.docusign.net/API/3.0">
      <Envelope>
        <AccountId>a6cff121-fb11-4a50-a2fb-9ce50ba70f7c</AccountId>
        <Documents>
          <Document>
            <ID>LEASE AGREEMENT</ID>
            <Name>838.doc</Name>
            <PDFBytes>BBBXXX</PDFBytes>
            <FileExtension>.doc</FileExtension>
          </Document>
        </Documents>
        <Recipients>
          <Recipient>
            <ID>1</ID>
            <UserName>MUKES OUTLOOK</UserName>
            <Email>M.AGRAWAL@OUTLOOK.COM</Email>
            <Type>Signer</Type>
            <AccessCode xsi:nil="true" />
            <PhoneAuthentication>
              <RecipMayProvideNumber>true</RecipMayProvideNumber>
              <RequireIDLookup>true</RequireIDLookup>
              <IDCheckConfigurationName>Phone Auth $</IDCheckConfigurationName>
            </PhoneAuthentication>
            <RoutingOrder>1</RoutingOrder>
            <RoleName>Lessee</RoleName>
          </Recipient>
          <Recipient>
            <ID>2</ID>
            <UserName>MUK TOPTAL</UserName>
            <Email>MUKESH@TOPTAL.COM</Email>
            <Type>Signer</Type>
            <AccessCode xsi:nil="true" />
            <AnySocialIDRecipientAuthentication>true</AnySocialIDRecipientAuthentication>
            <RoutingOrder>2</RoutingOrder>
            <RoleName>Guarantor</RoleName>
          </Recipient>
        </Recipients>
        <Tabs>
          <Tab>
            <DocumentID>LEASE AGREEMENT</DocumentID>
            <RecipientID>1</RecipientID>
            <AnchorTabItem>
              <AnchorTabString>??CS1!</AnchorTabString>
              <Unit>Cms</Unit>
              <IgnoreIfNotPresent>false</IgnoreIfNotPresent>
            </AnchorTabItem>
            <Type>SignHere</Type>
            <Name>Lessee Signature 1</Name>
          </Tab>
          <Tab>
            <DocumentID>LEASE AGREEMENT</DocumentID>
            <RecipientID>2</RecipientID>
            <AnchorTabItem>
              <AnchorTabString>??GS1!</AnchorTabString>
              <Unit>Cms</Unit>
              <IgnoreIfNotPresent>false</IgnoreIfNotPresent>
            </AnchorTabItem>
            <Type>SignHere</Type>
            <Name>Guarantor Signature 2</Name>
          </Tab>
        </Tabs>
        <Subject>Subject</Subject>
        <EmailBlurb>Body</EmailBlurb>
      </Envelope>
    </CreateAndSendEnvelope>
  </soap:Body>
</soap:Envelope>

那么在给定的代码中,如何获得$ nb_clients?

更新

我想我已经开始看到解决方案了。

这是我的第二次尝试:

class Chat implements WampServerInterface
{
    protected $conversationId;
    public function __construct(){
        $this->conversationId = null;
    }
    public function onSubscribe(ConnectionInterface $conn, $conversation_id){
        $this->conversationId = $conversation_id;
        echo "Client $conn->resourceId assigned to the conversation : $conversation_id\n";
    }
    public function onPublish(ConnectionInterface $conn, $conversation_id, $event, array $exclude, array $eligible){
        // How to get $nb_clients ?
        echo "$nb_clients User(s) in conversation";
        echo "Message sent to $conversation_id : $event";
        // ...
        $message = $event; 
        // Send data to conversation
        $this->conversationId->broadcast($message);
    }
}

任何想法,如果这将正常工作?它实际上似乎工作,但我仍然不确定它是否是最好的解决方案。实际上,我从Ratchet github获得灵感。

1 个答案:

答案 0 :(得分:5)

onPublish的第二个参数是Topic个对象(Interface WampServerInterface):

  

onPublish(Ratchet \ ConnectionInterface $ conn,   string | Ratchet \ Wamp \ Topic $ topic,string $ event,array $ exclude,array   $符合条件)

所以根据Ratchet's documentation,您可以在主题上使用count()方法来获取订阅者:

$nb_clients = $conversation_id->count();