-
Notifications
You must be signed in to change notification settings - Fork 0
/
ack-index.html
111 lines (82 loc) · 3.28 KB
/
ack-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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo Chat</title>
<link href="bootstrap.css" rel="stylesheet">
<style>
body {
padding:20px;
}
#console {
height: 400px;
overflow: auto;
}
.username-msg {color:orange;}
.connect-msg {color:green;}
.disconnect-msg {color:red;}
.send-msg {color:#888}
</style>
<script src="js/socket.io/socket.io.js"></script>
<script src="js/moment.min.js"></script>
<script src="js/jquery-1.7.2.min.js"></script>
<script>
var userName = 'user' + Math.floor((Math.random()*1000)+1);
var socket = io.connect('http://localhost:9092');
socket.on('connect', function() {
output('<span class="connect-msg">Client has connected to the server!</span>');
});
socket.on('message', function(data, ackServerCallback) {
output('<span class="username-msg">' + data.userName + ':</span> ' + data.message);
if (ackServerCallback) {
// send ack data to server
ackServerCallback('server message was delivered to client!');
}
});
socket.on('disconnect', function() {
output('<span class="disconnect-msg">The client has disconnected!</span>');
});
socket.on('ackevent2', function(data, ackServerCallback, arg1) {
output('<span class="username-msg">' + data.userName + ':</span> ' + data.message);
if (ackServerCallback) {
ackServerCallback('server message was delivered to client!');
}
});
socket.on('ackevent3', function(data, ackServerCallback) {
output('<span class="username-msg">' + data.userName + ':</span> ' + data.message);
if (ackServerCallback) {
ackServerCallback();
}
});
function sendDisconnect() {
socket.disconnect();
}
function sendMessage() {
var message = $('#msg').val();
$('#msg').val('');
var jsonObject = {'@class': 'com.corundumstudio.socketio.demo.ChatObject',
userName: userName,
message: message};
socket.emit('ackevent1', jsonObject, function(arg1, arg2) {
alert("ack from server! arg1: " + arg1 + ", arg2: " + arg2);
});
}
function output(message) {
var currentTime = "<span class='time'>" + moment().format('HH:mm:ss.SSS') + "</span>";
var element = $("<div>" + currentTime + " " + message + "</div>");
$('#console').prepend(element);
}
</script>
</head>
<body>
<h1>Netty-socketio Demo Chat</h1>
<br/>
<div id="console" class="well">
</div>
<form class="well form-inline" onsubmit="return false;">
<input id="msg" class="input-xlarge" type="text" placeholder="Type something..."/>
<button type="button" onClick="sendMessage()" class="btn">Send</button>
<button type="button" onClick="sendDisconnect()" class="btn">Disconnect</button>
</form>
</body>
</html>