分享creator 浏览器端 得到PC机IP地址

creator 浏览器端 得到PC机IP地址
方便写一些调试设置

if (cc.sys.isBrowser) {//浏览器
	g_getUserIP(function(ip){
		// alert("Got IP! :" + ip);
		if (ip == "192.168.1.100") {
			cc.log("本机调试")
			G_OpenFFDebug = true

			if (G_OpenFFDebug == true) {
				DataAdvGame.isOpenTouchMove = true	//A星点击移动 开关
				DataAdvGame.isDebugKeyboardMove = true	//键盘移动开关
				DataAdvGame.isDebugBatSpeedCut = true	//战斗减速

				DataNewer.isOpenNewer = false	//关闭新手引导
			}
		}
	});
}
// Usage
// getUserIP(function(ip){
// 	alert("Got IP! :" + ip);
//  });
function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
	//compatibility for firefox and chrome
	var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
	var pc = new myPeerConnection({
		iceServers: []
	}),
		noop = function () { },
		localIPs = {},
		ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
		key;

	function iterateIP(ip) {
		if (!localIPs[ip]) onNewIP(ip);
		localIPs[ip] = true;
	}

	//create a bogus data channel
	pc.createDataChannel("");

	// create offer and set local description
	pc.createOffer().then(function (sdp) {
		sdp.sdp.split('\n').forEach(function (line) {
			if (line.indexOf('candidate') < 0) return;
			line.match(ipRegex).forEach(iterateIP);
		});

		pc.setLocalDescription(sdp, noop, noop);
	}).catch(function (reason) {
		// An error occurred, so handle the failure to connect
	});

	//sten for candidate events
	pc.onicecandidate = function (ice) {
		if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
		ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
	};
}
window.g_getUserIP = getUserIP
1赞

厉害啊,大佬

mark一哈