var messageTxt; var messages; $(function () { messageTxt = $("#messageTxt"); messages = $("#messages"); /* secure wss because we ListenTLS */ w = new Ws("wss://" + HOST + "/my_endpoint"); w.OnConnect(function () { console.log("Websocket connection established"); }); w.OnDisconnect(function () { appendMessage($("

Disconnected

")); }); w.On("chat", function (message) { appendMessage($("
" + message + "
")); }); $("#sendBtn").click(function () { w.Emit("chat", 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; } }