-
-
Notifications
You must be signed in to change notification settings - Fork 339
/
index.html
131 lines (116 loc) · 4.58 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
<!doctype html>
<html>
<head>
<script type="text/javascript" src="javascript/jquery-2.0.3.js"></script>
<script type="text/javascript" src="javascript/atmosphere.js"></script>
<script type="text/javascript" src="javascript/jquery.list.js"></script>
<script type="text/javascript">
$(function() {
var socket = atmosphere;
var subSocket;
function getKeyCode(ev) {
if (window.event) return window.event.keyCode;
return ev.keyCode;
}
function getElementById(id) {
return document.getElementById(id);
}
function getTransport(t) {
transport = t.options[t.selectedIndex].value;
if (transport == 'autodetect') {
transport = 'websocket';
}
return false;
}
function getElementByIdValue(id) {
return document.getElementById(id).value;
}
function subscribe() {
var request = { url : document.location.toString() + 'search/' + getElementByIdValue('topic'),
transport: getElementByIdValue('transport'),
trackMessageLength : true
};
request.onMessage = function (response) {
detectedTransport = response.transport;
if (response.status == 200) {
var data = response.responseBody;
try {
var result = JSON.parse(data);
var i = 0;
for (i = result.statuses.length - 1; i > -1; i--) {
$('#result').prepend($('<li>').append($('<a>').append(result.statuses[i].text))).highlight(getElementById('topic').value);
$('#result').prepend($('<li>').append($('<h3>').append(result.statuses[i].from_user).append('</h3>')));
}
} catch (err) {
alert("Exception: " + err)
}
}
};
subSocket = socket.subscribe(request);
}
function connect() {
subscribe();
getElementById('connect').value = "Switch transport";
}
function stop() {
var request = { url : document.location.toString() + 'search/' + getElementByIdValue('topic') + "/stop" }
// REVISIT this is not working
socket.publish(request);
// if we can push it this way for websocket but this cannot work either
// as it won't find the target operation "stop"
// subSocket.push(JSON.stringify(request));
}
getElementById('connect').onclick = function(event) {
if (getElementById('topic').value == '') {
alert("Please enter a hashtag");
return;
}
connect();
}
getElementById('stop').onclick = function(event) {
if (getElementById('topic').value == '') {
alert("Please enter a hashtag");
return;
}
stop();
}
getElementById('topic').onkeyup = function(event) {
var keyc = getKeyCode(event);
if (keyc == 13 || keyc == 10) {
connect();
return false;
}
}
getElementById('topic').focus();
});
</script>
<style type='text/css'>
body {text-align: center;}
div {border: 0px solid black;}
.highlight {background-color: yellow}
input#topic {width: 20em; height: 3em; font-size: 100%; background-color: #e0f0f0;}
ul {list-style-type: none; text-align: left;}
</style>
</head>
<body>
<h1>Twitter Search using Atmosphere's JQuery Plug In</h1>
<h2>Enter an hashtag</h2>
<div id='pubsub'>
<input id='topic' type='text'/>
</div>
<h2>Select transport to use for subscribing</h2>
<h3>You can change the transport any time.</h3>
<div id='select_transport'>
<select id="transport">
<option id="autodetect" value="websocket">autodetect</option>
<option id="long-polling" value="long-polling">long-polling</option>
<option id="streaming" value="streaming">http streaming</option>
<option id="websocket" value="websocket">websocket</option>
</select>
<input id='connect' class='button' type='submit' name='connect' value='Get Real Time Tweet'/>
<input id='stop' class='button' type='submit' name='stop' value='Stop Real Time Tweet'/>
</div>
<h2>Real Time Twitter Search</h2>
<ul id="result"></ul>
</body>
</html>