-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
60 lines (56 loc) · 2.59 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
<!doctype html>
<html>
<head>
<title>Chat</title>
<style>
* { padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form {padding: 3px; position: fixed; bottom: 0; width: 100%; margin-right: 0.5%; margin-bottom: 0; background-color: #fff; }
form input { border: solid 1px #000; padding: 10px; width: 63%; margin-right: .5%; margin-left: 0 }
form button { min-width: 10%; background: #8BC34A; border: none; padding: 11px 15px; }
#my_id {min-width: 15%; margin-left: .5%; margin-right: 0; color: #fff; background-color: #000; padding: 9px; border: solid 1px #000; text-align: center; }
#messages { list-style-type: none; margin: 0 0 100px 0; padding: 0; height: 750px; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function() {
var socket = io();
socket.emit('connection success', " joined");
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(data){
var sender_id = $('#uid').text(); //retrieved from hidden field
if (sender_id == data.user_id) {
$('#messages').append("<li style='width:100%; float:right;'><span style = 'color : #fff; background-color: green; padding: 3px 10px;float:right; border: 2px solid; border-radius: 25px;'>" + data.msg + '</span>');
} else {
$('#messages').append("<li><span style = 'color : blue'>user-" + data.user_id + '</span> ' + data.msg);
}
});
socket.on('connection success',function(data){
$('#messages').append("<li style= 'color:red;'>user-" + data.user_id + data.msg);
});
socket.on('disconnected',function(msg){
$('#messages').append($("<li style='color:#ccc;'>").text(msg + " left"));
});
socket.on('my id',function(user_id){
$('#my_id').html('user-'+ user_id);
$('#uid').html(user_id); // to store uid in hidden field for future purpose
});
});
</script>
<body>
<span id='uid' style="display: none"></span> <!-- to store id in hidden field -->
<ul id="messages"></ul>
<div id="info"></div>
<form action="" style="display: flex">
<span id="my_id" title="Your ID">receiving user id</span><input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>