首先我使用JDare/clanckbundle我遵循这些指令从客户端调用函数https://github.com/JDare/ClankBundle/blob/master/Resources/docs/RPCSetup.md
我一直在控制台上得到这个错误"RPC错误[对象对象]未定义"
我注意到,如果我拼写错误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 GabrielNotificationsBundleRPC;
use RatchetConnectionInterface as Conn;
class PostCommentNotificationService
{
public function notifyFunc(Conn $conn,$params)
{
return array("result"=>array_sum($params));
}
}
配置
#services.yml
services:
notifications.newcommentpost:
class: GabrielNotificationsBundleRPCPostCommentNotificationService
#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.
我也遇到过类似的问题。
我发现停止并重新启动内置的websocket服务器解决了这个问题。对websocket代码所做的每一次修改都需要重新启动websocket服务器,以使这些更改发生…
php app/console clank:server