This repository was archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
59 lines (49 loc) · 1.97 KB
/
main.js
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
function connect() {
try {
const protocolSelect = document.getElementById("protocol-select");
const portInput = document.getElementById("port-input");
const url = protocolSelect.value + "://127.0.0.1:" + portInput.value;
var connection = new WebSocket(url);
// When the connection is open, send some data to the server
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
// Log errors
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
const responseField = document.getElementById("response-field");
responseField.innerText += "Error: " + error + "\n";
};
// Log messages from the server
connection.onmessage = function (e) {
const responseField = document.getElementById("response-field");
responseField.innerText += e.data + "\n";
// const preview = document.querySelector('img');
// preview.src = "data:image/png;base64," + e.data;
// const reader = new FileReader();
// reader.addEventListener("load", function () {
// // convert image file to base64 string
// preview.src = reader.result;
// }, false);
//
// reader.readAsDataURL(e.data);
};
}
catch (e) {
console.error(e);
const responseField = document.getElementById("response-field");
responseField.innerText += "Error: " + e + "\n";
}
}
function previewFile() {
const preview = document.querySelector('img');
const file = document.querySelector('input[type=file]').files[0];
const reader = new FileReader();
reader.addEventListener("load", function () {
// convert image file to base64 string
preview.src = reader.result;
}, false);
if (file) {
reader.readAsDataURL(file);
}
}