-
Notifications
You must be signed in to change notification settings - Fork 21
/
chat.html
45 lines (42 loc) · 1.59 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
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<script type="text/javascript">
if(window.WebSocket){
var webSocket = new WebSocket("wss://game.fx4j.com:9507");
webSocket.onopen = function (event) {
webSocket.send('{"c":"game", "m":"ver", "userid":"123593"}');
};
webSocket.onmessage = function (event) {
var content = document.getElementById('content');
if(event.data instanceof Blob) {
var img = document.createElement("img");
img.src = window.URL.createObjectURL(event.data);
content.appendChild(img);
}else {
content.innerHTML = content.innerHTML.concat('<p style="margin-left:20px;height:20px;line-height:20px;">'+event.data+'</p>');
}
};
var sendMessage = function(){
var data = document.getElementById('message').value;
webSocket.send(data);
document.getElementById('message').value = '';
}
}else{
console.log("fail to connect to websocket");
}
</script>
</head>
<body>
<div style="width:600px;margin:0 auto;border:1px solid #ccc;">
<div id="content" style="overflow-y:auto;height:300px;"></div>
<hr/>
<div style="height:40px">
<input type="text" id="message" style="margin-left:10px;height:25px;width:450px;">
<button onclick="sendMessage()" style="height:28px;width:75px;">发送</button>
</div>
</div>
</body>
</html>