-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketClient.html
More file actions
284 lines (259 loc) · 9.6 KB
/
WebSocketClient.html
File metadata and controls
284 lines (259 loc) · 9.6 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WebSocket-Client</title>
<style type="text/css">
body {font-family: sans-serif;}
table {empty-cells: show;}
.on {background-color: #00FF00;}
.off {background-color: #FF0000;}
.set_on {background-color: #CCFFCC;}
.set_off {background-color: #FFCCCC;}
.unknown {background-color: #DDDDDD;}
.on:before, .set_on:before {content: "ON";}
.off:before, .set_off:before {content: "OFF";}
.unknown:before {content: "?";}
#output0,#output1{
cursor: pointer;
}
</style>
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.min.js"></script>
<script languange="javascript">
var client = "";
function ClientConnect() {
if(document.getElementById("host").value == "")
{
document.getElementById("host").value = "broker.mqttdashboard.com";
};
if(document.getElementById("port").value == "")
{
document.getElementById("port").value = "8000";
};
if(document.getElementById("clientid").value == "")
{
document.getElementById("clientid").value = "myclientid_" + parseInt(Math.random() * 100, 10);
};
if(document.getElementById("timeout").value == "")
{
document.getElementById("timeout").value = "3";
};
var c_hostname = document.getElementById("host").value;
var c_port = parseInt(document.getElementById("port").value);
var c_clientId = document.getElementById("clientid").value;
//Connect Options
var options = {
useSSL: stringToBoolean(document.getElementById("cbossl").value),
userName : document.getElementById("username").value,
password : document.getElementById("password").value,
timeout: parseInt(document.getElementById("timeout").value),
//Gets Called if the connection has sucessfully been established
onSuccess: function () {
console.log("Connected");
alert("Connected");
},
//Gets Called if the connection could not be established
onFailure: function (message) {
console.log("Connection failed: " + message.errorMessage);
alert("Connection failed: " + message.errorMessage);
}
};
//Using the HiveMQ public Broker, with a random client Id
client = new Paho.MQTT.Client(c_hostname, c_port, c_clientId);
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect(options);
};
function ClientDisconnect() {
client.disconnect();
console.log("Disconnected");
alert("Disconnected");
};
function ClientPublish() {
var p_topic = document.getElementById("p_topic").value;
var p_payload = document.getElementById("p_payload").value;
var p_qos = parseInt( document.getElementById("cbop_qos").value);
//var p_retained = false;
publish(p_payload,p_topic,p_qos);
};
function ClientSubscribe() {
var s_topic = document.getElementById("s_topic").value;
var s_qos = parseInt( document.getElementById("cbos_qos").value);
client.subscribe(s_topic,s_qos);
alert('Subscribed');
};
function onConnectionLost(responseObject) {
//Depending on your scenario you could implement a reconnect logic here
if (responseObject.errorCode !== 0)
console.log("onConnectionLost:" + responseObject.errorMessage);
//alert("connection lost: " + responseObject.errorMessage);
};
function onMessageArrived(message) {
//Do something with the push message you received
document.getElementById('messages').append('Topic: ' + message.destinationName + ' | ' + message.payloadString + '\r\n');
//console.log("onMessageArrived:" + message.payloadString);
//alert("onMessageArrived:" + message.payloadString);
};
//Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
function publish (payload, topic, qos) {
//Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
var message = new Paho.MQTT.Message(payload);
message.destinationName = topic;
message.qos = qos;
client.send(message);
}
function stringToBoolean(string){
switch(string.toLowerCase().trim()){
case "true": case "yes": case "1": return true;
case "false": case "no": case "0": case null: return false;
default: return Boolean(string);
}
}
function checkClick() {
var checked = document.getElementById("checkssl").checked;
if (checked) {
document.getElementById("cbossl").value = "true";
}
else {
document.getElementById("cbossl").value = "false";
}
}
</script>
<table border="0" cellpadding="0" width="100%" height="1">
<tr>
<td>
<div align="right">
<table border="0" cellpadding="0" width="100%" height="21">
<tr>
<td align="left" class="style1">
<font color="#668E39">WebSocket-Client</font></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td width="100%" height="1">
<hr color="#668E39" size="1">
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
<tr>
<td width="25%"><font face="arial" size="1"><b> Welcome, Guest</b></font>
</td>
<td width="25%"> </td>
<td width="25%"> </td>
</tr>
</table>
<table>
<tr>
<td>
<h2>Connection</h2>
</td>
</tr>
</table>
<table>
<tr>
<td>Host : </td>
<td><input type="visible" id ="host" name="host" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td>Port : </td>
<td><input type="visible" id ="port" name="port" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td>ClientID : </td>
<td><input type="visible" id ="clientid" name="clientid" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td><button onclick="ClientConnect();">1. Connect</button></td>
<td><button onclick="ClientDisconnect();">(4. Disconnect)</button></td>
</tr>
<tr>
<td>Username : </td>
<td><input type="visible" id ="username" name="username" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td>Password : </td>
<td><input type="password" id ="password" name="password" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td>Keep Alive : </td>
<td><input type="visible" id ="timeout" name="timeout" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td>
<input type="checkbox" name="checkssl" id="checkssl" onClick="checkClick()">SSL
</td>
<td>
<select size="1" id="cbossl" name="cbossl" style="font-weight: 700;visibility:hidden;">
<option value="false">false</option>
<option value="true">true</option>
</select>
</td>
</tr>
</table>
<table>
<tr>
<td>
<h2>Publish</h2>
</td>
</tr>
</table>
<table>
<tr>
<td>Topic : </td>
<td><input type="visible" id ="p_topic" name="p_topic" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td>QoS : </td>
<td>
<select size="1" id="cbop_qos" name="cbop_qos" style="font-weight: 700">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td><button onclick="ClientPublish();">2. Publish</button></td>
</tr>
<tr>
<td>Message : </td>
<td><input type="visible" id ="p_payload" name="p_payload" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
</tr>
</table>
<table>
<tr>
<td>
<h2>Subscribe</h2>
</td>
</tr>
</table>
<table>
<tr>
<td>Topic : </td>
<td><input type="visible" id ="s_topic" name="s_topic" value="" size="15" style="font-weight: 700; font-family: 'Times New Roman'; font-size: 10.0pt; text-align: center; vertical-align: middle">
</td>
<td>QoS : </td>
<td>
<select size="1" id="cbos_qos" name="cbos_qos" style="font-weight: 700">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td><button onclick="ClientSubscribe();">3. Subscribe</button></td>
</tr>
</table>
<div id="messages"></div>
<table border="0" cellpadding="0" width="100%" height="1">
<tr>
<td width="100%" height="1">
<hr color="#668E39" size="1">
</td>
</tr>
<tr>
<td width="100%" height="1" align="center">
<font face="Microsoft Sans Serif" size="1" color="#999999">Developed by <a href="mailto:deadzone19842002@gmail.com?subject=
GitHub:WebSocket-Client" style="color:#668E39;">Mohamad Aimaduddin</a> - Copyright 2021 MI Development</font></td>
</tr>
</table>
</body>
</html>