ClankBundle - Symfony中的WebSocket

时间:2015-03-12 15:45:37

标签: php symfony

我尝试在我的symfony项目中使用webscoket。我找到了这个捆绑包,但我无法设置它。

https://github.com/JDare/ClankBundle

我的ChatTopic.php

<?php

namespace My\ChatBundle\Topic;

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

class ChatTopic 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)
    {
        //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)
    {
        //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)
    {
        /*
        $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
        ));
    }

}

现在我的服务

my_chat.chat_topic_handle:
        class: My\ChatBundle\Topic\ChatTopic

配置

# Clank Configuration
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
    topic:
        -
            name: "chat"
            service: "my_chat.chat_topic_handle"

这是我的js代码:

var myClank = Clank.connect("ws://localhost:8080");

myClank.on("socket/connect", function(session){

    session.publish("chat/channel", {msg: "This is a message!"});

    //the callback function in "subscribe" is called everytime an event is published in that channel.
    session.subscribe("chat/channel", function(uri, payload){
        console.log("Received message", payload.msg);
    });

    session.unsubscribe("chat/channel");

    session.publish("chat/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);
})

刷新页面后,我的控制台中的websocket没有任何内容。 Webscoket与服务器连接,但我认为我的ChatTopic.php不起作用,我不知道为什么。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我认为问题出在您的js代码中。

您连接到套接字,订阅&#34;聊天/频道&#34;并立即取消订阅。这可以防止您收到任何消息。

你应该按顺序:

  1. 订阅
  2. 公布
  3. 永远不会取消订阅(至少,我仍然没有找到理由这样做)
  4. 顺便说一句,您不必刷新页面&#34;,您应该在同一个网址上打开两个浏览器页面:当第二个加载时,您应该在另一个网页上看到一条消息。