-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.slim
78 lines (69 loc) · 2.43 KB
/
index.slim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
html
link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"
css:
#message-box{
height: 300px;
overflow: auto; scroll;
background-color: #989898;
padding-left: 10px;
margin-bottom: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
.submited-message{
padding-left: 10px;
}
body
.container
.row
.col-lg-8.col-lg-offset-2
h1 Mini Chatter
#message-box.row-fluid
.row-fluid
form#new-message role="form"
.form-group
label for="content" Type message
input#new-message-content.form-control placeholder=("Enter message") type="content"
button.btn.btn-default type="submit" Submit
javascript:
var username = "User_" + Math.floor((Math.random()*1000)+1);
var connection = new WebSocket('ws://'+ location.host + '/?username=' + username);
connection.onmessage = function(e){
var response = JSON.parse(e.data);
appendToMessageBox(response.username, response.message);
}
document.forms["new-message"].onsubmit = function(){
var messageText = document.getElementById("new-message-content");
if(messageText.value != ''){
var newMessage = {
'username': username,
'message': messageText.value
};
connection.send(JSON.stringify(newMessage));
messageText.value ='';
}
return false;
}
function appendToMessageBox(name, message){
var newContent = document.createElement('h4');
var nameLabel = document.createElement('span');
var labelClass = 'label label-';
if(message == "connected"){
labelClass += 'success';
}else if(message == "disconnected"){
labelClass += 'danger';
}else{
labelClass += 'primary';
}
nameLabel.className = labelClass;
nameLabel.innerHTML = name;
var messageContent = document.createElement('span');
messageContent.className = "submited-message"
messageContent.innerHTML = message;
newContent.appendChild(nameLabel)
newContent.appendChild(messageContent)
var messageBox = document.getElementById("message-box");
messageBox.appendChild(newContent);
messageBox.scrollTop = messageBox.scrollHeight;
}