forked from amberframework/amber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamber.js
278 lines (247 loc) · 7.1 KB
/
amber.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const EVENTS = {
join: 'join',
leave: 'leave',
message: 'message'
}
const STALE_CONNECTION_THRESHOLD_SECONDS = 100
const SOCKET_POLLING_RATE = 10000
/**
* Returns a numeric value for the current time
*/
let now = () => {
return new Date().getTime()
}
/**
* Returns the difference between the current time and passed `time` in seconds
* @param {Number|Date} time - A numeric time or date object
*/
let secondsSince = (time) => {
return (now() - time) / 1000
}
/**
* Class for channel related functions (joining, leaving, subscribing and sending messages)
*/
export class Channel {
/**
* @param {String} topic - topic to subscribe to
* @param {Socket} socket - A Socket instance
*/
constructor(topic, socket) {
this.topic = topic
this.socket = socket
this.onMessageHandlers = []
}
/**
* Join a channel, subscribe to all channels messages
*/
join() {
this.socket.ws.send(JSON.stringify({ event: EVENTS.join, topic: this.topic }))
}
/**
* Leave a channel, stop subscribing to channel messages
*/
leave() {
this.socket.ws.send(JSON.stringify({ event: EVENTS.leave, topic: this.topic }))
}
/**
* Calls all message handlers with a matching subject
*/
handleMessage(msg) {
this.onMessageHandlers.forEach((handler) => {
if (handler.subject === msg.subject) handler.callback(msg.payload)
})
}
/**
* Subscribe to a channel subject
* @param {String} subject - subject to listen for: `msg:new`
* @param {function} callback - callback function when a new message arrives
*/
on(subject, callback) {
this.onMessageHandlers.push({ subject: subject, callback: callback })
}
/**
* Send a new message to the channel
* @param {String} subject - subject to send message to: `msg:new`
* @param {Object} payload - payload object: `{message: 'hello'}`
*/
push(subject, payload) {
this.socket.ws.send(JSON.stringify({ event: EVENTS.message, topic: this.topic, subject: subject, payload: payload }))
}
}
/**
* Class for maintaining connection with server and maintaining channels list
*/
export class Socket {
/**
* @param {String} endpoint - Websocket endpont used in routes.cr file
*/
constructor(endpoint) {
this.endpoint = endpoint
this.ws = null
this.channels = []
this.lastPing = now()
this.reconnectTries = 0
this.attemptReconnect = true
}
/**
* Returns whether or not the last received ping has been past the threshold
*/
_connectionIsStale() {
return secondsSince(this.lastPing) > STALE_CONNECTION_THRESHOLD_SECONDS
}
/**
* Tries to reconnect to the websocket server using a recursive timeout
*/
_reconnect() {
clearTimeout(this.reconnectTimeout)
this.reconnectTimeout = setTimeout(() => {
this.reconnectTries++
this.connect(this.params)
this._reconnect()
}, this._reconnectInterval())
}
/**
* Returns an incrementing timeout interval based around the number of reconnection retries
*/
_reconnectInterval() {
return [1000, 2000, 5000, 10000][this.reconnectTries] || 10000
}
/**
* Sets a recursive timeout to check if the connection is stale
*/
_poll() {
this.pollingTimeout = setTimeout(() => {
if (this._connectionIsStale()) {
this._reconnect()
} else {
this._poll()
}
}, SOCKET_POLLING_RATE)
}
/**
* Clear polling timeout and start polling
*/
_startPolling() {
clearTimeout(this.pollingTimeout)
this._poll()
}
/**
* Sets `lastPing` to the curent time
*/
_handlePing() {
this.lastPing = now()
}
/**
* Clears reconnect timeout, resets variables an starts polling
*/
_reset() {
clearTimeout(this.reconnectTimeout)
this.reconnectTries = 0
this.attemptReconnect = true
this._startPolling()
}
/**
* Connect the socket to the server, and binds to native ws functions
* @param {Object} params - Optional parameters
* @param {String} params.location - Hostname to connect to, defaults to `window.location.hostname`
* @param {String} parmas.port - Port to connect to, defaults to `window.location.port`
* @param {String} params.protocol - Protocol to use, either 'wss' or 'ws'
*/
connect(params) {
this.params = params
let opts = {
location: window.location.hostname,
port: window.location.port,
protocol: window.location.protocol === 'https:' ? 'wss:' : 'ws:',
}
if (params) Object.assign(opts, params)
if (opts.port) opts.location += `:${opts.port}`
return new Promise((resolve, reject) => {
this.ws = new WebSocket(`${opts.protocol}//${opts.location}${this.endpoint}`)
this.ws.onmessage = (msg) => { this.handleMessage(msg) }
this.ws.onclose = () => {
if (this.attemptReconnect) this._reconnect()
}
this.ws.onopen = () => {
this._reset()
resolve()
}
})
}
/**
* Closes the socket connection permanently
*/
disconnect() {
this.attemptReconnect = false
clearTimeout(this.pollingTimeout)
clearTimeout(this.reconnectTimeout)
this.ws.close()
}
/**
* Adds a new channel to the socket channels list
* @param {String} topic - Topic for the channel: `chat_room:123`
*/
channel(topic) {
let channel = new Channel(topic, this)
this.channels.push(channel)
return channel
}
/**
* Message handler for messages received
* @param {MessageEvent} msg - Message received from ws
*/
handleMessage(msg) {
if (msg.data === "ping") return this._handlePing()
let parsed_msg = JSON.parse(msg.data)
this.channels.forEach((channel) => {
if (channel.topic === parsed_msg.topic) channel.handleMessage(parsed_msg)
})
}
}
module.exports = {
Socket: Socket
}
/**
* Allows delete links to post for security and ease of use similar to Rails jquery_ujs
*/
document.addEventListener("DOMContentLoaded", function () {
var elements = document.querySelectorAll("a[data-method='delete']");
var i;
for (i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", function (e) {
e.preventDefault();
var message = e.target.getAttribute("data-confirm") || "Are you sure?";
if (confirm(message)) {
var form = document.createElement("form");
var input = document.createElement("input");
form.setAttribute("action", e.target.getAttribute("href"));
form.setAttribute("method", "POST");
input.setAttribute("type", "hidden");
input.setAttribute("name", "_method");
input.setAttribute("value", "DELETE");
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
return false;
})
}
});
if (!Date.prototype.toGranite) {
(function() {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
Date.prototype.toGranite = function() {
return this.getUTCFullYear() +
'-' + pad(this.getUTCMonth() + 1) +
'-' + pad(this.getUTCDate()) +
' ' + pad(this.getUTCHours()) +
':' + pad(this.getUTCMinutes()) +
':' + pad(this.getUTCSeconds()) ;
};
}());
}