[ 一日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

 

 

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
1091 [빅데이터의 활용의 오해] 데이터가 당신을 스마트하게 만들고 있을까? 졸리운_곰 2022.09.01 10
1090  [ 一日30分 인생승리의 학습법 ] [Git] Git 명령어 정리 졸리운_곰 2022.08.16 19
1089 [ 一日30分 인생승리의 학습법 ] LLVM이란 file 졸리운_곰 2022.08.13 22
» [ 一日30分 인생승리의 학습법 ] Build Your Own Remote Desktop Application using Javascript, Python & WebRTC — Part 2 file 졸리운_곰 2022.08.05 25
1087 [ 一日30分 인생승리의 학습법 ] Build Your Own Remote Desktop Application using Javascript, Python & WebRTC — Part 1 file 졸리운_곰 2022.08.05 13
1086 [ 一日30分 인생승리의 학습법 ] REST API 규칙 졸리운_곰 2022.08.03 15
1085 [ 一日30分 인생승리의 학습법 ]REST API 설계 (네이밍) 졸리운_곰 2022.08.03 11
1084 [ 一日30分 인생승리의 학습법] [2탄!!] KrakenD Demo 면을 알아보죠! file 졸리운_곰 2022.08.01 13
1083 [ 一日30分 인생승리의 학습법] [1탄!!]KrakenD가 무엇인가? 과연 Api Gateway로 으뜸인가요? file 졸리운_곰 2022.08.01 12
1082 [ 一日30分 인생승리의 학습법] 오픈소스 라이선스 별 의무사항 졸리운_곰 2022.07.29 18
1081 [ 一日30分 인생승리의 학습법] The complete guide to (external) Domain Specific Languages file 졸리운_곰 2022.07.08 24
1080 [ 一日30分 인생승리의 학습법] [Elasticsearch] 기본 개념잡기 file 졸리운_곰 2022.06.02 23
1079 [ 一日30分 인생승리의 학습법]node - pm2로 node.js 프로세스 관리하기 - 기본 명령어, 실행하기 file 졸리운_곰 2022.05.28 17
1078 [ 一日30分 인생승리의 학습법][1탄!!]KrakenD가 무엇인가? 과연 Api Gateway로 으뜸인가요? file 졸리운_곰 2022.04.15 15
1077 [ 一日30分 인생승리의 학습법][API Gateway] Kong Gateway 설치 file 졸리운_곰 2022.04.15 31
1076 [ 一日30分 인생승리의 학습법] TeX_및_LaTeX_수식_문법 file 졸리운_곰 2022.03.19 53
1075 [ 一日30分 인생승리의 학습법] Visual Basic application on linux 졸리운_곰 2022.02.22 16
1074 [ 一日30分 인생승리의 학습법] Truffle을 이용한 DApp 개발환경 구성 file 졸리운_곰 2022.02.20 86
1073 [ 一日30分 인생승리의 학습법]LaTeX 활용해서 논문쓰장 file 졸리운_곰 2022.02.17 20
1072 [ 一日30分 인생승리의 학습법] LaTeX 초보자가 감을 잡는 것을 돕는 몇가지 팁 졸리운_곰 2022.02.17 279
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED