自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

遙控你的電腦:通過Node.js和Socket.io實(shí)現(xiàn)手機(jī)的遠(yuǎn)程控制

移動(dòng)開發(fā)
用手機(jī)來實(shí)現(xiàn)遠(yuǎn)程控制是不是很酷?你不需要去寫一個(gè)APP應(yīng)用來實(shí)現(xiàn)這種功能,現(xiàn)在的手機(jī)瀏覽器已經(jīng)支出了web socket技術(shù),這提供了很多的可能。這篇文章我們將用Node.js和Socket.io來實(shí)現(xiàn)手機(jī)控制PC的效果。

用手機(jī)來實(shí)現(xiàn)遠(yuǎn)程控制是不是很酷?你不需要去寫一個(gè)APP應(yīng)用來實(shí)現(xiàn)這種功能,現(xiàn)在的手機(jī)瀏覽器已經(jīng)支出了web socket技術(shù),這提供了很多的可能。

這篇文章我們將用Node.js和Socket.io來實(shí)現(xiàn)手機(jī)控制PC的效果。

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

我們不必自己來實(shí)現(xiàn)遠(yuǎn)程控制的接口,我們將實(shí)現(xiàn)手機(jī)和電腦的同步。這樣不僅能控制進(jìn)度,也能同步的顯示在你的手機(jī)上。

實(shí)現(xiàn)思路

技術(shù)上很簡(jiǎn)單。Reveal.js讓當(dāng)前的幻燈片序號(hào)在URL的hash上(e.g. http://example.com/#/1).我們將這個(gè)hash發(fā)送到所有連接的設(shè)備上,

對(duì)應(yīng)的將會(huì)根據(jù)這個(gè)hash來自動(dòng)的顯示響應(yīng)幻燈片。這樣的話,別人可以方便的直接通過url來直接訪問到顯示的頁面,所有我們需要輸入一個(gè)驗(yàn)證碼

在連接之前進(jìn)行驗(yàn)證。

要說的是Reveal.js已經(jīng)有一套API,我們可以直接調(diào)用來同步。但是hash變化的技術(shù)很簡(jiǎn)單,我們可以直接來實(shí)現(xiàn)。

[[140461]]

運(yùn)行

你可以本地運(yùn)行示例,或者部署到能提供node.js環(huán)境的服務(wù)器上。本地運(yùn)行更簡(jiǎn)單些,但是必須本地有node.js并執(zhí)行npm install.

運(yùn)行本地代碼

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

代碼

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

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

public/index.html文件保護(hù)顯示的代碼。express.static將會(huì)讓它自動(dòng)顯示,所以我們不需要/來路由。

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文件,將監(jiān)聽hashchange事件,并發(fā)送socket.io消息到服務(wù)器端。

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. }); 

現(xiàn)在是幻燈放映時(shí)間

手機(jī)遠(yuǎn)程訪問控制已經(jīng)可以了。希望你能從中得到有趣的體驗(yàn)。這種高端的玩具,還不快點(diǎn)兒操練起來??!

責(zé)任編輯:倪明 來源: tutorialzine
相關(guān)推薦

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手機(jī)監(jiān)控遠(yuǎn)程

2014-04-10 09:55:46

手機(jī)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大數(shù)據(jù)

2011-12-23 10:51:24

Node.js

2017-09-05 15:30:00

JavascriptSocket.ioNode.js

2014-01-17 17:33:32

遠(yuǎn)程開機(jī)

2017-03-13 10:00:25

Chrome瀏覽器Windows 10

2022-09-04 15:54:10

Node.jsAPI技巧

2014-01-07 17:21:27

遠(yuǎn)程控制

2012-01-09 13:24:27

2022-04-02 06:04:03

Node.js代碼緩存V8

2022-04-01 08:02:32

Node.js快照加速hooks

2013-12-20 16:43:33

遠(yuǎn)程開機(jī)關(guān)機(jī)

2021-11-16 08:51:29

Node JavaScript變量類型

2021-09-10 06:50:03

Node.jsSocket端口
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)