-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.html
142 lines (103 loc) · 3.48 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>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<style>
.j-message {
margin-bottom:50px;
}
.j-footer {
width: 100%;
height: 50px;
position: fixed;
bottom: 0;
background-color:white;
border-top:1px solid black;
}
table {
height: 100%;
}
</style>
</head>
<body>
<div class="j-message">
</div>
<div class="j-footer">
<table>
<tr>
<td width="100%">
<input id="message-input" class="form-control" type="text">
</td>
<td width="20%">
<button id="message-button" class="btn btn-default" type="submit">SEND</button>
</td>
</tr>
</table>
</div>
<script type="text/javascript" src="http://cdn.socket.io/socket.io-1.4.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script>
var serverURL = 'localhost:50000';
var name = 'jin';
var room = '100';
var socket = null;
function writeMessage(type, name, message) {
var html = '<div>{MESSAGE}</div>';
var printName = '';
if(type === 'me') {
printName = name + ' : ';
}
html = html.replace('{MESSAGE}', printName + message);
$(html).appendTo('.j-message');
$('body').stop();
$('body').animate({scrollTop:$('body').height()}, 500);
}
function sender(text) {
socket.emit('user', {
name : name,
message : text
});
writeMessage('me', name, text);
}
$(document).ready(function() {
socket = io.connect(serverURL);
socket.on('connection', function(data) {
console.log('connect');
if(data.type === 'connected') {
socket.emit('connection', {
type : 'join',
name : name,
room : room
});
}
});
socket.on('system', function(data) {
writeMessage('system', 'system', data.message);
});
socket.on('message', function(data) {
writeMessage('other', data.name, data.message);
});
$('#message-button').click(function() {
var $input = $('#message-input');
var msg = $input.val();
sender(msg);
$input.val('');
$input.focus();
});
$('#message-input').on('keypress', function(e) {
if(e.keyCode === 13) {
var $input = $('#message-input');
var msg = $input.val();
sender(msg);
$input.val('');
$input.focus();
}
});
});
</script>
</body>
</html>