iris/_examples/intermediate/websockets/native-messages/static/js/chat.js
Gerasimos (Makis) Maropoulos 7c5d7cae05 Add More Examples & Categorized in Folders & TOC
Part 2.


Former-commit-id: 3ccb7c259e86c0b6e5147d372aa9cff10c1b5bb1
2017-03-24 00:25:59 +02:00

39 lines
834 B
JavaScript

var messageTxt;
var messages;
$(function () {
messageTxt = $("#messageTxt");
messages = $("#messages");
w = new WebSocket("ws://" + HOST + "/my_endpoint");
w.onopen = function () {
console.log("Websocket connection enstablished");
};
w.onclose = function () {
appendMessage($("<div><center><h3>Disconnected</h3></center></div>"));
};
w.onmessage = function(message){
appendMessage($("<div>" + message.data + "</div>"));
};
$("#sendBtn").click(function () {
w.send(messageTxt.val().toString());
messageTxt.val("");
});
})
function appendMessage(messageDiv) {
var theDiv = messages[0];
var doScroll = theDiv.scrollTop == theDiv.scrollHeight - theDiv.clientHeight;
messageDiv.appendTo(messages);
if (doScroll) {
theDiv.scrollTop = theDiv.scrollHeight - theDiv.clientHeight;
}
}