我正在构建一个应用程序,我可以在其中看到日志中的实时更改此应用程序是使用Symfony v4.1构建的。有this捆绑包有一个基于Ratchet和Autobahn.js的Web Socket服务器和客户端
我根据文档设置了所有要求以使其工作。
连接脚本工作正常,直到我订阅频道/主题。连接立即在客户端关闭,服务器无法检测到它。有谁知道如何解决这个问题?此外,我很好奇这个响应代码WS-1007的含义。
使用Javascript:
var ws = WS.connect("ws://" + $websocket_host + ":" + $websocket_port);
ws.on("socket/connect", function(session) {
if (window.$debug) {
console.log("websocket connected");
}
console.log(session);
session.subscribe("log/channel", function(uri, payload) {
console.log(payload);
});
});
ws.on("socket/disconnect", function(e) {
if (window.$debug) {
console.log("websocket disconnected [reason:" + e.reason + " code:" + e.code + "]");
}
});
Javascript日志:
~ websocket connected
~ websocket disconnected [reason:Connection was closed properly [WS-1007: ] code:0]
服务器日志:
14:15:39 DEBUG [websocket] INSERT CLIENT 2926 ["user" => "s:37:"anon-19491835335b991f8bde43b229754494";"] []
14:15:39 INFO [websocket] anon-19491835335b991f8bde43b229754494 connected ["connection_id" => 2926,"session_id" => "19491835335b991f8bde43b229754494","storage_id" => 2926] []
14:15:39 DEBUG [websocket] GET CLIENT 2926 [] []
14:15:39 INFO [websocket] anon-19491835335b991f8bde43b229754494 subscribe to log/channel [] []
14:15:39 DEBUG [websocket] Matched route "shop4_log" [] []
14:15:39 DEBUG [websocket] Matched route "shop4_log" [] []
主题类:
namespace App\Service\WebSocket\Topic;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
class LogTopic implements TopicInterface
{
public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
{
$topic->broadcast(['msg' => $event]);
}
public function getName()
{
return "log_topic";
}
....
}
pubsub.yaml
shop4_log:
channel: log/channel
handler:
callback: "log_topic"
所以,我最终找到了解决方案,需要在服务配置中标记该主题
services.yaml:
App\Service\Websocket\Topic\LogTopic:
tags:
- { name: gos_web_socket.topic }