-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient.html
162 lines (137 loc) · 4.1 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Harbour websocket server</title>
</head>
<style>
.myhead {
padding: 5px;
background-color: white;
margin-bottom: 20px;
}
.myhead > span {
font-size: 25px;
font-family: times, Times New Roman, times-roman, georgia, serif;
font-size: 28px;
line-height: 40px;
letter-spacing: -1px;color: #444;
}
#mylogo {
width: 130px;
margin-left: 10px;
margin-right: 10px;
vertical-align: middle;
}
#harbour {
height: 180px;
margin-bottom: 50px;
}
#content {
text-align: center;
}
input[type=text], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 20px;
}
.btn {
width: 15%;
background-color: #4CAF50;
color: white;
padding: 13px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 30px;
margin-bottom: 20px;
margin-right: 10px;
margin-left: 10px;
font-size: 20px;
}
#btn:hover {
background-color: #45a049;
}
</style>
<body>
<div class="myhead">
<span>Harbour websocket server - client demo</span>
<hr>
</div>
<div id="content">
<a href='https://harbour.github.io'>
<img id="harbour" title='The Harbour project' src="https://github.com/FiveTechSoft/screenshots/blob/master/harbour.png?raw=true"></a><br>
<input type="text" id="msg"><br>
<button class="btn" onclick="Send( document.getElementById( 'msg' ).value )">Send</button>
<button class="btn" onclick="SendFile()">Send file</button>
<button class="btn" id='onoff' onclick='OnOff()'>Disconnect</button>
<button class="btn" onclick="Clear()">Clear</button>
<br><a style='font-size:20' href="https://fivetechsoft.github.io/mod_harbour/">modharbour.org</a>
<br><br><textarea id="output" rows="20" cols="140"></textarea>
</div>
<script>
var socket;
Connect();
function Send( cMsg ) {
if( socket == null )
Connect();
socket.send( cMsg );
}
function OnOff() {
if( socket == null )
Connect();
else
Send( 'exit' );
}
function Connect() {
socket = new WebSocket( "ws://localhost:9000" );
socket.onopen = function( e ) {
document.getElementById( 'output' ).innerHTML = "Connection established\n";
socket.send( "Harbour websocket server" );
document.getElementById( 'onoff' ).innerHTML = 'Disconnect';
};
socket.onmessage = function( event ) {
document.getElementById( 'output' ).innerHTML += event.data + '\n';
};
socket.onclose = function( event ) {
document.getElementById( 'output' ).innerHTML += event.code + '\n';
document.getElementById( 'output' ).innerHTML += event.reason + '\n';
document.getElementById( 'output' ).innerHTML += ( ! event.wasClean ? "": "not " ) +
"clean exit" + '\n';
socket = null;
document.getElementById( 'onoff' ).innerHTML = 'Connect';
};
socket.onerror = function(error) {
alert( 'Please run wsserver.exe first on your computer' ); // `[error] ${error.message}`);
document.getElementById( 'msg' ).innerHTML = 'Disconnect';
};
}
function SendFile() {
var filePicker = document.createElement( "input" );
filePicker.type = "file";
filePicker.accept = ".*";
filePicker.style.visibility = "hidden";
filePicker.onchange = function( evt ) {
var reader = new FileReader();
var file = evt.target.files[ 0 ];
reader.readAsDataURL( file );
reader.onload = function( e ) {
Send( e.target.result );
}
};
filePicker.click();
}
function Clear() {
document.getElementById( 'msg' ).value = '';
document.getElementById( 'output' ).innerHTML = '';
}
</script>
</body>
</html>