-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.js
129 lines (104 loc) · 2.88 KB
/
poll.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
"use strict";
function Channel(chat) {
this.chat = chat;
this.pollUIDs = {};
this.pollRequest = this.createRequest();
this.pollRequest.onreadystatechange = this.receivePoll.bind(this);
this.nextPoll();
this.pushRequest = this.createRequest();
this.pushRequest.onreadystatechange = this.receivePush.bind(this);
this.pushLeft = [];
this.pushing = false;
this.messageRequest = this.createRequest();
this.messageRequest.onreadystatechange = this.receiveMessage.bind(this);
this.messageLeft = [];
this.messaging = false;
this.onMessageScan = null;
this.onMessageReceive = null;
}
Channel.prototype.createRequest = function() {
return new XMLHttpRequest();
}
Channel.prototype.receivePoll = function() {
if (this.pollRequest.readyState!=4) {
return false;
}
if (this.pollRequest.status!=200) {
this.nextPoll();
return false;
}
let json = JSON.parse(this.pollRequest.responseText);
if (json["uids"]!==undefined && Object.keys(json["uids"]).length>0) {
this.pollUIDs = json["uids"];
}
if (json["messages"]!==undefined) {
for (let v in json["messages"]) {
let id = json["messages"][v]["id"];
this.onMessageScan(id);
}
}
this.nextPoll();
}
Channel.prototype.nextPoll = function() {
let json = {"node":this.chat, "chat":this.chat, "uids":this.pollUIDs};
this.pollRequest.open("PUT", "poll.php", true);
this.pollRequest.send(JSON.stringify(json));
}
Channel.prototype.push = function(data) {
this.pushLeft.push(data);
this.nextPush();
}
Channel.prototype.receivePush = function() {
if (this.pushRequest.readyState!=4) {
return false;
}
if (this.pushRequest.status!=200) {
this.nextPush();
return false;
}
this.pushLeft.shift();
this.pushing = false;
this.nextPush();
}
Channel.prototype.nextPush = function() {
if (this.pushLeft.length==0) {
return;
}
if (this.pushing) {
return;
}
let json = {"node":"0", "chat":this.chat, "data":this.pushLeft[0]};
this.pushRequest.open("PUT", "push.php", true);
this.pushRequest.send(JSON.stringify(json));
this.pushing = true;
}
Channel.prototype.message = function(node) {
this.messageLeft.push(node);
this.nextMessage();
}
Channel.prototype.receiveMessage = function() {
if (this.messageRequest.readyState!=4) {
return false;
}
if (this.messageRequest.status!=200) {
this.nextMessage();
return false;
}
let json = JSON.parse(this.messageRequest.responseText);
this.onMessageReceive(json["node"], json["data"]);
this.messageLeft.shift();
this.messaging = false;
this.nextMessage();
}
Channel.prototype.nextMessage = function() {
if (this.messageLeft.length==0) {
return;
}
if (this.messaging) {
return;
}
let json = {"node":this.messageLeft[0], "chat":this.chat};
this.messageRequest.open("PUT", "message.php", true);
this.messageRequest.send(JSON.stringify(json));
this.messaging = true;
}