/
hs_notification.js
233 lines (191 loc) · 5.88 KB
/
hs_notification.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var HS = HS || {};
HS.MessageQueue = [];
/**
* websocketによるシステムからのノティフィケーション
*
* @namespace HS
* @class notification
*/
HS.Notification = function () { 'use strict'; this.init(); };
HS.Notification.prototype = (function () {
'use strict';
/* private */
var uri = "ws://" + location.host + "/websock/notification";
var websock = undefined;
var not_support_msg = "プッシュ通知サービスはサポートされません";
var websock_shoutdown_msg = "(;・ิω・ิ)サーバとの通信が途切れました";
var reconnect_interval = 5000;
var keep_alive_interval = 60000;
var keep_alive_timer = null;
var open_connection = function(mode) {
var self = this;
if ("WebSocket" in window) {
websock = new WebSocket(uri);
} else if ("MozWebSocket" in window) {
websock = new MozWebSocket(uri);
}
if (websock) { // browser supports websockets
websock.onopen = handle_open;
websock.onmessage = handle_event;
websock.onclose = handle_close;
} else { // browser does not support websockets
debug_message(self.not_support_msg);
if (mode == "retry") {
setTimeout(function() { open_connection("retry"); },
reconnect_interval);
}
}
};
var debug_message = function(msg) { /* alert(msg); */ };
/**
* サーバとの接続がオープンしたら呼ばれる
*
* @method handle_close
*/
var handle_open = function() {
if (keep_alive_timer != null) {
clearTimeout(keep_alive_timer);
keep_alive_timer = null;
}
keep_alive_timer =
setTimeout(function() { keep_alive(); }, keep_alive_interval);
};
/**
* サーバとの接続が遮断されたら呼ばれる
*
* @method handle_close
*/
var handle_close = function() {
debug_message(websock_shoutdown_msg);
if (keep_alive_timer != null) {
clearTimeout(keep_alive_timer);
keep_alive_timer = null;
}
setTimeout(function() { open_connection("retry"); },
reconnect_interval);
};
/**
* サーバからデータが送信されたら呼ばれる
*
* @method handle_event
*/
var handle_event = function(evt) {
var msg_data = evt.data; debug_message(msg_data);
var msg = JSON.parse(msg_data);
HS.MessageQueue.push(msg);
if (msg.type == "keep_alive") { handle_keep_alive(msg); }
if (msg.type == "message") { handle_message(msg); }
};
/**
* ユーザへののティフィケーションメッセージを受け取った時の処理
*
* @method handle_message
*/
var notification_change_flag = false;
var notification_title_change_timer = null;
var handle_message = function(msg) {
var view = create_notification_view(msg.message);
$("div.notification-message").remove();
$("div#header").append(view);
$(view).fadeIn("normal");
new HS.TeamViewController().set_messages_to_timeline([msg.message]);
setTimeout(function() {
$(view).animate({opacity:0.5}, 2000);
}, 10000)
var original_title = $("title").text();
/* 他のユーザのメッセージ投稿があった場合にウインドウのタイトルを反転させる */
if (notification_title_change_timer == undefined) {
notification_title_change_timer = setInterval(function() {
if (true == notification_change_flag) {
$("title").text("新規メッセージがあります");
notification_change_flag = false;
} else {
$("title").text(original_title);
notification_change_flag = true;
}
}, 1000);
var stop_fun = function() {
clearInterval(notification_title_change_timer);
$("title").text(original_title);
notification_title_change_timer = undefined;
window.onfocus = undefined;
document.onclick = undefined;
};
window.onfocus = stop_fun;
document.onclick = stop_fun;
};
};
/**
* 一定時間通信しないと接続遮断するルータ対策に時々通信しておく
*
* @method keep_alive
*/
var keep_alive = function() {
if (websock) {
websock.send("TICK");
keep_alive_timer =
setTimeout(function() { keep_alive(); }, keep_alive_interval);
}
};
/**
* keep_aliveでデータ送信した応答確認用関数。今はなにもしない
*
* @method handle_keep_alive
*/
var handle_keep_alive = function(msg) {
}
/**
* メッセージ要素のビューを作って返す
*
* @method create_notification_view
*/
var max_message_len = 30;
var create_notification_view = function(message) {
var div = document.createElement("div");
div.className = "notification-message";
var img = document.createElement("img");
img.src = message.usr.icon_url;
img.alt = message.usr.name;
var sub_text = message.text;
if (sub_text.length > max_message_len) {
sub_text = sub_text.substr(0, max_message_len - 2) + "...";
}
var p = document.createElement("p");
var text = document.createTextNode(sub_text);
p.appendChild(text);
var clear_div1 = document.createElement("div");
clear_div1.style.cssText = "clear:both;";
var name_h3 = document.createElement("h3");
var name_text = document.createTextNode(message.usr.name);
name_h3.appendChild(name_text);
var time_p = document.createElement("p");
time_p.className = "tweet_time";
var time_text = document.createTextNode(message.created_at);
time_p.appendChild(time_text);
var reply_a = document.createElement("a");
reply_a.href = "#";
var clear_div2 = document.createElement("div");
clear_div2.style.cssText = "clear:both;";
div.appendChild(img);
div.appendChild(p);
div.appendChild(clear_div1);
div.appendChild(name_h3);
div.appendChild(time_p);
div.appendChild(reply_a);
div.appendChild(clear_div2);
return div;
};
/* public */
return {
/**
* 初期化
*
* @method init
*/
init: function () {
},
start: function() {
open_connection();
}
};
}());