mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 11:11:03 +01:00
7c5d7cae05
Part 2. Former-commit-id: 3ccb7c259e86c0b6e5147d372aa9cff10c1b5bb1
39 lines
837 B
JavaScript
39 lines
837 B
JavaScript
var messageTxt;
|
|
var messages;
|
|
|
|
$(function () {
|
|
|
|
messageTxt = $("#messageTxt");
|
|
messages = $("#messages");
|
|
|
|
|
|
w = new Ws("ws://" + HOST + "/my_endpoint");
|
|
w.OnConnect(function () {
|
|
console.log("Websocket connection established");
|
|
});
|
|
|
|
w.OnDisconnect(function () {
|
|
appendMessage($("<div><center><h3>Disconnected</h3></center></div>"));
|
|
});
|
|
|
|
w.On("chat", function (message) {
|
|
appendMessage($("<div>" + message + "</div>"));
|
|
});
|
|
|
|
$("#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;
|
|
}
|
|
}
|