-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatclient.html
75 lines (58 loc) · 1.99 KB
/
chatclient.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
<!doctype html>
<html>
<head>
<title>Socket.io Chat</title>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<script>
var socket;
var firstconnect = true;
function connect()
{
if(firstconnect) {
socket = io.connect(null);
socket.on('connect', function(){ $('#status').html('Connected to Server'); });
socket.on('disconnect', function(){ $('#status').html('Disconnected from Server') });
socket.on('reconnecting', function( nextRetry ){ $('#status').html('Reconnecting in -'+ nextRetry + ' milliseconds') });
socket.on('reconnect_failed', function(){ $('#status').html('Reconnect Failed')});
//callback for the chat event
socket.on('chat', function(client, message){
$('#messages').append('<b>Client '+ message.username + ' says: </b>' + message.message + '</br>');
});
//callback for username event
socket.on('username', function(client, username){
$('#username').val(username).attr("disabled",true);
$('#btn_createUserName').hide();
});
firstconnect = false;
}else{
socket.socket.reconnect();
}
}
function disconnect(){
socket.disconnect();
}
function send(){
socket.emit('message',{ "message": $('#message').val(), "username": $('#username').val() } );
}
function createUserName()
{
socket.emit('username', $('#username').val());
}
</script>
<h1>Socket.io Chat</h1>
<div><p id='status'>Waiting for input</p></div>
<h3>Username</h3>
<input id='username'></input>
<button id='btn_createUserName' onClick='createUserName()' />Create </button><br />
<button id='connect' onClick='connect()' /> Connect </button>
<button id='disconnect' onClick='disconnect()' />Disconnect</button><br />
<input id='message'></input>
<button id='send' onClick='send()' />Send Message</button>
<br />
<h1>Messages:</h1>
<div id="messages"></div>
</body>
</html>