-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_socket_html.html
83 lines (80 loc) · 2.01 KB
/
test_socket_html.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
<!DOCTYPE html>
<head>
<title>WebSocket</title>
<meta charset="utf-8">
<style>
html,body{font:normal 0.9em arial,helvetica;}
#log {width:440px; height:200px; border:1px solid #7F9DB9; overflow:auto;}
#msg {width:440px;}
</style>
<script>
var socket;
function init(){
var host = "ws://127.0.0.1:9080/";
try{
socket = new WebSocket(host);
socket.onopen = function(msg){
console.log("socket session create sucess!");
log("socket session create sucess!");
};
socket.onmessage = function(msg){
console.log("message Sucess");
//socket.send("Hello");
//log(msg.data);
log(msg.data)
};
socket.onclose = function(msg){ console.log("close Sucess"); log("Connection Lose!"); };
socket.onerror = function(msg){ console.log("Error!"); };
}catch(ex){
log(ex);
}
$("msg").focus();
}
function send(){
var txt,msg;
txt = $("msg");
msg = txt.value;
console.log("message is", msg)
if(!msg){
alert("Message can not be empty");
return;
}
txt.value="";
txt.focus();
try{
//log(msg);
socket.send(msg);
}catch(ex){
log(ex);
}
}
window.onbeforeunload=function(){
try{
socket.send('quit');
socket.close();
socket=null;
}catch(ex){
log(ex);
}
};
function $(id){
return document.getElementById(id);
}
function log(msg){
$("log").innerHTML+="<br>"+msg;
}
function onkey(event){
if(event.keyCode==13){
send();
}
}
</script>
</head>
<body onload="init()">
<h3>WebSocket</h3>
<br><br>
<div id="log"></div>
<input id="msg" type="textbox" onkeypress="onkey(event)"/>
<button onclick="send()">发送</button>
</body>
</html>