Skip to content

Show 'typing...' while user writes some text to other #20 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions public/assests/js/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ function setConversationDetails(details) {

// Update Current Conversation
function updateConversation(data) {

var ele = document.getElementById("conversation");
ele.innerHTML = "";

Expand All @@ -200,7 +199,18 @@ function updateConversation(data) {
var divElement1 = $("<div></div>").addClass("row message-body");
var divElement2 = $("<div></div>").addClass("col-sm-12");
var divElement3 = $("<div></div>");
var messageText = $("<div></div>").addClass("message-text").text(data[i].message);
var messageText = $("<div></div>").addClass("message-text");

// Embed YouTube video if link presents in message.
var youtube_link = data[i].message.indexOf('https://www.youtube.com');
if(youtube_link !== -1) {
messageText.html(data[i].message + '<iframe width="100%" src="https://www.youtube.com/embed/' +
data[i].message.substring(youtube_link + 32, youtube_link + 43) + '" frameborder="0" allowfullscreen></iframe>');
}
else {
messageText.text(data[i].message);
}

var spanElement = $("<span></span>").addClass("message-time pull-right").text(data[i].time);

if (data[i]["sent_by"] !== data[i].start)
Expand All @@ -224,6 +234,7 @@ function updateConversation(data) {
ele.scrollTop = $("#conversation")[0].scrollHeight - heightFrom;
}
}

setConversationDetails(data[0]);
}

Expand All @@ -243,6 +254,16 @@ function reply() {
}
}

// Typing indication
function typing() {
var id = $("#text_reply").attr("name");
var msg = {
"name": id,
"type": "typing"
};
sendTo(msg);
}

function notFound(eleId) {
eleId = "#" + eleId;
$(eleId).text("");
Expand Down Expand Up @@ -346,7 +367,7 @@ function startDictation() {
conn.onmessage = function(e)
{
var msg = JSON.parse(e.data);
// console.log(msg);

if (!width()) {
SideBar(msg.sidebar);
} else {
Expand Down Expand Up @@ -377,6 +398,15 @@ conn.onmessage = function(e)
if (typeof(msg.Compose) !== "undefined") {
composeResult(msg.Compose);
}

if (typeof(msg.typing) !== "undefined") {
if($('#user_typing').length == 0) {
$('#conversation').append('<div class="row message-body" id="user_typing" style=""><div class="col-sm-12 message-main-receiver"><div class="receiver"><span class="message-time pull-right">typing...</span></div></div></div>');
var element = document.getElementById("conversation");
element.scrollTop = element.scrollHeight;
setTimeout(function() { $('#user_typing').remove(); }, 1000);
}
}
};

// Event Listeners
Expand All @@ -391,6 +421,11 @@ conn.onmessage = function(e)
function() {
reply();
});

$("body").on("change keyup paste", "#text_reply",
function() {
typing();
});

$("body").on("click", ".reply-recording",
function() {
Expand Down
8 changes: 8 additions & 0 deletions src/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ public function onMessage(ConnectionInterface $from, $msg)
$msg->userId = $from->userId;
$composeResult = $this->onCompose($msg);
$from->send($composeResult);
} else if ($msg->type == 'typing') {
$msg->userId = $from->userId;
$msg->name = convert_uudecode(hex2bin($msg->name));
foreach ($this->clients as $client) {
if ($client->userId == $msg->name) {
$client->send(json_encode(array('typing' => 'typing')));
}
}
} else {
$msg->userId = $from->userId;
$msg->name = convert_uudecode(hex2bin($msg->name));
Expand Down