-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.html
115 lines (96 loc) · 2.25 KB
/
client.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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
<style>
html, body {
font: normal 0.9em arial, helvetica;
}
#log {
padding: 10px;
width: 420px;
height: 180px;
border: 1px solid #7F9DB9;
overflow: auto;
}
#msg {
margin-top: 10px;
width: 436px;
}
</style>
<script>
// The socket which will be used to communicate with the server
var socket;
/**
* This function initializes the WebSocket to use
**/
function init(){
try{
// Create the socket
socket = new WebSocket("ws://localhost:12345/server.php");
log('WebSocket - status '+socket.readyState);
// Listen for Open, Message & Close events
socket.onopen = function(msg){
log("Welcome - status "+this.readyState);
};
socket.onmessage = function(msg){
log("Received: "+msg.data);
};
socket.onclose = function(msg){
log("Disconnected - status "+this.readyState);
};
} catch(ex){
log(ex);
}
$("msg").focus();
}
/**
* This function sends data to the server through the socket
**/
function send(){
var txt = $("msg");
var msg = txt.value;
// Make sure we have something to send
if(msg.length == 0){
return alert("Message can not be empty");
}
// Clear the text field and make sure it still has focus
txt.value = "";
txt.focus();
try{
socket.send(msg);
log('Sent: '+msg);
} catch(ex){
log(ex);
}
}
/**
* Shortcut for gEBI
**/
function $(id){
return document.getElementById(id);
}
/**
* Function used to log messages on the page
**/
function log(msg){
$("log").innerHTML += msg + "<br>";
$("log").scrollTop = $("log").scrollTopMax;
}
/**
* Handler for the keypress event on the textfield
* to allow sending data when ENTER is pressed
**/
function onkey(event){
if(event.keyCode==13){
send();
}
}
</script>
</head>
<body onload="init()">
<h3>EchoServer v1.00</h3>
<div id="log"></div>
<input id="msg" type="text" onkeypress="onkey(event)"/>
</body>
</html>