UNPKG

node-red-contrib-leap-motion

Version:

Node-Red nodes for leap motion

99 lines (79 loc) 3.08 kB
var BaseConnection = module.exports = require('./base') , _ = require('underscore'); var BrowserConnection = module.exports = function(opts) { BaseConnection.call(this, opts); var connection = this; this.on('ready', function() { connection.startFocusLoop(); }) this.on('disconnect', function() { connection.stopFocusLoop(); }) } _.extend(BrowserConnection.prototype, BaseConnection.prototype); BrowserConnection.__proto__ = BaseConnection; BrowserConnection.prototype.useSecure = function(){ return location.protocol === 'https:' } BrowserConnection.prototype.getScheme = function(){ return this.useSecure() ? 'wss:' : 'ws:' } BrowserConnection.prototype.getPort = function(){ return this.useSecure() ? 6436 : 6437 } BrowserConnection.prototype.setupSocket = function() { var connection = this; var socket = new WebSocket(this.getUrl()); socket.onopen = function() { connection.handleOpen(); }; socket.onclose = function(data) { connection.handleClose(data['code'], data['reason']); }; socket.onmessage = function(message) { connection.handleData(message.data) }; socket.onerror = function(error) { // attempt to degrade to ws: after one failed attempt for older Leap Service installations. if (connection.useSecure() && connection.scheme === 'wss:'){ connection.scheme = 'ws:'; connection.port = 6437; connection.disconnect(); connection.connect(); } }; return socket; } BrowserConnection.prototype.startFocusLoop = function() { if (this.focusDetectorTimer) return; var connection = this; var propertyName = null; if (typeof document.hidden !== "undefined") { propertyName = "hidden"; } else if (typeof document.mozHidden !== "undefined") { propertyName = "mozHidden"; } else if (typeof document.msHidden !== "undefined") { propertyName = "msHidden"; } else if (typeof document.webkitHidden !== "undefined") { propertyName = "webkitHidden"; } else { propertyName = undefined; } if (connection.windowVisible === undefined) { connection.windowVisible = propertyName === undefined ? true : document[propertyName] === false; } var focusListener = window.addEventListener('focus', function(e) { connection.windowVisible = true; updateFocusState(); }); var blurListener = window.addEventListener('blur', function(e) { connection.windowVisible = false; updateFocusState(); }); this.on('disconnect', function() { window.removeEventListener('focus', focusListener); window.removeEventListener('blur', blurListener); }); var updateFocusState = function() { var isVisible = propertyName === undefined ? true : document[propertyName] === false; connection.reportFocus(isVisible && connection.windowVisible); } // save 100ms when resuming focus updateFocusState(); this.focusDetectorTimer = setInterval(updateFocusState, 100); } BrowserConnection.prototype.stopFocusLoop = function() { if (!this.focusDetectorTimer) return; clearTimeout(this.focusDetectorTimer); delete this.focusDetectorTimer; }