This commit is contained in:
afornerot 2019-07-17 17:06:05 +02:00
parent 58aa6c3d86
commit bf1c7299b1
2 changed files with 60 additions and 23 deletions

View File

@ -49,8 +49,11 @@
<div class="mychat">
{% for message in messages %}
<div class='message row'>
<div class='msgavatar'><img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/{{message.user.avatar}}' class='avatar'></div>
<div id='message-{{message.id}}' class='message row'>
<div class='msgavatar'>
<img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/{{message.user.avatar}}' class='avatar'><br>
<i class='delmessage fa fa-trash fa-fw' data-id='{{message.id}}' style='cursor: pointer;'></i>
</div>
<div class='msgdiv'>
<div class='msgtitle'>
{{message.user.lastname}} {{message.user.firstname}}<br>
@ -76,7 +79,6 @@
{% if app.user %}
var _WS_URI = "wss://{{ gos_web_socket_server_host }}:{{ gos_web_socket_server_port }}";
var _WS_URI = "wss://{{ gos_web_socket_server_host }}:5555";
console.log(_WS_URI);
var webSocket = WS.connect(_WS_URI);
@ -96,30 +98,55 @@
//the callback function in "subscribe" is called everytime an event is published in that channel.
session.subscribe("websocket/channel/{{groupid}}", function (uri, payload) {
if(payload.msg) {
html ="<div class='message row'>";
html+="<div class='msgavatar'><img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/"+payload.msg.avatar+"' class='avatar'></div>";
html ="<div id='message-"+payload.msg.id+"' class='message row'>";
html+="<div class='msgavatar'>";
html+="<img id='user_avatar_img' src='/{{ alias }}/uploads/avatar/"+payload.msg.avatar+"' class='avatar'><br>";
if(payload.msg.userid=={{app.user.id}}) {
html+="<i class='delmessage fa fa-trash fa-fw' data-id='"+payload.msg.id+"' style='cursor: pointer;'></i>";
}
html+="</div>";
html+="<div class='msgdiv'>"
html+="<div class='msgtitle'>"+payload.msg.lastname+"<br><small>"+new Date(payload.msg.submitdate.date).toLocaleDateString("fr-FR", dateoptions)+"</small></div>";
html+="<div class='msgtopic'>"+payload.msg.message+"</div>";
html+="</div>";
html+="</div>";
$(".mychat").prepend(html);
console.log("Received message", payload.msg);
console.log("Received message by "+payload.msg.userid+" = ",payload.msg);
}
if(payload.log) {
console.log("Received message", payload.log);
}
if(payload.del) {
$("#message-"+payload.del).remove();
}
if(payload.alert) {
alert(payload.alert);
}
});
$( "#sendbtn" ).click(function() {
var data = CKEDITOR.instances["chat_message"].getData();
if(data) session.publish("websocket/channel/{{groupid}}", data);
CKEDITOR.instances["chat_message"].setData('');
if(data) {
event={type: "add", message: data};
session.publish("websocket/channel/{{groupid}}", event);
CKEDITOR.instances["chat_message"].setData('');
}
});
$(document).on('click', '.delmessage', function(){
console.log("suppression");
id=$(this).data("id");
event={type: "del", id: id};
session.publish("websocket/channel/{{groupid}}", event);
});
})
{% endif %}
});
{% endblock %}

View File

@ -79,29 +79,39 @@ class WebsocketTopic implements TopicInterface
$usergroup=$this->em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(['keyvalue'=> $userkey]);
if(!$usergroup) {
$topic->broadcast(['log' => 'user NOT IN GROUP = '.$userkey]);
$topic->broadcast(['log' => 'user NOT IN GROUP']);
}
else {
$user=$usergroup->getUser();
}
if($group&&$usergroup&&$user) {
$message=new Message();
$message->setTopic($event);
$message->setUser($user);
$message->setGroup($group);
$this->em->persist($message);
$this->em->flush();
if($event["type"]=="add") {
$message=new Message();
$message->setTopic($event["message"]);
$message->setUser($user);
$message->setGroup($group);
$this->em->persist($message);
$this->em->flush();
$return["lastname"]=$user->getLastname()." ".$user->getFirstname();
$return["avatar"]=$user->getAvatar();
$return["submitdate"]=$message->getSubmitdate();
$return["message"]=$event;
$return["id"]=$message->getId();
$return["lastname"]=$user->getLastname()." ".$user->getFirstname();
$return["avatar"]=$user->getAvatar();
$return["submitdate"]=$message->getSubmitdate();
$return["message"]=$event["message"];
$return["userid"]=$user->getId();
//this will broadcast the message to ALL subscribers of this topic.
$topic->broadcast([
'msg' => $return,
]);
//this will broadcast the message to ALL subscribers of this topic.
$topic->broadcast(['msg' => $return]);
}
if($event["type"]=="del") {
$message=$this->em->getRepository("CadolesWebsocketBundle:Message")->find($event["id"]);
if($message&&($usergroup->getFgmanager()||$message->getUser()==$user)) {
$topic->broadcast(['del' => $message->getId()]);
}
else $topic->broadcast(['log' => "Suppression interdite"]);
}
}
}