【问题标题】:Cannot call function on Symfony2 websocket connection无法在 Symfony2 websocket 连接上调用函数
【发布时间】:2014-06-10 10:42:56
【问题描述】:

首先我使用 JDare/ClankBundle 我正在按照这些说明从客户端调用函数 https://github.com/JDare/ClankBundle/blob/master/Resources/docs/RPCSetup.md

我一直在控制台“RPC 错误 [object Object] undefined”上收到此错误

我注意到如果我拼错了 notify_func 函数也没关系,它仍然会抛出相同的错误。 我不知道我做错了什么,但我假设没有找到该函数的开头?

这是客户端(Twig)

websocket连接成功

  <script type="text/javascript">
        var myClank = Clank.connect('ws://localhost:9999');

        myClank.on("socket/connect", function(session){
            alertify.success("Connected to websocket server");

            //this will call the server side function "Sample::addFunc"
            session.call("notify/notify_func",[2,5])
                .then(  //using "then" promises.

                    function(result) //the function for a valid result
                    {
                        alertify.log("RPC Valid! "+result);
                    },
                    function(error, desc) // the function to handle an error
                    {
                        alertify.log("RPC Error"+" "+error+" "+desc);
                    }

                );
        });

        myClank.on("socket/disconnect", function(error){
            alertify.error("Disconnect for "+error.reason+" with code "+error.code);
        });
    </script>

这是服务器端

    <?php
namespace Gabriel\NotificationsBundle\RPC;
use Ratchet\ConnectionInterface as Conn;

class PostCommentNotificationService
{
    public function notifyFunc(Conn $conn,$params)
    {
        return array("result"=>array_sum($params));
    }
}

这是配置

#services.yml
services:
     notifications.newcommentpost:
       class: Gabriel\NotificationsBundle\RPC\PostCommentNotificationService
#config.yml
     # Clank Configuration
clank:
    web_socket_server:
        port: 9999       #The port the socket server will listen on
        host: localhost   #(optional) The host ip to bind to
    rpc:
       -
        name: "notify" #Important! this is the network namespace used to match calls to this service!
        service: "notifications.newcommentpost" #The service id.

【问题讨论】:

  • 问题解决了吗?您在哪个控制台上遇到错误?在浏览器控制台上还是在您运行服务器的地方?我已经在我的系统中测试了你的代码,它对我来说工作正常。我没有使用 alertify js,我使用过 console.log。
  • 这是一个非常古老的问题,我最终卸载了 clank 包并使用本机 php websockets 实现了我自己的逻辑

标签: symfony websocket phpwebsocket ratchet


【解决方案1】:

我有一个非常相似的问题。

我发现停止并重新启动内置 websocket 服务器解决了这个问题。对 websocket 代码所做的每一次修改都需要重新启动 websocket 服务器才能使这些更改发生...

php app/console clank:server

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 2017-09-10
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2019-10-14
    相关资源
    最近更新 更多