成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

遙控你的電腦:通過Node.js和Socket.io實現手機的遠程控制

移動開發
用手機來實現遠程控制是不是很酷?你不需要去寫一個APP應用來實現這種功能,現在的手機瀏覽器已經支出了web socket技術,這提供了很多的可能。這篇文章我們將用Node.js和Socket.io來實現手機控制PC的效果。

用手機來實現遠程控制是不是很酷?你不需要去寫一個APP應用來實現這種功能,現在的手機瀏覽器已經支出了web socket技術,這提供了很多的可能。

這篇文章我們將用Node.js和Socket.io來實現手機控制PC的效果。

我們不必到處尋找基于HTML5的這種應用庫。我們將使用Reveal.js-它來處理幻燈片動畫效果,并支持鍵盤和觸摸事件。

我們不必自己來實現遠程控制的接口,我們將實現手機和電腦的同步。這樣不僅能控制進度,也能同步的顯示在你的手機上。

實現思路

技術上很簡單。Reveal.js讓當前的幻燈片序號在URL的hash上(e.g. http://example.com/#/1).我們將這個hash發送到所有連接的設備上,

對應的將會根據這個hash來自動的顯示響應幻燈片。這樣的話,別人可以方便的直接通過url來直接訪問到顯示的頁面,所有我們需要輸入一個驗證碼

在連接之前進行驗證。

要說的是Reveal.js已經有一套API,我們可以直接調用來同步。但是hash變化的技術很簡單,我們可以直接來實現。

[[140461]]

運行

你可以本地運行示例,或者部署到能提供node.js環境的服務器上。本地運行更簡單些,但是必須本地有node.js并執行npm install.

運行本地代碼

  • 下載示例代碼
  • 確保本地已經安裝node.js。如果沒有,請安裝
  • 解壓剛才下載的代碼包
  • 打開終端進入相應的文件夾
  • 運行npm install來安裝依賴包
  • 運行node app.js來啟動應用
  • PC端在瀏覽器打開http://localhost:8080,并輸入連接碼(默認是kittens)
  • 在手機端瀏覽器打開http://,并輸入連接碼
  • 請享受

代碼

思路說完,讓我們來看看代碼。這主要涉及2個js文件-app.js服務端控制,script.js瀏覽器端。你可以運行這個應用在Node.js 1.10+或者io.js.

后端,我們用到了expressSocket.io。它主要用來響應socket.io的事件監聽。用express.static來讓public下的文件可以訪問到。

public/index.html文件保護顯示的代碼。express.static將會讓它自動顯示,所以我們不需要/來路由。

app.js

  1. // This is the server-side file of our mobile remote controller app. 
  2. // It initializes socket.io and a new express instance. 
  3. // Start it by running 'node app.js' from your terminal. 
  4. // Creating an express server 
  5. var express = require('express'),app = express(); 
  6. // This is needed if the app is run on heroku and other cloud providers: 
  7. var port = process.env.PORT || 8080
  8. // Initialize a new socket.io object. It is bound to 
  9. // the express app, which allows them to coexist. 
  10. var io = require('socket.io').listen(app.listen(port)); 
  11. // App Configuration 
  12. // Make the files in the public folder available to the world 
  13. app.use(express.static(__dirname + '/public')); 
  14. // This is a secret key that prevents others from opening your presentation 
  15. // and controlling it. Change it to something that only you know. 
  16. var secret = 'kittens'
  17. // Initialize a new socket.io application 
  18. var presentation = io.on('connection', function (socket) { 
  19.     // A new client has come online. Check the secret key and 
  20.     // emit a "granted" or "denied" message. 
  21.     socket.on('load', function(data){ 
  22.         socket.emit('access', { 
  23.             access: (data.key === secret ? "granted" : "denied"
  24.         }); 
  25.     }); 
  26.     // Clients send the 'slide-changed' message whenever they navigate to a new slide. 
  27.     socket.on('slide-changed', function(data){ 
  28.         // Check the secret key again 
  29.         if(data.key === secret) { 
  30.             // Tell all connected clients to navigate to the new slide 
  31.             presentation.emit('navigate', { 
  32.                 hash: data.hash 
  33.             }); 
  34.         } 
  35.     }); 
  36. }); 
  37. console.log('Your presentation is running on http://localhost:' + port); 

下面是前端的js文件,將監聽hashchange事件,并發送socket.io消息到服務器端。

public/assets/js/script.js

 
  1. $(function() { 
  2.     // Apply a CSS filter with our blur class (see our assets/css/styles.css) 
  3.     var blurredElements = $('.homebanner, div.reveal').addClass('blur'); 
  4.     // Initialize the Reveal.js library with the default config options 
  5.     // See more here https://github.com/hakimel/reveal.js#configuration 
  6.     Reveal.initialize({ 
  7.         history: true        // Every slide will change the URL 
  8.     }); 
  9.     // Connect to the socket 
  10.     var socket = io(); 
  11.     // Variable initialization 
  12.     var form = $('form.login'), 
  13.         secretTextBox = form.find('input[type=text]'); 
  14.     var key = "", animationTimeout; 
  15.     // When the page is loaded it asks you for a key and sends it to the server 
  16.     form.submit(function(e){ 
  17.         e.preventDefault(); 
  18.         key = secretTextBox.val().trim(); 
  19.         // If there is a key, send it to the server-side 
  20.         // through the socket.io channel with a 'load' event. 
  21.         if(key.length) { 
  22.             socket.emit('load', { 
  23.                 key: key 
  24.             }); 
  25.         } 
  26.     }); 
  27.     // The server will either grant or deny access, depending on the secret key 
  28.     socket.on('access', function(data){ 
  29.         // Check if we have "granted" access. 
  30.         // If we do, we can continue with the presentation. 
  31.         if(data.access === "granted") { 
  32.             // Unblur everything 
  33.             blurredElements.removeClass('blurred'); 
  34.             form.hide(); 
  35.             var ignore = false
  36.             $(window).on('hashchange', function(){ 
  37.                 // Notify other clients that we have navigated to a new slide 
  38.                 // by sending the "slide-changed" message to socket.io 
  39.                 if(ignore){ 
  40.                     // You will learn more about "ignore" in a bit 
  41.                     return
  42.                 } 
  43.                 var hash = window.location.hash; 
  44.                 socket.emit('slide-changed', { 
  45.                     hash: hash, 
  46.                     key: key 
  47.                 }); 
  48.             }); 
  49.             socket.on('navigate', function(data){ 
  50.                 // Another device has changed its slide. Change it in this browser, too: 
  51.                 window.location.hash = data.hash; 
  52.                 // The "ignore" variable stops the hash change from 
  53.                 // triggering our hashchange handler above and sending 
  54.                 // us into a never-ending cycle. 
  55.                 ignore = true
  56.                 setInterval(function () { 
  57.                     ignore = false
  58.                 },100); 
  59.             }); 
  60.         } 
  61.         else { 
  62.             // Wrong secret key 
  63.             clearTimeout(animationTimeout); 
  64.             // Addding the "animation" class triggers the CSS keyframe 
  65.             // animation that shakes the text input. 
  66.             secretTextBox.addClass('denied animation'); 
  67.             animationTimeout = setTimeout(function(){ 
  68.                 secretTextBox.removeClass('animation'); 
  69.             }, 1000); 
  70.             form.show(); 
  71.         } 
  72.     }); 
  73. }); 

現在是幻燈放映時間

手機遠程訪問控制已經可以了。希望你能從中得到有趣的體驗。這種高端的玩具,還不快點兒操練起來?。?/p>

責任編輯:倪明 來源: tutorialzine
相關推薦

2011-09-08 13:53:31

Node.js

2013-10-23 17:17:31

Node.jsdoT

2014-04-10 09:43:00

Node.jsTwilio

2021-05-25 11:55:30

Python手機監控遠程

2014-04-10 09:55:46

手機Node.jswilio

2014-07-22 10:29:04

背包算法coffee

2019-07-26 14:40:58

Vue.jsSocket.IO前端

2016-11-22 13:25:28

Apache Spar大數據

2011-12-23 10:51:24

Node.js

2014-01-17 17:33:32

遠程開機

2017-09-05 15:30:00

JavascriptSocket.ioNode.js

2022-09-04 15:54:10

Node.jsAPI技巧

2017-03-13 10:00:25

Chrome瀏覽器Windows 10

2022-04-02 06:04:03

Node.js代碼緩存V8

2022-04-01 08:02:32

Node.js快照加速hooks

2021-11-16 08:51:29

Node JavaScript變量類型

2014-01-07 17:21:27

遠程控制

2013-12-20 16:43:33

遠程開機關機

2012-01-09 13:24:27

2021-09-10 06:50:03

Node.jsSocket端口
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久综合一区二区三区 | 女人牲交视频一级毛片 | 国产激情免费视频 | 在线免费黄色 | 亚洲精品18| 365夜爽爽欧美性午夜免费视频 | 四虎影院一区二区 | 欧美成人综合 | 日本免费在线观看视频 | 国产在线视频在线观看 | 欧美 日韩 亚洲91麻豆精品 | www.嫩草| 国产精品久久久久久久久久久久久 | 超碰在线97国产 | 区一区二区三在线观看 | 亚洲第一免费播放区 | 波多野结衣在线观看一区二区三区 | 久久免费精品视频 | 欧美综合一区二区三区 | 国产精品久久久久aaaa樱花 | 中文字幕1区2区 | 久久精品国产一区 | 欧美日韩亚洲三区 | 欧美老少妇一级特黄一片 | 97精品一区二区 | 亚洲成人中文字幕 | a国产一区二区免费入口 | 国产高清精品在线 | 欧美区日韩区 | 情侣酒店偷拍一区二区在线播放 | 国产成人精品一区二区三区 | 精品中文在线 | 欧美精品1区2区3区 精品国产欧美一区二区 | 欧美aaaaaaaaaa| 国产成人久久精品 | 影视一区 | 久久精品久久久 | 91久久国产精品 | 亚洲精品久久久9婷婷中文字幕 | 狠狠色网 | 日本不卡在线观看 |