Created
February 22, 2014 22:00
-
-
Save l0gicpath/9163091 to your computer and use it in GitHub Desktop.
Streaming your screen over http using screenshots. Uses 'screencapture' for mac systems, you'll need to find another screen capturing utility to be used from the terminal on your system of choice. An enhanced code over https://gist.github.com/blazeeboy/9151757
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'socket' # needed for creating tcp servers | |
require 'base64' # we'll use base64 for encoding images and embedding them in the response | |
require 'cgi' # has a lovely helper method for generating rfc1123 compliant dates for http headers | |
refresh_rate = 1 # in seconds, interval between each screen capture | |
server = TCPServer.open 3005 # start our server at port 3005 | |
# the html page template we'll be sending over to the browser | |
page_tmpl = %Q[ | |
<html> | |
<head> | |
<script> | |
setTimeout(function() { | |
document.location.reload(); | |
}, #{refresh_rate * 1000}); | |
</script> | |
</head> | |
<body style="padding: 0px; margin: 0px"> | |
<img style="height: 100%%" src="data: image/png;base64,%s" /> | |
</body> | |
</html> | |
] | |
Thread.start do | |
loop { | |
system('screencapture -C -T 0.5 -r -x tmp.png') # we'll capture the screen in a separate thread | |
sleep refresh_rate | |
} | |
end | |
loop do | |
connection = server.accept # accept a connection and spawn a thread to handle it | |
Thread.start(connection) do |client| | |
File.open('tmp.png', 'rb') do |image| | |
response = page_tmpl % Base64.encode64(image.read) | |
# send out http headers | |
client.puts ["HTTP/1.1 200 OK", | |
"Date: #{CGI.rfc1123_date(Time.now)}", | |
"Server: Ruby", | |
"Content-Type: text/html", | |
"Content-Length: #{response.length}\r\n\r\n"].join("\r\n") | |
client.puts response | |
client.close | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment