-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·142 lines (116 loc) · 5.45 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
<!doctype html>
<html>
<head>
<title>Chat básico</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #bbbbbb; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form .form-control { border: 0; padding: 10px; width: 100%; margin-right: .5%; }
form button { width: 9%;padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 10px 15px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<div class="col-md-9">
<h2>
<i class="glyphicon glyphicon-comment"></i>
Conversación actual
</h2>
<ul id="messages"></ul>
</div>
<div class="col-md-3 hidden-sm hidden-xs">
<h2>
<i class="glyphicon glyphicon-off"></i>
Usuarios online
</h2>
<div id="users">
</div>
<div id="user-panel" class="panel panel-default" style="display:none">
<div class="panel-body">
<i class="glyphicon glyphicon-user"></i> Usuario <span class="user-number"></span> se ha conectado
</div>
</div>
</div>
<form id="sendMessage" class="form-inline" action="">
<div class="form-group" style="width:90%">
<input style="width:100%" class="form-control" placeholder="Escriba su mensaje" id="m" autocomplete="off" />
</div>
<button class="btn btn-default">Enviar</button>
</form>
<!-- Modal -->
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Seleccionar un cuarto</h4>
</div>
<div class="modal-body">
<label for="cuarto">Nombre del cuarto</label>
<input type="text" class="form-control" id="cuarto" placeholder="nombre del cuarto">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
<button id="conectarCuarto" type="button" class="btn btn-primary">Ingresar</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$('#myModal').modal('show');
});
$('#conectarCuarto').click(function(){
$('#myModal').modal('hide');
var room = $('#cuarto').val();
// Usuario actual
var number = 0;
// Abre conexión bi-direccional con el servidor
var socket = io();
socket.on('connect', function(){
socket.emit("join room", room);
});
// Función que se ejecuta al momento de hacer submit al formulario
$('#sendMessage').submit(function(){
// Emite (publica) un nuevo mensaje en el tópico 'chat message' (que sólo recibe el servidor)
// socket.to(room).emit('chat message', $('#m').val()+';@'+number);
socket.emit('chat message', $('#m').val()+';@'+number);
$('#m').val('');
return false;
});
// En el momento en que llegue un mensaje del tópico 'chat message' ejecuta la función
socket.on('chat message', function(msg){
// Agrega un nuevo mensaje a la lista de mensajes
var message = msg.split(';@')[0];
var userNumber = msg.split(';@')[1];
$('#messages').append($('<li>').text('Usuario ' + userNumber + ': ' + message));
});
// En el momento en que llegue un mensaje del tópico 'new user' ejecuta la función
socket.on('new user', function(msg){
// Agrega un nuevo panel indicando que hay uno nuevo usuario
var userPanel = $( "#user-panel" ).clone().css('display','block').attr('id','');
userPanel.find('.user-number').text(msg);
$('#users').append(userPanel);
if(number === 0)
number = msg;
});
// En el momento en que llegue un mensaje del tópico 'new user' ejecuta la función
socket.on('user disconnected', function(msg){
console.log("Diconnected:" + msg.user);
var panel = $("#users");
var mySpan = panel.find("span:contains("+msg.user+")");
var myDiv = mySpan.parent();
myDiv.html('<i class="glyphicon glyphicon-user"></i> Usuario <span class="user-number">'+msg.user+'</span> se ha desconectado');
myDiv.css( "background-color", "red" );
});
});
</script>
</body>
</html>