-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
262 lines (243 loc) · 8.09 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="favicon.png">
<script data-ad-client="ca-pub-8790289893638852" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<title>Chattin App</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<!-- Begin emoji-picker Stylesheets -->
<!-- <link href="../lib/css/emoji.css" rel="stylesheet"> -->
<!-- End emoji-picker Stylesheets -->
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 13px Helvetica, Arial;
}
#chatdiv {
/* background: #000; */
padding: 3px;
position: fixed;
bottom: 0;
width: 100%;
}
#chatdiv input#m {
border: 1;
border-radius: 1mm;
padding: 10px;
width: 70%;
margin-right: 1%;
margin-bottom: 5mm;
}
#chatdiv input#input {
border: 1;
padding: 10px;
width: 10%;
margin-right: .5%;
}
#chatdiv button {
width: 9%;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
}
#messages {
list-style-type: none;
margin: 0;
padding: 0;
}
#messages li {
padding: 5px 10px;
}
#messages li:nth-child(odd) {
background: #eee;
}
#messages {
margin-bottom: 40px
}
#nameinput {
border: 1;
padding: 10px;
width: 40%;
/* margin-right: .5%; */
}
#roominput {
border: 1;
padding: 10px;
width: 40%;
/* margin-right: .5%; */
}
#joinbtn {
width: 9%;
background: rgb(130, 224, 255);
border: 1 solid #000;
;
padding: 10px;
}
#discbtn {
width: 10%;
background: rgb(130, 224, 255);
padding: 10px;
}
</style>
<meta charset="utf-8">
<title>SocketIO Client</title>
<script src="/socket.io/socket.io.js"></script>
<script src="https://unpkg.com/peerjs@1.0.0/dist/peerjs.min.js"></script>
</head>
<body>
<!-- File input and image output -->
<input type="text" id="nameinput" placeholder="enter your name" autofocus><input type="text" id="roominput"
placeholder="enter room name"><button id="joinbtn">Join</button><button id="discbtn"onClick="window.location.reload();" hidden>Disconnect</button>
<!-- <input type="file" id="input"> -->
<!-- <input type="text" id="inputtxt">
<input type="text" id="inputtxt1"> -->
<ul id="messages" style="font-size: larger;"></ul>
<div id="chatdiv" hidden>
<input type="file" id="input"><input id="m" autocomplete="off" /><button type="submit" id="sendbtn">Send</button>
</div><br>
<!-- PEER -->
<!-- <button id="start">start</button>
<button id="join">join</button>
<input type="text" id="peerchat">
<button id="senddata">send</button> -->
<!-- Client Code -->
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript">
// Get WebSocket
var socket = io();
// Get DOM elements
$('nameinput').focus();
document.getElementById("nameinput").addEventListener("keydown", function (event) {
if (event.keyCode === 13) {
$('#roominput').focus();
}
});
document.getElementById("roominput").addEventListener("keydown", function (event) {
if (event.keyCode === 13) {
socket.emit("join", $('#roominput').val(), $('#nameinput').val());
$('#roominput').hide();
$('#joinbtn').hide();
$('#nameinput').hide();
$('#chatdiv').removeAttr('hidden');
$('#discbtn').removeAttr('hidden');
$('#m').focus();
$('#messages').append($('<li>').html("<p style='text-align:center'><strong>ROOM</strong>:" + $('#roominput').val()));
}
});
document.getElementById("joinbtn").addEventListener("click", function (event) {
socket.emit("join", $('#roominput').val(), $('#nameinput').val());
$('#roominput').hide();
$('#joinbtn').hide();
$('#nameinput').hide();
$('#chatdiv').show();
});
document.getElementById("m").addEventListener("keydown", function (event) {
if (event.keyCode === 13) {
socket.emit("chat", $('#m').val());
$('#messages').append($('<li>').html("<strong>YOU</strong>:" + $('#m').val()));
window.scrollTo(0, document.body.scrollHeight);
$('#m').val('');
$('#m').focus()
}
});
document.getElementById("sendbtn").addEventListener("click", function (event) {
socket.emit("chat", $('#m').val());
$('#messages').append($('<li>').html("<strong>YOU</strong>:" + $('#m').val()));
window.scrollTo(0, document.body.scrollHeight);
$('#m').val('');
$('#m').focus()
});
document.getElementById("input").addEventListener("change", function (event) {
// Prepeare file reader
var file = event.target.files[0];
var fileReader = new FileReader();
fileReader.onloadend = function (event) {
// Send an image event to the socket
var image = event.target.result
$('#messages').append($('<li>').html(`<img src=${image}>`));
socket.emit("image", image);
$('#m').focus();
};
// Read file
fileReader.readAsDataURL(file);
});
socket.on("image", function (image) {
// output.src = image;
$('#messages').append($('<li>').html(`<img src=${image}>`));
});
// socket.on("text", function (text) {
// txtinput1.value = text;
// });
socket.on("chat", function (text) {
$('#messages').append($('<li>').html(text));
window.scrollTo(0, document.body.scrollHeight);
});
//////////////////PEER//////////////
// var peer = null;
// var conn = null;
// var peerid = null;
// document.getElementById("start").addEventListener("click", function (event) {
// peer = new Peer('peer1', { host: 'localhost', port: 9000, path: '/api' });
// peer.on('open', function (id) {
// peerid = id;
// });
// peer.on('connection', function (c) {
// if (conn) {
// c.close()
// return
// }
// conn = c
// conn.on('data', function (data) {
// $('#messages').append($('<li>').html(data));
// });
// conn.on('close', function (data) {
// console.log(data);
// });
// });
// });
// document.getElementById("join").addEventListener("click", function (event) {
// peer = new Peer('peer2', { host: 'localhost', port: 9000, path: '/api' });
// peer.on('open', function (id) {
// peerid = id;
// });
// peer.on('open', function () {
// conn = peer.connect('peer1', {
// reliable: true
// });
// conn.on('open', function () {
// conn.on('data', function (data) {
// $('#messages').append($('<li>').html(data));
// });
// conn.on('close', function (data) {
// console.log(data);
// });
// });
// });
// });
// document.getElementById("peerchat").addEventListener("keydown", function (event) {
// if (event.keyCode === 13) {
// conn.send('<strong style="text-transform:uppercase">' + peerid + '</strong>: ' + $('#peerchat').val());
// $('#peerchat').val('');
// $('#peerchat').focus()
// }
// })
// document.getElementById("senddata").addEventListener("click", function (event) {
// conn.send('<strong style="text-transform:uppercase">' + peerid + '</strong>: ' + $('#peerchat').val());
// $('#peerchat').val('');
// $('#peerchat').focus()
// });
</script>
</body>
</html>