forked from caosiyang/websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.htm
91 lines (85 loc) · 2.57 KB
/
chat.htm
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
<html>
<head>
<script type="text/javascript">
var host = "ws://122.70.141.32:10086";
var ws;
var b_connect = false;
function connect() {
if (!b_connect) {
ws = new WebSocket(host);
ws.onopen = function(evt) {
b_connect = true;
document.getElementById('connect_button').value = 'disconnect';
chat_content_append("connect to server done");
};
ws.onclose = function(evt) {
b_connect = false;
document.getElementById('connect_button').value = 'connect';
chat_content_append("disconnected");
};
ws.onmessage = function(evt) {
chat_content_append(evt.data);
};
ws.onerror = function(evt) {
alert("error");
};
} else {
ws.close();
}
}
function send() {
if (b_connect) {
if (document.getElementById('username').value == '') {
chat_content_append("please input your name");
return;
}
if (document.getElementById('chat_input').value != '') {
var message = document.getElementById('username').value + ": " + document.getElementById('chat_input').value;
ws.send(message);
document.getElementById('chat_input').value = '';
chat_content_append(message);
}
} else {
chat_content_append("please click 'connect' button to connect to the webchat server");
}
}
function login() {
if (b_connect) {
var message = "command=login\r\nuid=123456\r\nencpass=abcdef\r\nroomid=1001\r\n";
message = message.length.toString() + "\r\n" + message;
ws.send(message);
} else {
chat_content_append("please click 'connect' button to connect to the webchat server");
}
}
function chat_content_append(line) {
var chat_area = document.getElementById('chat_content');
chat_area.value += line + "\n";
chat_area.scrollTop = chat_area.scrollHeight;
}
//function document.onkeydown() {
//if (event.keyCode == 13) {
//alert("ahha");
//}
//}
</script>
</head>
<body>
<h1>WebSocket Demo</h1>
Server:
<input type='text' value='122.70.141.32:10086' />
<input type='button' id='connect_button' value='connect' onclick='connect()' />
<br />
Message:
<br/>
<textarea rows='20' cols='100' id='chat_content' readonly></textarea>
<br />
Username:
<input type='text' id='username' />
<br />
Input:
<input type='text' id='chat_input' />
<input type='button' value='send' onclick='send()' />
<input type='button' value='login' onclick='login()' />
</body>
</html>