50 lines
1.1 KiB
Ruby
Executable File
50 lines
1.1 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
require 'websocket-eventmachine-client'
|
|
require 'pp'
|
|
|
|
IP = ARGV[0]
|
|
EFFECT = ARGV[1]
|
|
uri = "ws://#{IP}:80/ws"
|
|
puts "Connecting to #{uri}..."
|
|
|
|
EM.run do
|
|
ws = WebSocket::EventMachine::Client.connect(uri: uri)
|
|
|
|
ws.onopen do
|
|
puts "\033[2J\033[H\n Connected."
|
|
ws.send "effect:#{EFFECT}" if EFFECT
|
|
ws.send "monitor:1"
|
|
end
|
|
|
|
ws.onmessage do |msg, type|
|
|
if type==:binary
|
|
data = msg.unpack("C*")
|
|
width = data.shift
|
|
height = data.shift
|
|
splitter = data.shift
|
|
raise "Unexpected value #{splitter} at index 2" unless splitter==0xFF
|
|
expected_length = width * height * 3 + 4
|
|
raise "Unexpected message length. Expected: #{expected_length}, was: #{data.count + 3}" unless data.count + 3==expected_length
|
|
|
|
str = "\033[H+#{"-"*width}+\n"
|
|
(0...height).each do |y|
|
|
str += "|"
|
|
(0...width).each do |x|
|
|
r, g, b = *data.shift(3)
|
|
str += "\033[38;2;#{r};#{g};#{b}m●"
|
|
end
|
|
str += "\033[0m|\n"
|
|
end
|
|
str += "+#{"-"*width}+\n"
|
|
puts str
|
|
else
|
|
puts msg
|
|
exit
|
|
end
|
|
end
|
|
|
|
ws.onclose do |msg, reason|
|
|
puts "Disconnected. Message: #{msg}. Reason: #{reason}."
|
|
end
|
|
end
|