I have a small shoutbox chat written with PHP and Ratchet.
In JS, I have the following event, which fires a regular js function which happens on pressing enter at the moment:
socket.onmessage = function(evt) {
var data = JSON.parse(evt.data);
addMessage(data.msg);
};
That works, but I want to write a message "user is typing" while the user is typing and before the message is actually sent, so I am guessing I need to call the onmessage
event manually, is that possible?
relevant js:
var chat = $('#chatwindow');
var msg = $('#messagebox');
function addMessage(msg) {
chat.append("<p>" + msg + "</p>");
}
msg.keypress(function( event ) {
if ( event.which != 13 ) {
return;
}
event.preventDefault();
if (msg.val() == "" || !open) {
return;
}
socket.send(JSON.stringify({
msg: msg.val()
}));
addMessage(msg.val());
msg.val("");
});
socket.onmessage = function(evt) {
var data = JSON.parse(evt.data);
addMessage(data.msg);
};