-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
221 lines (174 loc) · 6.9 KB
/
index.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
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
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="https://www.freepngimg.com/download/chat/12-2-chat-png.png" type="image/gif" sizes="16x16">
<script type="text/javascript" src="/node_modules//browser-image-compression//dist/browser-image-compression.js"
defer="defer"></script>
<title>Socket.IO chat</title>
<link rel="stylesheet" href="style.css" />
<style>
</style>
</head>
<body>
<div id="overlay">
<div class="inner">
<input type="text" id="user" autocomplete="off" placeholder="Enter any username for profile"><button
id="senduser">Start</button>
</div>
</div>
<ul id="messages"></ul>
<form action="" style="display: flex; flex-direction: column;">
<div id="feedback"></div>
<div style="display: flex;">
<label for="uploadfile"><span style="color: white;">Filetypes: mp4, mkv, ogg, mp3, wav, aac, all image format
including gifs</span>
<input type="file" id="uploadfile" />
</label>
<span id="progress">uploading: <span id="percentage"></span> %</span>
</div>
<input id="m" autocomplete="off" placeholder="type message" /><button>Send</button>
</form>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
</body>
<script src="/socket.io/socket.io.js"></script>
<script src="/node_modules//jquery/dist/jquery.min.js"></script>
<script>
$(function () {
var socket = io();
var username = $('#user');
var senduser = $('#senduser');
$('form').submit(function (e) {
e.preventDefault(); // prevents page reloading
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function (msg) {
$('#messages').append($('<li>').html(msg));
});
senduser.click(function () {
console.log(username.val());
socket.emit("change_username", { username: username.val() })
$('#overlay').hide();
})
$('#m').bind("keypress", () => {
$("html, body").animate({ scrollTop: $('#messages').height() }, 100);
socket.emit('typing');
})
$('#m').keyup(function () {
socket.emit('stop typing');
});
socket.on('typing', (data) => {
$('#feedback').fadeIn();
$('#feedback').html("<p><i>" + data.username + " is typing a message...." + "</i></p>");
})
socket.on('stop typing', (data) => {
setTimeout($('#feedback').fadeOut(), 1000);
})
//serving images and video files
// $('#uploadfile').bind('change', function (e) {
// var data = e.originalEvent.target.files[0];
// readThenSendFile(data);
// });
$('#uploadfile').bind('change', async function (event) {
const imageFile = event.target.files[0];
console.log('originalFile instanceof Blob', imageFile instanceof Blob); // true
console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
const options = {
maxSizeMB: 0.2,
maxWidthOrHeight: 600,
useWebWorker: true
}
try {
const compressedFile = await imageCompression(imageFile, options);
console.log('compressedFile instanceof Blob', compressedFile instanceof Blob); // true
console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`); // smaller than maxSizeMB
await readThenSendFile(compressedFile); //readin the compressed file
} catch (error) {
console.log(error);
await readThenSendFile(imageFile); // if filetype is not image then sent orignal data without compression
}
});
function readThenSendFile(data) {
//show progress msg
$('#progress').fadeIn(100);
var reader = new FileReader();
reader.onload = function (evt) {
var msg = {};
msg.username = username;
msg.file = evt.target.result;
msg.fileName = data.name;
socket.emit('base64 file', msg);
};
reader.readAsDataURL(data);
reader.onprogress = function (currentFile) {
if (currentFile.lengthComputable) {
var progress = parseInt(((currentFile.loaded / currentFile.total) * 100), 10);
$('#percentage').html(progress);
console.log(progress);
}
}
reader.onerror = function () {
alert("Could not read the file: large file size");
};
}
socket.on('base64 file', (data) => {
//hide progress msg when data received
$('#progress').hide();
//appending data according to data types
let filetype = data.fileName.split('.').pop();
if (filetype == 'mp4' || filetype == 'ogg' || filetype == 'mkv') {
$('#messages').append($('<li>').html(`<p class="username">${data.username}</p><video class="imgupload" src="${data.file}" height="400" width="400" controls/>`));
} else if (filetype == 'mp3' || filetype == 'wav' || filetype == 'aac') {
$('#messages').append($('<li>').html(`<p class="username">${data.username}</p><audio class="imgupload" src="${data.file}" height="400" width="400" controls/>`));
} else {
$('#messages').append($('<li>').html(`<p class="username">${data.username}</p><img class="imgupload" src="${data.file}" height="200" width="200" onclick="showimg(this)"/>`));
}
})
});
//compressing image
// async function handleImageUpload(event) {
// const imageFile = event.target.files[0];
// console.log('originalFile instanceof Blob', imageFile instanceof Blob); // true
// console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
// const options = {
// maxSizeMB: 1,
// maxWidthOrHeight: 600,
// useWebWorker: true
// }
// try {
// const compressedFile = await imageCompression(imageFile, options);
// console.log('compressedFile instanceof Blob', compressedFile instanceof Blob); // true
// console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`); // smaller than maxSizeMB
// await uploadToServer(compressedFile); // write your own logic
// } catch (error) {
// console.log(error);
// }
// }
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
function showimg(data) {
var img = data;
var modalImg = document.getElementById("img01");
img.onclick = function () {
modal.style.display = "block";
modalImg.src = this.src;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
}
}
</script>
</html>