Created
January 1, 2017 16:05
-
-
Save anonymous/d3680602c7d91c0d18ccc837d5f4fab5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$(document).ready(function(){ | |
// Открываем соединение, при открытии страницы | |
console.log('Сокет открыт в первый раз'); | |
var socket = io.connect(location.origin + ':9999'); | |
// При возвращении на вкладку | |
$(window).focus(function(e) { | |
// Подключаемся к серверу, если отключались | |
if (socket.disconnected) { | |
console.log('Сокет открыт повторно'); | |
socket = io.connect(location.origin + ':9999'); | |
}; | |
}); | |
// При уходе с вкладки | |
$(window).blur(function(e) { | |
console.log('Сокет закрыт'); | |
socket.close(); | |
}); | |
// Работаем с сокетом, если он подключен | |
if (socket.connected) { | |
// Получаем ответ от сервера | |
socket.on('server notice', function(json){ | |
// Открываем новую вкладку | |
if (json.do == 'openNewTab') { | |
window.open(json.URN); | |
}; | |
}); | |
}; | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment