Я пытаюсь использовать webscoket в своем проекте symfony. Я нашел этот пакет, но я не могу его настроить.ClankBundle - WebSocket в Symfony
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 не работает, и я не знаю почему. Спасибо за помощь.
Hello @Alberto, Спасибо за указание на 3. никогда не отписываться. – herr