This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket.js
103 lines (96 loc) · 2.74 KB
/
socket.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
var sockets = {};
var channel = {};
sockets.getTokenRequest = function() {
windows.connecting();
console.log("connecting for token...");
config.load();
var pastFrame = chrome.extension.getBackgroundPage().document.getElementById('wcs-iframe');
if(pastFrame)
pastFrame.parentNode.removeChild(pastFrame);
auth.request(config.host + 'channels/get/' + config.identifier, sockets.getTokenResult, sockets.getTokenResult);
console.log("Sent token request");
}
sockets.getTokenResult = function(resp, xhr) {
channel = {};
console.log(resp);
try {
result = JSON.parse(resp);
} catch(err) {
result = {};
result.code = 600;
result.err = err;
}
if(result.code === 200 || result.code === 304) {
//handle a successful retrieval of a token
//200 means a token was created
//304 means a token was already cached
if(config.debug) {
console.log("Channel token result:");
console.log(result);
}
channel.token = result.token;
sockets.connect();
} else if(result.code === 503) {
windows.overQuota();
} else {
channel.error = "Error " + result.code +": ";
channel.error += JSON.stringify(result.err);
}
}
sockets.connect = function() {
channel.channel = new goog.appengine.Channel(channel.token);
channel.socket = channel.channel.open();
channel.socket.onopen = sockets.onOpen;
channel.socket.onmessage = sockets.onMessage;
channel.socket.onerror = sockets.onError;
channel.socket.onclose = sockets.onDisconnect;
}
sockets.onOpen = function() {
windows.connected();
window.setTimeout(
function() {
auth.request(config.host + 'channels/connected/' + config.identifier, function(resp, xhr) { }, {'method' : 'POST'});
}, 100);
}
sockets.onMessage = function(evt) {
if(config.debug)
console.log(evt)
var message = JSON.parse(evt.data);
if(config.debug)
console.log(message);
if(message.links) {
linksToSend = Array();
for(link in message.links) {
windows.openLink(message.links[link]);
linksToSend.push(message.links[link].id);
}
auth.request(config.host + 'links/mark_read', function(resp, xhr) {
if(config.debug) {
console.log("Marked link as read:");
console.log(evt.data);
console.log("Response: " + resp);
}
}, {
'method': 'POST',
'parameters': {
'links': JSON.stringify(linksToSend)
}
}
);
}
//TODO: Handle other messages
}
sockets.onError = function(error) {
console.log(error);
if(error.code != 401) {
channel.error = "Error " + error.code +": ";
channel.error += error.description;
windows.serverDown();
console.log(error);
} else {
sockets.getTokenRequest();
}
}
sockets.onDisconnect = function() {
windows.disconnected();
}