[ 一日30分 인생승리의 학습법 ] Build Your Own Remote Desktop Application using Javascript, Python & WebRTC — Part 2

Build Your Own Remote Desktop Application using Javascript, Python & WebRTC — Part 2

What we going to do ?

We going to build a simple web page which will share your screen on the browser and in that we will integrate mouse onclick events which will get the events from the browser and it will be executed on your desktop using “Python”.

Note : Basically Screen Share API will only allow us to share the screen and its audio. It won’t provide the control functionalities like mouse movements or onclick events. So we are going to add the control feature manually.

Requirements

  • Python 3.4 or above
  • HTML & JavaScript ????

Python Libraries

Let us code . . . ????

Step 1 : Create a client web page with Share Screen Share API

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./app.js"></script>
    <link href="./style.css" rel="stylesheet">
</head>
<body>
    <video id="vid" 
    autoplay 
    onclick="pointer(event)" 
    width="1920" height="1080">
</video>
</body>
</html>

style.css

body {
    display: flex;
    justify-content: center;
}

app.js

/* code to connect with websocket server */
const socket = new WebSocket('ws://localhost:5000')
/* code to get screen display */
navigator.mediaDevices.getDisplayMedia({video: true, audio: true})
.then(stream => {
    // inject stream object as a source for video element //
    document.getElementById('vid').srcObject = stream
    document.getElementById('vid').play
    // inject stream object as a source for video element //
})
.catch(e => console.log(e))
/* The below function send the mouse coordinates to the server */
function pointer(e) {
    let posX = document.getElementById('vid').offsetLeft
    let posY = document.getElementById('vid').offsetTop
    let X = e.pageX - posX
    let Y = e.pageY - posY
    let pointer = X.toString() + ',' + Y.toString()
    console.log(pointer)
    socket.send(pointer)
}

Note : The above javascript code will act as a client and it will send the mouse coordinates (click event) to the server.

Step 2 : Create a websocket server to execute a mouse movement on desktop using Python

implementer.py

import asyncio      # importing asyncio library #
import websockets   # importing websocket library #
import pyautogui    # importing pyautogui library ## Asynchronous function receive the message from client #
async def handler(websocket, path):
    async for message in websocket:
        try:
            pointer = message.split(',')
            pyautogui.moveTo(int(pointer[0]), int(pointer[1]))
            print('X : ', pointer[0])
            print('Y : ', pointer[1])
        except:
            pass# Iinitating the server #
start_server = websockets.serve(handler, "localhost", 5000)# Run's the server continuously #
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Note : In the above python program we are receiving the coordinates as a string instead of getting it as an object and splitting it by passing “comma” as an argument. There is a reason behind sending and receiving the coordinates in “string” format. Because in this example we are integrating the mouse click event functionalities using websockets but in future will establish a remote connection between two different computers using “WebRTC” instead of websockets or socket.io based on that transferring of object using WebRTC may not work. Hence we are transferring the coordinates as a string.

How it works ?

We need to execute python script first and it will wait for the client to connect and then run the web page. Once the web page loaded on the browser it will ask us to select the window to share. After clicking the window it will start to share screen and at the same time the websocket connection will be established.

We need to choose the window
Need to zoom out the browser to 50%. Because we specified the video size as 1920 x 1080.

Note : We need to zoom out the browser to 50%. Because we specified video size as 1920 x 1080 hence in default the web page will be large in size so we need to resize it using browser zoom out option.

After zooming out to 50% you will see your screen on the browser. Now if you click anywhere on the video element the cursor will automatically move to that position on your main desktop.

That’s it… Done.

Finally we added the mouse onclick functionality to Screen Share API. ????

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Important Note :

The Part 1 & Part 2 blogs are mainly posted to get some idea about developing Remote Desktop Application. In upcoming posts we will develope Remote Desktop Application with full functionalities like Mouse Events, Keyboard Events & On-Screen JoyPad Controls (To play PC games on Mobile).

The latest blog links will be updated here soon…

Thank You & Happy Learning ????

[출처] https://ragul-harisankar.medium.com/build-your-own-remote-desktop-application-using-javascript-python-webrtc-c77b32c0249e

 

 

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
1220 ePub 의 개요 [전자책 표준] 가을의 곰을... 2009.09.03 30273
1219 URL Rewrite : 동적 URL 지정 : creating Dynamic URL 가을의 곰을... 2011.11.16 26855
1218 ubuntu에서 tcl/tk 설치 가을의 곰을... 2010.08.08 25275
1217 ProGuard - 자바 역컴파일 방지 [1] 가을의 곰을... 2010.01.14 22736
1216 안드로이드 구조분석 wiki file 가을의 곰을... 2010.01.10 22182
1215 C Programming Links 가을의 곰을... 2009.09.02 21285
1214 자바에서 x86 어셈블리로 프로그래밍: x86 Assembly Programming in Java Platform 가을의 곰을... 2011.11.15 20607
1213 ubuntu에서 wxPython 설치하기 가을의 곰을... 2010.08.08 19759
1212 Programatically retrieving data from a website into a database file 졸리운_곰 2017.02.26 19018
1211 ▣ Emacs 사용법 ver 3.0 [1] 가을의 곰을... 2010.01.02 18711
1210 GOF 디자인패턴 file 가을의 곰을... 2009.12.05 17716
1209 emacs 사용법 file 가을의 곰을... 2010.01.03 17446
1208 미래 네트워크 연구 동향 file 가을의 곰을... 2009.12.13 17268
1207 소스인사이트 단축키 (2) 가을의 곰을... 2010.10.11 17028
1206 Android 빌드하여 AVD 생성 및 시뮬에 올리기 file 가을의 곰을... 2010.08.15 16976
1205 기계학습 (머신러닝:Machine Learning) 참고자료 링크 : 머신러닝 : 기계 학습 프로그래밍 자료 졸리운_곰 2014.11.29 16092
1204 Overview of MS Fortran Compiler 가을의 곰을... 2009.09.04 15981
1203 Java GUI 프로그래밍 가을의 곰을... 2011.06.05 15724
1202 < 목표성취의 7단계 > 가을의 곰을... 2009.08.17 15515
1201 JQuery의 힘으로 제작된 17 가지 오픈소스 웹 게임들 가을의 곰을... 2013.01.02 15373
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED