sockets - ClankBundle 不会触发订阅

标签 sockets symfony autobahn ratchet

我正在尝试将 ClankBundle 实现到 Symfony2 项目。
当我创建一个从 js 调用的函数时,它运行良好,但当我尝试在 Symfony2 上创建 onSubscribe 函数时,它不会触发并且我没有收到错误。
顺便说一句,此检查是在 Ubuntu 上运行的本地环境中进行的
这是我的代码
config.yml -

clank:
    web_socket_server:
        port: 8080        #The port the socket server will listen on
        host: 127.0.0.1   #(optional) The host ip to bind to
    rpc:
        -
            name: "acme" #Important! this is the topic namespace used to match to this service!
            service: "acme_hello.topic_sample_service" #The service id.
        -
            name: "sample" #Important! this is the network namespace used to match calls to this service!
            service: "my_bundle.rpc_sample_service" #The service id.

服务.yml -
  my_bundle.rpc_sample_service:
      class: MyBundle\RPC\SocketService

  acme_hello.topic_sample_service:
      class: MyBundle\Topic\MyTopic

MyTopic.php -
<?php

namespace MyBundle\Topic;

use JDare\ClankBundle\Topic\TopicInterface;
use Ratchet\ConnectionInterface as Conn;

class MyTopic implements TopicInterface
{

  /**
   * This will receive any Subscription requests for this topic.
   *
   * @param \Ratchet\ConnectionInterface $conn
   * @param $topic
   * @return void
   */
  public function onSubscribe(Conn $conn, $topic)
  {
    file_put_contents('/deleteme.txt', 'onSubscribe called \n', FILE_APPEND);
    //this will broadcast the message to ALL subscribers of this topic.
    $topic->broadcast($conn->resourceId . " has joined " . $topic->getId());
  }

  /**
   * This will receive any UnSubscription requests for this topic.
   *
   * @param \Ratchet\ConnectionInterface $conn
   * @param $topic
   * @return void
   */
  public function onUnSubscribe(Conn $conn, $topic)
  {
    file_put_contents('/deleteme.txt', 'onUnSubscribe called \n', FILE_APPEND);
    //this will broadcast the message to ALL subscribers of this topic.
    $topic->broadcast($conn->resourceId . " has left " . $topic->getId());
  }


  /**
   * This will receive any Publish requests for this topic.
   *
   * @param \Ratchet\ConnectionInterface $conn
   * @param $topic
   * @param $event
   * @param array $exclude
   * @param array $eligible
   * @return mixed|void
   */
  public function onPublish(Conn $conn, $topic, $event, array $exclude, array $eligible)
  {
    file_put_contents('/deleteme.txt', 'onPublish called \n', FILE_APPEND);
    /*
    $topic->getId() will contain the FULL requested uri, so you can proceed based on that

    e.g.

    if ($topic->getId() == "acme/channel/shout")
        //shout something to all subs.
    */


    $topic->broadcast(array(
      "sender" => $conn->resourceId,
      "topic" => $topic->getId(),
      "event" => $event
    ));
  }

}

js代码——
var mysess;
    $(document).ready(function(){
        var myClank = Clank.connect("ws://localhost:8080");

        myClank.on("socket/connect", function(session){
            //session is an Autobahn JS WAMP session.
            mysess = session;
            console.log("Successfully Connected!");
            //the callback function in "subscribe" is called everytime an event is published in that channel.
            console.log('before subscribe');
            session.subscribe("acme/channel", function(uri, payload){
                console.log("Received message", payload.msg);
            });
            console.log('afte subscribe')
            session.publish('im publishing from js');
            mysess.publish("acme/channel", {msg: "This is a message!"});
//            mysess.unsubscribe("acme/channel");
//            mysess.publish("acme/channel", {msg: "I won't see this"});

        })

        myClank.on("socket/disconnect", function(error){
            //error provides us with some insight into the disconnection: error.reason and error.code
            console.log("Disconnected for " + error.reason + " with code " + error.code);
        })
    })

最佳答案

topic 和 rpc 是不同的配置条目:

 topic:
    -
        name: "acme" #Important! this is the topic namespace used to match to this service!
        service: "acme_hello.topic_sample_service" #The service id.
 rpc:
    -
        name: "sample" #Important! this is the network namespace used to match calls to this service!   
        service: "my_bundle.rpc_sample_service" #The service id.

关于sockets - ClankBundle 不会触发订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30716987/

相关文章:

c++ - 线程池实现

c - HTTP 通过 C 中的套接字 : Basic file server fails to properly return file

c++ - 通过 TCP 发送图片

java - LibGDX:无法在 Android 上打开服务器套接字

php - 交响乐 2.3 : How do you configure SwiftMailer to automatically use a custom plugin?

Symfony Easyadmin - 如何在 "btn action-new"附近添加自定义操作?

symfony - Doctrine2 - 如何按月设置查询?

apache - 如何使用安全网络套接字(wss)?

node.js - 无法在我的 node.js 项目中的 wamp 套接字中配置 ssl

python - 使用 Autobahn WebSocket 库使用 sendMessage 进行 CouchDB 更改