-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.js
159 lines (159 loc) · 6.02 KB
/
sockets.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
/* jshint esversion: 6 */
// socket_send = set_socket('all_share_data', 'socket', function(msg) { console.log(msg); });
// socket_send('momoca',null,{})
const set_socket = function(namespace, socket_var, ok, err) {
err = err || function(msg) {};
if (!namespace) return;
if (!socket_var) return;
var cache = (key, value = undefined, time = 0) => {
let res = {
key,
value,
time
},
_t = ((new Date()).getTime());
time = parseInt(time);
if (isNaN(time)) return error_log(res, 'time error');
time = time * 1000;
if (key === undefined) return error_log(res, 'key undefined');
let storage = window.localStorage;
if (key === null) return storage.clear();
if (typeof(key) !== 'string') return error_log(res, 'key not string');
if (storage === null) return error_log(res, 'Storage not create');
if (value === null) {
storage.removeItem(key);
} else {
if (value === undefined) {
value = null;
obj = storage.getItem(key);
if (obj) {
obj = JSON.parse(obj);
if ((obj.t === 0) || (obj.t > 0 && obj.t > _t)) {
value = obj.value;
}
}
return value;
} else {
let t = (time == 0 ? 0 : _t + time);
storage.setItem(key, JSON.stringify({
t,
value
}));
return value;
}
}
},
o = function(obj) {
var current = obj || {};
return {
merge: function(obj1, obj2) {
if (obj1.constructor !== Object && obj2.constructor !== Object) return obj2;
for (var p in obj2) {
try {
if (obj1[p].constructor == Object && obj2[p].constructor == Object) {
obj1[p] = this.merge(obj1[p], obj2[p]);
} else {
obj1[p] = obj2[p];
}
} catch (e) {
obj1[p] = obj2[p];
}
}
return obj1;
},
path: function(_path, _value, _obj) {
var i;
if (!_obj) {
_obj = current;
}
if (typeof _path === 'string') {
return this.path(_path.split('.'), _value);
}
if (_value === undefined) {
//get
for (i = 0; i < _path.length; ++i) {
if (current[_path[i]] === undefined) {
return undefined;
} else {
current = current[_path[i]];
}
}
return current;
} else if (_value === null) {
//del
if (_path.length === 1) {
delete(_obj[_path[0]]);
return _obj;
}
if (!_obj[_path[0]]) return _obj;
_obj[_path[0]] = this.path(_path.slice(1), _value, _obj[_path[0]]);
} else {
//set
if (_path === null) {
return _value;
}
if (_path.length === 1) {
_obj[_path[0]] = _value;
return _obj;
}
if (!_obj[_path[0]] || !(_obj[_path[0]] instanceof Object)) _obj[_path[0]] = {};
_obj[_path[0]] = this.path(_path.slice(1), _value, _obj[_path[0]]);
}
return _obj;
}
};
};
socket_var = namespace + '_' + socket_var;
let host = "wss://api.v2.ctx.org.cn/mwss/" + namespace;
if (window[socket_var]) window[socket_var].close();
if (window[socket_var + 'Interval']) clearInterval(window[socket_var + 'Interval']);
window[socket_var] = new WebSocket(host);
window[socket_var].onopen = function(msg) {
window[socket_var + 'Interval'] = setInterval(function() {
window[socket_var].send('ping');
}, 1000 * 60 * 20);
};
window[socket_var].onmessage = function(msg) {
// {
// key:xxx
// lasttime:xxx
// data:
// }
if (msg.data === 'ping') return;
let data = JSON.parse(decodeURIComponent(msg.data));
let c_data = cache(data.key);
if (!c_data) {
//create
cache(data.key, data);
if (ok) ok(data);
return;
} else {
//update
if (c_data.lasttime <= data.lasttime) {
data = o().merge(c_data, data);
tmp = data;
} else {
tmp = c_data;
}
cache(data.key, tmp);
if (ok) ok(tmp);
return;
}
return;
};
window[socket_var].onclose = err;
return function(key, path, value) {
let data = cache(key);
data = data || {};
_val = o(data.data).path(path);
if (_val !== undefined) value = o().merge(_val, value);
data = o(data.data).path(path, value);
let msg = {
key: key,
lasttime: (new Date()).getTime(),
data: data,
};
cache(msg.key, msg);
window[socket_var].send(encodeURIComponent(JSON.stringify(msg)));
};
};