李勇

1.增加获取本地server.json文件,获取mcu 和ms列表;

2.开启mcu ms列表选择功能,优先从server.JSON文件中获取,没有获取到的时候再使用Sass中的
1 -import Emiter from 'src/Emiter';  
2 -import Loger from 'src/Loger';  
3 -import MessageTypes from 'src/MessageTypes';  
4 -import GlobalConfig from 'src/GlobalConfig';  
5 -import MD5 from "md5";  
6 -import ApeConsts from 'src/apes/ApeConsts';  
7 -import iphunter from 'iphunter';  
8 -import Server from "src/config/Server";  
9 -import fetchJsonp from 'fetch-jsonp';  
10 -  
11 -// 日志对象  
12 -const loger = Loger.getLoger('ServerCheck');  
13 -  
14 -//ip选点流程的状态  
15 -let isRequestMcuCallback = false;//是否获取最佳mcu返回  
16 -let isRequestMsCallback = false;//是否获取ms最佳返回  
17 -let isTestFromSass = false;  
18 -let isTestFromServer = false;  
19 -  
20 -let tempMcuIp = "";  
21 -let tempMcuPort = "";  
22 -let tempMsIp = "";  
23 -let tempMsPort = "";  
24 -let msDefaultPort = "1935";  
25 -let mcuDefaultPort = "7777";  
26 -let speedTestPort = ':5555';//测速端口统一  
27 -  
28 -let checkMcuIpGroup = [];//储存MCU需要查询的ip数组  
29 -let checkMsIpGroup = [];//储存MCU需要查询的ip数组  
30 -const timeOutDelay = 1000;//选点超时  
31 -  
32 -class ServerCheck extends Emiter {  
33 - constructor() {  
34 - super();  
35 -  
36 - }  
37 -  
38 - //根据userIp获取ip相关的信息,参数是userIp  
39 - getUserIpInfo(token, userIp) {  
40 - //重置ip选点流程状态  
41 - isRequestMcuCallback = false;  
42 - isRequestMsCallback = false;  
43 - isTestFromSass = false;  
44 - isTestFromServer = false;  
45 - checkMcuIpGroup = [];  
46 - checkMsIpGroup = [];  
47 - let userIpInfo = new Object;  
48 - userIpInfo.ret = -1;  
49 -  
50 - let ip = userIp;  
51 - let md5Str = MD5("addr=" + ip + "&token=b657c3507b324353e09c1958ee956a98efceb3e3");  
52 - // /("addr=" + ip + "&token=b657c3507b324353e09c1958ee956a98efceb3e3"),转成MD5  
53 - let timestamp = new Date().getTime();  
54 - let location = `http://ipapi.ipip.net/find?addr=${ip}&sid=14&uid=5237&sig=${md5Str}&_=${timestamp}`;  
55 -  
56 - fetchJsonp(location, {  
57 - timeout: timeOutDelay,  
58 - }).then(function (response) {  
59 - return response.json()  
60 - }).then(function (json) {  
61 - // loger.log('获取IP信息返回', json)  
62 - if (json) {  
63 - userIpInfo.ret = json.ret;  
64 - userIpInfo.country = json.data[0];//国家  
65 - userIpInfo.province = json.data[1];//省份  
66 - userIpInfo.city = json.data[2];//城市  
67 - userIpInfo.isp = json.data[4];//运营商  
68 - }  
69 - this.serverGetUserIpInfoCallback(userIpInfo);  
70 - }.bind(this)).catch(function (ex) {  
71 - // loger.log('获取IP信息失败', ex.message)  
72 - this.serverGetUserIpInfoCallback(userIpInfo);  
73 - }.bind(this));  
74 - }  
75 -  
76 - //获取ip信息返回  
77 - serverGetUserIpInfoCallback(userIpInfo) {  
78 - // loger.log("获取IP详情,开始处理", userIpInfo);  
79 - if (userIpInfo.ret == "ok") {  
80 - GlobalConfig.country = userIpInfo.country;//国家  
81 - GlobalConfig.city = userIpInfo.city;//城市  
82 - GlobalConfig.province = userIpInfo.province;//服务商  
83 - GlobalConfig.isp = userIpInfo.isp;//服务商  
84 - // loger.log("获取ip详情成功,country:" + GlobalConfig.country + ",city:" + GlobalConfig.city + ",isp:" + GlobalConfig.isp);  
85 - this._chooseBestIpFromServer();  
86 - }  
87 - else {  
88 - // loger.log("获取ip详情失败");  
89 - this._chooseBestIpFromSassParam();  
90 - }  
91 - }  
92 -  
93 - //从IPIP服务列表中选择最快的IP  
94 - _chooseBestIpFromServer() {  
95 - // loger.log("从Server服务列表中选择最快的IP");  
96 - isRequestMcuCallback = false;  
97 - isRequestMsCallback = false;  
98 - isTestFromServer = true;  
99 - isTestFromSass = false;  
100 -  
101 - //country, province, ctiy, isp, jsona  
102 - checkMcuIpGroup = this._returnServerMCU(  
103 - GlobalConfig.country,  
104 - GlobalConfig.province,  
105 - GlobalConfig.city,  
106 - GlobalConfig.isp,  
107 - Server.serverList);  
108 -  
109 - checkMsIpGroup = this._returnServerMS(  
110 - GlobalConfig.country,  
111 - GlobalConfig.province,  
112 - GlobalConfig.city,  
113 - GlobalConfig.isp,  
114 - Server.serverList);  
115 -  
116 - let mcuArr = [];  
117 - let msArr = [];  
118 - for (let i in checkMcuIpGroup) {  
119 - if (checkMcuIpGroup[i]) {  
120 - mcuArr.push(checkMcuIpGroup[i].ip + speedTestPort);  
121 - }  
122 - }  
123 - for (let k in checkMsIpGroup) {  
124 - if (checkMsIpGroup[k]) {  
125 - msArr.push(checkMsIpGroup[k].ip + speedTestPort);  
126 - }  
127 - }  
128 - this.getBestMcuServer(mcuArr);  
129 - this.getBestMsServer(msArr);  
130 - }  
131 -  
132 - //从Sass返回的msList mcuList中选点  
133 - _chooseBestIpFromSassParam() {  
134 - // loger.log("从Sass服务列表中选择最快的IP");  
135 - isRequestMcuCallback = false;  
136 - isRequestMsCallback = false;  
137 - isTestFromSass = false;  
138 - isTestFromServer = false;  
139 - checkMcuIpGroup = GlobalConfig.mcuList;  
140 - checkMsIpGroup = GlobalConfig.msList;  
141 - //MCU  
142 - let mcuIpGroup = [];  
143 - for (let i = 0; i < checkMcuIpGroup.length; i++) {  
144 - let ipPort = checkMcuIpGroup[i].ip + speedTestPort;  
145 - mcuIpGroup.push(ipPort)  
146 - }  
147 - this.getBestMcuServer(mcuIpGroup);  
148 -  
149 - //MS  
150 - let msIpGroup = [];  
151 - for (let k = 0; k < checkMsIpGroup.length; k++) {  
152 - let ipPort = checkMsIpGroup[k].ip + speedTestPort;  
153 - msIpGroup.push(ipPort)  
154 - }  
155 - this.getBestMsServer(msIpGroup);  
156 - }  
157 -  
158 -  
159 - //获取最快的MCU服务器地址,参数是一个ip数组  
160 - getBestMcuServer(_param) {  
161 - // loger.log('开始MCU选点 ', _param);  
162 - if (_param == null || _param.length < 1) {  
163 - this._getBestMcuServerCallbackHandler("");  
164 - return;  
165 - }  
166 - iphunter(_param, function (fatest_ip_response) {  
167 - if (!fatest_ip_response) {  
168 - loger.warn('getBestMcuServer -> nothing!');  
169 - this._getBestMcuServerCallbackHandler("");  
170 - } else {  
171 - loger.log('getBestMcuServer done -> ', fatest_ip_response);  
172 - this._getBestMcuServerCallbackHandler(fatest_ip_response);  
173 - }  
174 - }.bind(this), timeOutDelay);  
175 - }  
176 -  
177 - //获取最快的MS服务器地址,参数是一个ip数组  
178 - getBestMsServer(_param) {  
179 - // loger.log('开始MS选点 ', _param);  
180 - if (_param == null || _param.length < 1) {  
181 - this._getBestMsServerCallbackHandler("")  
182 - return;  
183 - }  
184 - iphunter(_param, function (fatest_ip_response) {  
185 - if (!fatest_ip_response) {  
186 - loger.warn('getBestMsServer -> nothing!');  
187 - this._getBestMsServerCallbackHandler("");  
188 - } else {  
189 - loger.log('getBestMsServer done -> ', fatest_ip_response);  
190 - this._getBestMsServerCallbackHandler(fatest_ip_response);  
191 - }  
192 - }.bind(this), timeOutDelay);  
193 - }  
194 -  
195 - _getBestMcuServerCallbackHandler(_data) {  
196 - // loger.log("MCU选点返回1", _data);  
197 - if (isRequestMcuCallback) {  
198 - // loger.log("MCU选点,已经有返回");  
199 - return;  
200 - }  
201 - isRequestMcuCallback = true;  
202 - if (_data) {  
203 - let server = _data.split(":");  
204 - if (server[0]) {  
205 - tempMcuIp = server[0];  
206 - }  
207 - tempMcuPort = mcuDefaultPort;  
208 - for (let i = 0; i < checkMcuIpGroup.length; i++) {  
209 - if (tempMcuIp == checkMcuIpGroup[i].ip) {  
210 - tempMcuPort = checkMcuIpGroup[i].port || mcuDefaultPort;  
211 - break;  
212 - }  
213 - }  
214 - }  
215 - // loger.log("MCU选点返回2",tempMcuIp,tempMcuPort);  
216 - this._startConnectMcu();  
217 - }  
218 -  
219 - _getBestMsServerCallbackHandler(_data) {  
220 - // loger.log("MS选点返回1", _data);  
221 - if (isRequestMsCallback) {  
222 - // loger.log("_getBestMsServerCallbackHandler,已经有返回");  
223 - return;  
224 - }  
225 - isRequestMsCallback = true;  
226 - if (_data) {  
227 - let server = _data.split(":");  
228 - if (server[0]) {  
229 - tempMsIp = server[0];  
230 - }  
231 - tempMsPort = msDefaultPort;  
232 - for (let i = 0; i < checkMsIpGroup.length; i++) {  
233 - if (tempMsIp == checkMsIpGroup[i].ip) {  
234 - tempMsPort = checkMsIpGroup[i].port || msDefaultPort;  
235 - break;  
236 - }  
237 - }  
238 - }  
239 - // loger.log("MS选点返回2", tempMsIp,tempMsPort);  
240 - this._startConnectMcu();  
241 - }  
242 -  
243 - //ip选点结束,开始连接MCU  
244 - _startConnectMcu() {  
245 - if (isRequestMcuCallback && isRequestMsCallback) {  
246 - if (isTestFromServer && !isTestFromSass) {  
247 - //从Server服务列表中选点结束,如果没有选到合适的,从Sass的列表中获取  
248 - if (!tempMcuIp || !tempMsIp) {  
249 - this._chooseBestIpFromSassParam();  
250 - } else {  
251 - GlobalConfig.MCUServerIP = tempMcuIp;  
252 - GlobalConfig.MCUServerPort = tempMcuPort;  
253 - GlobalConfig.MS_PUBLISH_IP = tempMsIp;  
254 - GlobalConfig.MS_PUBLISH_PORT = tempMsPort;  
255 - // loger.log("Server选点完成", "mcu-->",GlobalConfig.MCUServerIP,GlobalConfig.MCUServerPort,"ms---->",GlobalConfig.MSServerIP,GlobalConfig.MSServerPort);  
256 - this._emit(ServerCheck.SEVER_CHECK_BEST_IP_SUCCESS);  
257 - }  
258 - } else {  
259 - //从Sass返回的服务列表中选点结束  
260 - if (tempMcuIp) {  
261 - GlobalConfig.MCUServerIP = tempMcuIp;  
262 - GlobalConfig.MCUServerPort = tempMcuPort;  
263 - }  
264 - if (tempMsIp) {  
265 - GlobalConfig.MS_PUBLISH_IP = tempMsIp;  
266 - GlobalConfig.MS_PUBLISH_PORT = tempMsPort;  
267 - }  
268 - loger.log("Sass选点完成", "mcu-->", GlobalConfig.MCUServerIP, GlobalConfig.MCUServerPort, "ms---->", GlobalConfig.MS_PUBLISH_IP, GlobalConfig.MS_PUBLISH_PORT);  
269 - this._emit(ServerCheck.SEVER_CHECK_BEST_IP_SUCCESS);  
270 - }  
271 - } else {  
272 - loger.warn("_startConnectMcu->正在选点", isRequestMcuCallback, isRequestMsCallback);  
273 - }  
274 - }  
275 -  
276 - //检测MCU连接地址  
277 - _returnServerMCU(country, province, ctiy, isp, jsona) {  
278 - let countryData = jsona.MCU[country];  
279 - //按country没有查找到就返回default  
280 - if (!countryData) {  
281 - countryData = jsona.MCU.default;  
282 - // loger.log("_returnServerMCU->countryData->default",countryData);  
283 - return countryData;  
284 - }  
285 -  
286 - if (country != "中国") {  
287 - // loger.log("_returnServerMCU->countryData",countryData);  
288 - return countryData;  
289 - }  
290 -  
291 - //中国的需要细分  
292 - //按isp查找  
293 - let ispData = countryData.isp[isp];  
294 - //isp查找到就返回  
295 - if (ispData) {  
296 - // loger.log("_returnServerMCU->ispData",ispData);  
297 - return ispData;  
298 - }  
299 -  
300 - //isp没查找到,用province  
301 - let provinceData = countryData.province[province];  
302 - //用province查找到就返回  
303 - if (provinceData) {  
304 - // loger.log("_returnServerMCU->provinceData",provinceData);  
305 - return provinceData;  
306 - }  
307 -  
308 -  
309 - //isp province都没有,使用default  
310 - let defaultData = countryData.default  
311 - if (defaultData) {  
312 - // loger.log("_returnServerMCU->defaultData",defaultData);  
313 - return defaultData;  
314 - } else {  
315 - // loger.log("_returnServerMCU->defaultData","");  
316 - return [];  
317 - }  
318 - return [];  
319 - }  
320 -  
321 - //检测MS连接地址  
322 - //Config.ipInfo  
323 - _returnServerMS(country, province, ctiy, isp, jsona) {  
324 - let countryData = jsona.MS[country];  
325 - //按country没有查找到就返回default  
326 - if (!countryData) {  
327 - countryData = jsona.MS.default;  
328 - // loger.log("_returnServerMS->countryData->default",countryData);  
329 - return countryData;  
330 - }  
331 -  
332 - if (country != "中国") {  
333 - // loger.log("_returnServerMS->countryData",countryData);  
334 - return countryData;  
335 - }  
336 -  
337 - //中国的需要细分  
338 - //按isp查找  
339 - let ispData = countryData.isp[isp];  
340 - //isp查找到就返回  
341 - if (ispData) {  
342 - // loger.log("_returnServerMS->ispData",ispData);  
343 - return ispData;  
344 - }  
345 -  
346 - //isp没查找到,用province  
347 - let provinceData = countryData.province[province];  
348 - //用province查找到就返回  
349 - if (provinceData) {  
350 - // loger.log("_returnServerMS->provinceData",provinceData);  
351 - return provinceData;  
352 - }  
353 -  
354 -  
355 - //isp province都没有,使用default  
356 - let defaultData = countryData.default  
357 - if (defaultData) {  
358 - // loger.log("_returnServerMS->defaultData",defaultData);  
359 - return defaultData;  
360 - } else {  
361 - // loger.log("_returnServerMS->defaultData","");  
362 - return [];  
363 - }  
364 -  
365 - return [];  
366 - }  
367 -}  
368 -  
369 -ServerCheck.prototype.SEVER_CHECK_BEST_IP_SUCCESS = ServerCheck.SEVER_CHECK_BEST_IP_SUCCESS = 'severCheck_checkBestIpSuccess_message';//获取最快的MS地址  
370 -export default new ServerCheck;  
371 -  
此 diff 太大无法显示。
@@ -32,7 +32,7 @@ import QuestionApe from 'apes/QuestionApe'; @@ -32,7 +32,7 @@ import QuestionApe from 'apes/QuestionApe';
32 import UTF8 from 'utf-8'; 32 import UTF8 from 'utf-8';
33 33
34 let loger = Loger.getLoger('McuClient'); 34 let loger = Loger.getLoger('McuClient');
35 -let _sdkInfo = {"version": "v1.33.1.20170614", "author": "www.3mang.com"}; 35 +let _sdkInfo = {"version": "v1.33.2.20170615", "author": "www.3mang.com"};
36 36
37 //APE 37 //APE
38 let _sass; 38 let _sass;
@@ -689,8 +689,11 @@ export default class MessageEntrance extends Emiter { @@ -689,8 +689,11 @@ export default class MessageEntrance extends Emiter {
689 689
690 //根据UserIp获取ip信息,选点 690 //根据UserIp获取ip信息,选点
691 getUserIpInfo() { 691 getUserIpInfo() {
  692 + let _this=this;
692 if (_ipManager) { 693 if (_ipManager) {
693 - _ipManager.getUserIpInfo("", GlobalConfig.userIp, this._getUserIpCallbackHandler.bind(this), 1000); 694 + _ipManager.loadServerJosn(function(_data){
  695 + _ipManager.getUserIpInfo("", GlobalConfig.userIp, _this._getUserIpCallbackHandler.bind(_this), 2000);
  696 + })
694 } 697 }
695 } 698 }
696 699
@@ -770,16 +773,41 @@ export default class MessageEntrance extends Emiter { @@ -770,16 +773,41 @@ export default class MessageEntrance extends Emiter {
770 Server.serverList); 773 Server.serverList);
771 } 774 }
772 775
773 - //console.log(" GlobalConfig.mcuListFromServer", GlobalConfig.mcuListFromServer)  
774 - //console.log(" GlobalConfig.msListFromServer", GlobalConfig.msListFromServer)  
775 - //合并所有MCU、MS 列表  
776 - GlobalConfig.mcuListAll.length = 0; 776 + console.log(" GlobalConfig.mcuListFromServer", GlobalConfig.mcuListFromServer);
  777 + console.log(" GlobalConfig.msListFromServer", GlobalConfig.msListFromServer);
  778 + console.log(" GlobalConfig.mcuList", GlobalConfig.mcuList);
  779 + console.log(" GlobalConfig.msList", GlobalConfig.msList);
  780 +
  781 +
  782 + //********合并所有MCU、MS 列表********
  783 + //****server.json中的数据和从Sass获取的msList mcuList 数据需要分开,不能同时共存****
  784 + /*GlobalConfig.mcuListAll.length = 0;
777 GlobalConfig.mcuListAll = GlobalConfig.mcuListAll.concat(GlobalConfig.mcuList, GlobalConfig.mcuListFromServer); 785 GlobalConfig.mcuListAll = GlobalConfig.mcuListAll.concat(GlobalConfig.mcuList, GlobalConfig.mcuListFromServer);
778 GlobalConfig.msListAll.length = 0; 786 GlobalConfig.msListAll.length = 0;
779 - GlobalConfig.msListAll = GlobalConfig.msListAll.concat(GlobalConfig.msList, GlobalConfig.msListFromServer); 787 + GlobalConfig.msListAll = GlobalConfig.msListAll.concat(GlobalConfig.msList, GlobalConfig.msListFromServer);*/
  788 +
  789 + GlobalConfig.mcuListAll.length = 0;
  790 + if(GlobalConfig.mcuListFromServer.length>0){
  791 + //使用server中的MCU数据
  792 + loger.warn("使用server中的MCU数据")
  793 + GlobalConfig.mcuListAll = GlobalConfig.mcuListFromServer;
  794 + }else {
  795 + //使用Sass中的数据
  796 + loger.warn("使用Sass中的MCU数据")
  797 + GlobalConfig.mcuListAll = GlobalConfig.mcuList;
  798 + }
  799 +
  800 + GlobalConfig.msListAll.length = 0;
  801 + if(GlobalConfig.msListFromServer.length>0){
  802 + loger.warn("使用server中的MS数据")
  803 + GlobalConfig.msListAll = GlobalConfig.msListFromServer;
  804 + }else {
  805 + loger.warn("使用Sass中的MS数据")
  806 + GlobalConfig.msListAll = GlobalConfig.msList;
  807 + }
780 808
781 - //console.log(" GlobalConfig.mcuListAll", GlobalConfig.mcuListAll)  
782 - //console.log(" GlobalConfig.msListAll", GlobalConfig.msListAll) 809 + console.warn("mcuListAll", GlobalConfig.mcuListAll);
  810 + console.warn("msListAll", GlobalConfig.msListAll);
783 } 811 }
784 812
785 //开始MCU选点操作 813 //开始MCU选点操作
@@ -8,6 +8,7 @@ import Loger from 'Loger'; @@ -8,6 +8,7 @@ import Loger from 'Loger';
8 import MD5 from "md5"; 8 import MD5 from "md5";
9 import iphunter from 'iphunter'; 9 import iphunter from 'iphunter';
10 import fetchJsonp from 'fetch-jsonp'; 10 import fetchJsonp from 'fetch-jsonp';
  11 +import Server from "config/Server";
11 12
12 // 日志对象 13 // 日志对象
13 const loger = Loger.getLoger('IpManager'); 14 const loger = Loger.getLoger('IpManager');
@@ -21,7 +22,46 @@ class IpManager extends Emiter { @@ -21,7 +22,46 @@ class IpManager extends Emiter {
21 constructor() { 22 constructor() {
22 super(); 23 super();
23 } 24 }
24 - 25 + //获取本地Server JSON
  26 + loadServerJosn(_callback){
  27 + let serverInfo = {
  28 + ret: -1
  29 + };
  30 + let timestamp=new Date().getTime();
  31 + let url = `server.json?v=${timestamp}`;
  32 + loger.log("获取Server信息->", url);
  33 + let _this = this;
  34 + fetch(url, {
  35 + timeout: 3000
  36 + })
  37 + .then(ret => {
  38 + if (ret.ok) {
  39 + return ret.json();
  40 + } else {
  41 + loger.error(`获取Server信息-网络异常.状态码:${ret.status}`);
  42 + if (_callback) {
  43 + _callback(serverInfo);
  44 + }
  45 + return;
  46 + }
  47 + })
  48 + .then(ret => {
  49 + if (ret) {
  50 + Server.serverListData=ret||{};
  51 + serverInfo.data=ret;
  52 + }
  53 + loger.log('获取Server信息->完成->', ret);
  54 + if (_callback) {
  55 + _callback(serverInfo);
  56 + }
  57 + })
  58 + .catch(err => {
  59 + loger.error(`获取Server信息-异常.状态码:${err}`);
  60 + if (_callback) {
  61 + _callback(serverInfo);
  62 + }
  63 + });
  64 + }
25 //根据userIp获取ip相关的信息,参数是userIp 65 //根据userIp获取ip相关的信息,参数是userIp
26 getUserIpInfo(token, userIp, _callback, _timeOutDelay) { 66 getUserIpInfo(token, userIp, _callback, _timeOutDelay) {
27 let userIpInfo = { 67 let userIpInfo = {
@@ -12,10 +12,8 @@ class Server { @@ -12,10 +12,8 @@ class Server {
12 12
13 } 13 }
14 static get serverList(){ 14 static get serverList(){
15 - return {  
16 - "MS":{},  
17 - "MCU":{}  
18 - } 15 + return this.serverListData;
  16 +
19 /* return { 17 /* return {
20 "MS":{ 18 "MS":{
21 "共享地址": [ 19 "共享地址": [
@@ -637,5 +635,8 @@ class Server { @@ -637,5 +635,8 @@ class Server {
637 }*/ 635 }*/
638 } 636 }
639 } 637 }
640 - 638 +Server.serverListData={
  639 + "MS":{},
  640 + "MCU":{}
  641 +};
641 export default Server; 642 export default Server;