DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Emad has posted 58 posts at DZone. View Full User Profile

Share your screen on the Local Network using ruby

03.02.2014
| 1616 views |
  • submit to reddit
require 'socket'
require 'base64'

Refresh = 1 # seconds to refresh image on server
screen_capture_command = 'screencapture -C -x tmp.png'
image = ''
latest = Time.now
server = TCPServer.new 3000

loop do
  Thread.start(server.accept) do |client|
	if Time.now-latest>=Refresh
		system(screen_capture_command)
		latest = Time.now
		File.open('tmp.png', 'rb') do |file| 
			image = 'data:image/png;base64,'+Base64.encode64(file.read) 
		end
	end
  	client.puts '<html><head>'+
				"<script>setTimeout(function(){document.location.reload();},#{Refresh*1000});</script>"+
				'</head><body style="padding:0px;margin:0px;">'+
				"<img src=\"#{image}\" style=\"height:100%;\"/>"+
				'</body></html>'
    client.close
  end
end