Mn cho hỏi về socket với ạ



  • Mn cho hỏi tạo 1 socket dễ ko ạ
    Và nếu thay socket của họ thành của mình thì liệu có hoạt động ko hay sữa hàm linh tinh nữa ạ. Socket họ socket.ngocrong.net

    //setTimeout(() => {
    $.post(url + '/api/game/taixiu/invokeGameController/' + channelMd5).done((res) => {
    });
    //}, 100);

    var pusher = new Pusher('8ffbce8beac4f5362483', {
    cluster: 'ap1',
    forceTLS: true
    });

    var channelTaiXiuMd5 = pusher.subscribe('taixiu_' + channelMd5);
    var channelTaiXiu = pusher.subscribe('taixiu');
    var channelTaiXiuDatKeo = pusher.subscribe('taixiu.datkeo');

    [channelTaiXiu, channelTaiXiuMd5].map( x => x.bind('my-event', function(data) {
    var gameObj = data.message;

    vue.$refs.taixiu.id = gameObj.id;
    vue.$refs.taixiu.amountUser.tag.tai = generateAmountUserTag(gameObj.amountUserTai);
    vue.$refs.taixiu.amountUser.tag.xiu = generateAmountUserTag(gameObj.amountUserXiu);
    vue.$refs.taixiu.amount.tag.tai = generateAmountTag(gameObj.amountTai);
    vue.$refs.taixiu.amount.tag.xiu = generateAmountTag(gameObj.amountXiu);
    
    //console.log(gameObj);
    //console.log(gameObj.status);
    
    let refreshIntervalId;
    
    //nếu game ở trạng thĂ¡i Ä‘ang chạy
    if (gameObj.status === 'running'){
        //console.log('tai xiu running');
        vue.$refs.taixiu.status.downTimeDisplay = 'block';
        vue.$refs.taixiu.status.opacityDangCanCua = 0;
        vue.$refs.taixiu.status.opacityXiNgau = 0;
        vue.$refs.updia = false;
        showNotify = 0;
        showNotifyProfit = 0;
    
        //thời gian kết thĂºc
        //let finishedAt = gameObj.finishedAtUnixTimestamp;
    
        //đếm ngược
        refreshIntervalId = setInterval( () => {
            var now = new Date();
            var t = new Date(  gameObj.finishedAtUnixTimestamp  * 1000);
            let difference = (t - now) / 1000;
            difference = Math.round(difference);
            //console.log(difference);
    
            if( difference >= 0 ){
                //console.log("You waited: " + difference + " seconds");
                let left = difference.toString().charAt(0)
                let right = difference.toString().charAt(1)
    
                for (let i=0; i< percentArray.length; i++){
                    if(left == i.toString()){
                        left = percentArray[i];
                    }
    
                    if(right == i.toString()){
                        right = percentArray[i];
                    }
    
                    if ( difference.toString().length == 1 ){
                        left = 0;
                        right = percentArray[difference]
                    }
                }
    
                vue.$refs.taixiu.downTime.left = left;
                vue.$refs.taixiu.downTime.right = right;
            }else {
                clearInterval(refreshIntervalId);
                vue.$refs.taixiu.status.downTimeDisplay = 'none';
                vue.$refs.taixiu.status.opacityDangCanCua = 1;
                vue.$refs.taixiu.status.opacityXiNgau = 0;
            }
        }, 1000);
    }
    
    //nếu game ở trạng thĂ¡i Ä‘ang tĂ­nh toĂ¡n
    if (gameObj.status === 'calculating'){
        vue.$refs.taixiu.status.downTimeDisplay = 'none';
        vue.$refs.taixiu.status.opacityDangCanCua = 1;
        vue.$refs.taixiu.status.opacityXiNgau = 0;
    }
    
    //nếu game ở trạng thĂ¡i trả kết quả
    if (gameObj.status === 'resulting'){
        vue.$refs.taixiu.status.downTimeDisplay = 'none';
        vue.$refs.taixiu.status.opacityDangCanCua = 0;
        vue.$refs.taixiu.status.opacityXiNgau = 1;
        vue.$refs.taixiu.xi_ngau_1 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_1) + '% 0% / cover no-repeat';
        vue.$refs.taixiu.xi_ngau_2 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_2) + '% 0% / cover no-repeat';
        vue.$refs.taixiu.xi_ngau_3 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_3) + '% 0% / cover no-repeat';
    
        updateBalance();
        showNotify++;
    
        if (!vue.$refs.taixiu.nan){
            if (showNotify === 1) {
                generateBangKetQuaTaiXiu();
                $.notify("Kết quả tĂ i xỉu : " + gameObj.xuc_xac_1 + " " + gameObj.xuc_xac_2 + " " + gameObj.xuc_xac_3, "info");
                showNotify ++;
            }
        }
    
        //notify
        if (typeof (userId) != 'undefined'){
            //hiển thị số tiền thắng
            $.post(url + '/api/game/taixiu/tienthang', {
                'game_id' : gameObj.id,
                'user_id' : userId
            }).done(function (res) {
                console.log(res);
    
                let thang = res.repay_value - res.value;
    
                if (thang > 0){
                    //$.notify("Bạn thắng kèo tĂ i xỉu #" + data.id + " : " + thang, "info")
                    if (showNotifyProfit === 0)
                        $.notify( "+" + res.repay_value, "info")
                }else if (thang < 0) {
                    if (showNotifyProfit === 0)
                        $.notify( thang, "info")
                }
    
                showNotifyProfit++;
            });
        }
    
    }
    
    //nếu game ở trạng thĂ¡i hoĂ n thĂ nh
    if (gameObj.status === 'finished'){
        vue.$refs.taixiu.status.downTimeDisplay = 'none';
        vue.$refs.taixiu.status.opacityDangCanCua = 0;
        vue.$refs.taixiu.status.opacityXiNgau = 1;
        vue.$refs.taixiu.xi_ngau_1 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_1) + '% 0% / cover no-repeat';
        vue.$refs.taixiu.xi_ngau_2 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_2) + '% 0% / cover no-repeat';
        vue.$refs.taixiu.xi_ngau_3 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_3) + '% 0% / cover no-repeat';
    }
    

    }));

    channelTaiXiuDatKeo.bind('my-event', function(data) {
    var gameObj = data.message;
    vue.$refs.taixiu.amountUser.tag.tai = generateAmountUserTag(gameObj.amountUserTai);
    vue.$refs.taixiu.amountUser.tag.xiu = generateAmountUserTag(gameObj.amountUserXiu);
    vue.$refs.taixiu.amount.tag.tai = generateAmountTag(gameObj.amountTai);
    vue.$refs.taixiu.amount.tag.xiu = generateAmountTag(gameObj.amountXiu);
    });

    //chat
    var channelChat = pusher.subscribe('chat');

    $.get(url + '/chat').done(function (res) {
    res = JSON.parse(res);
    let list = [];
    res.forEach((item, index) => {
    list.push(JSON.parse(item));
    });

    	vue.$refs.chatbox.msg = list.reverse();
    	vue.$refs.chatbox._url = url;
    	vue.$refs.chatbox.chatColor = chatColor;
    });
    

    channelChat.bind('my-event', function (data) {
    $.get(url + '/chat').done(function (res) {
    res = JSON.parse(res);
    let list = [];
    res.forEach((item, index) => {
    list.push(JSON.parse(item));
    });

    	vue.$refs.chatbox.msg = list.reverse();
    	vue.$refs.chatbox._url = url;
    	vue.$refs.chatbox.chatColor = chatColor;
    });
    

    });

    //ably
    //WZ2P7w.DtxTxA:k7eJNFZz1doR6E7Q
    //qZmPwg.FxxTHQ:xblCfQ8qrEyDHCaw
    //qZQrkA.rM3QFw:Ngfkz9KNN0S9olnG
    //6stlRA.dDLpYg:oMCnJCOpdfT506NI
    AblyKeys = [
    'WZ2P7w.DtxTxA:k7eJNFZz1doR6E7Q',
    'qZmPwg.FxxTHQ:xblCfQ8qrEyDHCaw',
    'qZQrkA.rM3QFw:Ngfkz9KNN0S9olnG',
    '6stlRA.dDLpYg:oMCnJCOpdfT506NI',
    '6stlRA.JBr1Kg:ARNy4nlyH1u49tpN'
    ];

    AblyKeys.map( key => {
    console.log(key)

    let client = new Ably.Realtime(key);
    
    var channelTaiXiu = client.channels.get('taixiu');
    var channelTaiXiuMd5 = client.channels.get('taixiu_' + channelMd5);
    var channelTaiXiuDatKeo = client.channels.get('taixiu.datkeo');
    var channelChat = client.channels.get('chat');
    
    [channelTaiXiu, channelTaiXiuMd5].map( x => x.subscribe('myEvent', function(message) {
        var gameObj = message.data.message;
    
        vue.$refs.taixiu.id = gameObj.id;
        vue.$refs.taixiu.amountUser.tag.tai = generateAmountUserTag(gameObj.amountUserTai);
        vue.$refs.taixiu.amountUser.tag.xiu = generateAmountUserTag(gameObj.amountUserXiu);
        vue.$refs.taixiu.amount.tag.tai = generateAmountTag(gameObj.amountTai);
        vue.$refs.taixiu.amount.tag.xiu = generateAmountTag(gameObj.amountXiu);
    
        //console.log(gameObj);
        //console.log(gameObj.status);
    
        let refreshIntervalId;
    
        //nếu game ở trạng thĂ¡i Ä‘ang chạy
        if (gameObj.status === 'running'){
            //console.log('tai xiu running');
            vue.$refs.taixiu.status.downTimeDisplay = 'block';
            vue.$refs.taixiu.status.opacityDangCanCua = 0;
            vue.$refs.taixiu.status.opacityXiNgau = 0;
            vue.$refs.updia = false;
            showNotify = 0;
            showNotifyProfit = 0;
    
            //thời gian kết thĂºc
            //let finishedAt = gameObj.finishedAtUnixTimestamp;
    
            //đếm ngược
            refreshIntervalId = setInterval( () => {
                var now = new Date();
                var t = new Date(  gameObj.finishedAtUnixTimestamp  * 1000);
                let difference = (t - now) / 1000;
                difference = Math.round(difference);
                //console.log(difference);
    
                if( difference >= 0 ){
                    //console.log("You waited: " + difference + " seconds");
                    let left = difference.toString().charAt(0)
                    let right = difference.toString().charAt(1)
    
                    for (let i=0; i< percentArray.length; i++){
                        if(left == i.toString()){
                            left = percentArray[i];
                        }
    
                        if(right == i.toString()){
                            right = percentArray[i];
                        }
    
                        if ( difference.toString().length == 1 ){
                            left = 0;
                            right = percentArray[difference]
                        }
                    }
    
                    vue.$refs.taixiu.downTime.left = left;
                    vue.$refs.taixiu.downTime.right = right;
                }else {
                    clearInterval(refreshIntervalId);
                    vue.$refs.taixiu.status.downTimeDisplay = 'none';
                    vue.$refs.taixiu.status.opacityDangCanCua = 1;
                    vue.$refs.taixiu.status.opacityXiNgau = 0;
                }
            }, 1000);
        }
    
        //nếu game ở trạng thĂ¡i Ä‘ang tĂ­nh toĂ¡n
        if (gameObj.status === 'calculating'){
            vue.$refs.taixiu.status.downTimeDisplay = 'none';
            vue.$refs.taixiu.status.opacityDangCanCua = 1;
            vue.$refs.taixiu.status.opacityXiNgau = 0;
        }
    
        //nếu game ở trạng thĂ¡i trả kết quả
        if (gameObj.status === 'resulting'){
            vue.$refs.taixiu.status.downTimeDisplay = 'none';
            vue.$refs.taixiu.status.opacityDangCanCua = 0;
            vue.$refs.taixiu.status.opacityXiNgau = 1;
            vue.$refs.taixiu.xi_ngau_1 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_1) + '% 0% / cover no-repeat';
            vue.$refs.taixiu.xi_ngau_2 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_2) + '% 0% / cover no-repeat';
            vue.$refs.taixiu.xi_ngau_3 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_3) + '% 0% / cover no-repeat';
    
            updateBalance();
            showNotify++;
    
            if (!vue.$refs.taixiu.nan){
                if (showNotify === 1) {
                    generateBangKetQuaTaiXiu();
                    $.notify("Kết quả tĂ i xỉu : " + gameObj.xuc_xac_1 + " " + gameObj.xuc_xac_2 + " " + gameObj.xuc_xac_3, "info");
                    showNotify ++;
                }
            }
    
            //notify
            if (typeof (userId) != 'undefined'){
                //hiển thị số tiền thắng
                $.post(url + '/api/game/taixiu/tienthang', {
                    'game_id' : gameObj.id,
                    'user_id' : userId
                }).done(function (res) {
                    console.log(res);
    
                    let thang = res.repay_value - res.value;
    
                    if (thang > 0){
                        //$.notify("Bạn thắng kèo tĂ i xỉu #" + data.id + " : " + thang, "info")
                        if (showNotifyProfit === 0)
                            $.notify( "+" + thang, "info")
                    }else if (thang < 0) {
                        if (showNotifyProfit === 0)
                            $.notify( thang, "info")
                    }
    
                    showNotifyProfit++;
                });
            }
    
        }
    
        //nếu game ở trạng thĂ¡i hoĂ n thĂ nh
        if (gameObj.status === 'finished'){
            vue.$refs.taixiu.status.downTimeDisplay = 'none';
            vue.$refs.taixiu.status.opacityDangCanCua = 0;
            vue.$refs.taixiu.status.opacityXiNgau = 1;
            vue.$refs.taixiu.xi_ngau_1 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_1) + '% 0% / cover no-repeat';
            vue.$refs.taixiu.xi_ngau_2 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_2) + '% 0% / cover no-repeat';
            vue.$refs.taixiu.xi_ngau_3 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_3) + '% 0% / cover no-repeat';
        }
    
    }));
    
    channelTaiXiuDatKeo.subscribe('myEvent', function(message) {
    	var gameObj = message.data.message;
    
    	vue.$refs.taixiu.amountUser.tag.tai = generateAmountUserTag(gameObj.amountUserTai);
    	vue.$refs.taixiu.amountUser.tag.xiu = generateAmountUserTag(gameObj.amountUserXiu);
    	vue.$refs.taixiu.amount.tag.tai = generateAmountTag(gameObj.amountTai);
    	vue.$refs.taixiu.amount.tag.xiu = generateAmountTag(gameObj.amountXiu);
    });
    
    $.get(url + '/chat').done(function (res) {
    	res = JSON.parse(res);
    	let list = [];
    	res.forEach((item, index) => {
    		list.push(JSON.parse(item));
    	});
    	
    	vue.$refs.chatbox.msg = list.reverse();
    	vue.$refs.chatbox._url = url;
    	vue.$refs.chatbox.chatColor = chatColor;
    });
    
    channelChat.subscribe('myEvent', function(message) {
    	$.get(url + '/chat').done(function (res) {
    		res = JSON.parse(res);
    		let list = [];
    		res.forEach((item, index) => {
    			list.push(JSON.parse(item));
    		});
    		
    		vue.$refs.chatbox.msg = list.reverse();
    		vue.$refs.chatbox._url = url;
    		vue.$refs.chatbox.chatColor = chatColor;
    	});
    });
    

    });

    var socket = io('https://socket.ngocrong.net');

    channels = [ 'taixiu_' + channelMd5, 'taixiu' ];

    channels.map( (x) => {
    socket.on(x, function(data){
    var gameObj = data;
    console.log(gameObj);

    	vue.$refs.taixiu.id = gameObj.id;
    	vue.$refs.taixiu.amountUser.tag.tai = generateAmountUserTag(gameObj.amountUserTai);
    	vue.$refs.taixiu.amountUser.tag.xiu = generateAmountUserTag(gameObj.amountUserXiu);
    	vue.$refs.taixiu.amount.tag.tai = generateAmountTag(gameObj.amountTai);
    	vue.$refs.taixiu.amount.tag.xiu = generateAmountTag(gameObj.amountXiu);
    
    	//console.log(gameObj);
    	//console.log(gameObj.status);
    
    	let refreshIntervalId;
    
    	//nếu game ở trạng thĂ¡i Ä‘ang chạy
    	if (gameObj.status === 'running'){
    		//console.log('tai xiu running');
    		vue.$refs.taixiu.status.downTimeDisplay = 'block';
    		vue.$refs.taixiu.status.opacityDangCanCua = 0;
    		vue.$refs.taixiu.status.opacityXiNgau = 0;
    		vue.$refs.updia = false;
    		showNotify = 0;
    		showNotifyProfit = 0;
    
    		//thời gian kết thĂºc
    		//let finishedAt = gameObj.finishedAtUnixTimestamp;
    
    		//đếm ngược
    		refreshIntervalId = setInterval( () => {
    			var now = new Date();
    			var t = new Date(  gameObj.finishedAtUnixTimestamp  * 1000);
    			let difference = (t - now) / 1000;
    			difference = Math.round(difference);
    			//console.log(difference);
    
    			if( difference >= 0 ){
    				//console.log("You waited: " + difference + " seconds");
    				let left = difference.toString().charAt(0)
    				let right = difference.toString().charAt(1)
    
    				for (let i=0; i< percentArray.length; i++){
    					if(left == i.toString()){
    						left = percentArray[i];
    					}
    
    					if(right == i.toString()){
    						right = percentArray[i];
    					}
    
    					if ( difference.toString().length == 1 ){
    						left = 0;
    						right = percentArray[difference]
    					}
    				}
    
    				vue.$refs.taixiu.downTime.left = left;
    				vue.$refs.taixiu.downTime.right = right;
    			}else {
    				clearInterval(refreshIntervalId);
    				vue.$refs.taixiu.status.downTimeDisplay = 'none';
    				vue.$refs.taixiu.status.opacityDangCanCua = 1;
    				vue.$refs.taixiu.status.opacityXiNgau = 0;
    			}
    		}, 1000);
    	}
    
    	//nếu game ở trạng thĂ¡i Ä‘ang tĂ­nh toĂ¡n
    	if (gameObj.status === 'calculating'){
    		vue.$refs.taixiu.status.downTimeDisplay = 'none';
    		vue.$refs.taixiu.status.opacityDangCanCua = 1;
    		vue.$refs.taixiu.status.opacityXiNgau = 0;
    	}
    
    	//nếu game ở trạng thĂ¡i trả kết quả
    	if (gameObj.status === 'resulting'){
    		vue.$refs.taixiu.status.downTimeDisplay = 'none';
    		vue.$refs.taixiu.status.opacityDangCanCua = 0;
    		vue.$refs.taixiu.status.opacityXiNgau = 1;
    		vue.$refs.taixiu.xi_ngau_1 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_1) + '% 0% / cover no-repeat';
    		vue.$refs.taixiu.xi_ngau_2 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_2) + '% 0% / cover no-repeat';
    		vue.$refs.taixiu.xi_ngau_3 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_3) + '% 0% / cover no-repeat';
    
    		updateBalance();
    		showNotify++;
    
    		if (!vue.$refs.taixiu.nan){
    			if (showNotify === 1) {
    				generateBangKetQuaTaiXiu();
    				$.notify("Kết quả tĂ i xỉu : " + gameObj.xuc_xac_1 + " " + gameObj.xuc_xac_2 + " " + gameObj.xuc_xac_3, "info");
    				showNotify ++;
    			}
    		}
    
    		//notify
    		if (typeof (userId) != 'undefined'){
    			//hiển thị số tiền thắng
    			$.post(url + '/api/game/taixiu/tienthang', {
    				'game_id' : gameObj.id,
    				'user_id' : userId
    			}).done(function (res) {
    				console.log(res);
    
    				let thang = res.repay_value - res.value;
    
    				if (thang > 0){
    					//$.notify("Bạn thắng kèo tĂ i xỉu #" + data.id + " : " + thang, "info")
    					if (showNotifyProfit === 0)
    						$.notify( "+" + res.repay_value, "info")
    				}else if (thang < 0) {
    					if (showNotifyProfit === 0)
    						$.notify( thang, "info")
    				}
    
    				showNotifyProfit++;
    			});
    		}
    
    	}
    
    	//nếu game ở trạng thĂ¡i hoĂ n thĂ nh
    	if (gameObj.status === 'finished'){
    		vue.$refs.taixiu.status.downTimeDisplay = 'none';
    		vue.$refs.taixiu.status.opacityDangCanCua = 0;
    		vue.$refs.taixiu.status.opacityXiNgau = 1;
    		vue.$refs.taixiu.xi_ngau_1 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_1) + '% 0% / cover no-repeat';
    		vue.$refs.taixiu.xi_ngau_2 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_2) + '% 0% / cover no-repeat';
    		vue.$refs.taixiu.xi_ngau_3 = `url(${url}/caytien.net/tx/xn2.png) ` + generateXiNgauStyle(gameObj.xuc_xac_3) + '% 0% / cover no-repeat';
    	}
    		
    });
    

    });

    $.get(url + '/chat').done(function (res) {
    res = JSON.parse(res);
    let list = [];
    res.forEach((item, index) => {
    list.push(JSON.parse(item));
    });

    	vue.$refs.chatbox.msg = list.reverse();
    	vue.$refs.chatbox._url = url;
    	vue.$refs.chatbox.chatColor = chatColor;
    });
    

    socket.on('chat', function(data){
    $.get(url + '/chat').done(function (res) {
    res = JSON.parse(res);
    let list = [];
    res.forEach((item, index) => {
    list.push(JSON.parse(item));
    });

    	vue.$refs.chatbox.msg = list.reverse();
    	vue.$refs.chatbox._url = url;
    	vue.$refs.chatbox.chatColor = chatColor;
    });
    

    });

    socket.on('taixiu.datkeo', function(data){
    var gameObj = data;

    vue.$refs.taixiu.amountUser.tag.tai = generateAmountUserTag(gameObj.amountUserTai);
    vue.$refs.taixiu.amountUser.tag.xiu = generateAmountUserTag(gameObj.amountUserXiu);
    vue.$refs.taixiu.amount.tag.tai = generateAmountTag(gameObj.amountTai);
    vue.$refs.taixiu.amount.tag.xiu = generateAmountTag(gameObj.amountXiu);
    

    });


Log in to reply