Websocket 在订阅时断开连接
Websocket disconnects on subscribing
我正在构建一个应用程序,我可以在其中查看日志中的实时更改
此应用程序是使用 Symfony v4.1 构建的。有 this 捆绑包具有基于 Ratchet 和 Autobahn.js
的 Web Socket 服务器和客户端
我已经根据文档设置了所有要求以使其正常工作。
有一个话题class:
发布订阅路由已配置
服务器运行
客户端在 javascript 页面加载时运行
连接脚本工作正常,直到我订阅 channel/topic。连接在客户端立即关闭,服务器未检测到它。有谁知道如何解决这个问题?另外,我很好奇这个响应代码 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" [] []
主题class:
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 }
我正在构建一个应用程序,我可以在其中查看日志中的实时更改 此应用程序是使用 Symfony v4.1 构建的。有 this 捆绑包具有基于 Ratchet 和 Autobahn.js
的 Web Socket 服务器和客户端我已经根据文档设置了所有要求以使其正常工作。
有一个话题class:
发布订阅路由已配置
服务器运行
客户端在 javascript 页面加载时运行
连接脚本工作正常,直到我订阅 channel/topic。连接在客户端立即关闭,服务器未检测到它。有谁知道如何解决这个问题?另外,我很好奇这个响应代码 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" [] []
主题class:
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 }