forked from rvirding/chat_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
140 lines (124 loc) · 3.77 KB
/
chat.html
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<html>
<head>
<script type="text/javascript" src="jquery-1.5.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
if ("WebSocket" in window) {
// browser supports websockets
// var ws = new WebSocket("ws://localhost:8080/service");
var ws = new WebSocket("ws://%%HOST:PORT%%/service");
ws.onopen = function() {
// websocket is connected
addStatus("websocket connected!");
// send hello data to server.
ws.send("hello server!");
};
ws.onmessage = function(evt) {
// Message has form "<dest> ! <text>"
// like and Erlang send.
var data = evt.data;
var i = data.indexOf(" ! "); //From server
var tag = data.slice(0, i);
if (tag == "output") { //Chat output
addOutput(data.slice(i+3));
}
else if (tag == "status") { //Status info
addStatus(data.slice(i+3));
}
else { //Unknown, just log it
addStatus("server sent: '" + tag + "'" + data + "'");
};
};
ws.onclose = function() {
// websocket was closed
addStatus("websocket was closed");
};
// Other events
send_input = function(id, clear) {
var val=$("#"+id).val();
if (clear) { $("#"+id).val(""); };
ws.send(id + " ! " + val);
return true; // must do this
};
} else {
// browser does not support websockets
addStatus("sorry, your browser does not support websockets.");
};
});
function addStatus(text){
var date = new Date();
var old = document.getElementById('status').innerHTML;
document.getElementById('status').innerHTML =
date + ": " + text + "<br>" + old;
};
function addOutput(text){
var old = document.getElementById('output').innerHTML;
document.getElementById('output').innerHTML =
old + "<p>" + text + "</p>";
};
</script>
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<style>
body {
background-color: #ADB04D;
font-family: 'Ubuntu', arial, serif;
font-size: 14px;
}
#main {
width: 600px;
padding: 20px;
margin: 40px auto;
background: white;
-webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.3), 0 0 60px rgba(0, 0, 0, 0.1) inset;
-moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
}
#output {
height: 300px;
width: 100%;
overflow: auto;
-moz-border-radius: 3px;
border-radius: 3px;
border: 1px solid #aaa;
}
#output p {
padding: 8px;
margin: 0;
}
#output p:nth-child(odd) {
background: #F6F6F6;
}
input {
padding: 5px;
background: #fff;
-moz-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3) inset;
-moz-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3) inset;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3) inset;
border: none;
border: 1px solid #aaa;
}
h1 {}
#msg, #output {
width: 100%;
}
#status {
border: 3px solid #e28c33;
background-color: #FEE77D;
padding: 5px 10px;
font-family: monospace;
}
</style>
</head>
<body>
<div id="main">
<h1>Erlang Websocket Chat</h1>
<p><label>Nick <input id="nick" onchange="send_input('nick', false); return false">
</label></p>
<div id="output"></div>
<p><input id="msg" onchange="send_input('msg', true); return false;"></p>
<div id="status"></div>
<div>
</body>
</html>