From 589b9f6f7a6be3394f2c7a504f52bc30e4bc4888 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 23 Dec 2023 22:44:45 -0500 Subject: [PATCH] added client.js for shared code --- client.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ control.html | 35 ++++++++++++++++++++++++++++++++++ serve-videos | 41 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 client.js create mode 100644 control.html diff --git a/client.js b/client.js new file mode 100644 index 0000000..d6589d5 --- /dev/null +++ b/client.js @@ -0,0 +1,54 @@ +const Client = ((self = {})=>{ + let ws = null; + + self.connect = (onmsg)=>{ + self.onmessage = onmsg; + + ws = new WebSocket( + "ws://" + window.location.host + ":3000"); + + ws.onmessage = (event)=>{ + self.onmessage(JSON.parse(event.data)); + }; + + ws.onclose = ()=>{ + self.connect(); + }; + }; + + const send = (cmd, data = {})=>{ + const blob = { cmd: cmd, data: data}; + console.log(blob); + ws.send(JSON.stringify(blob)); + }; + + self.skip = (data)=>{ + send("skip"); + }; + + self.play = (data)=>{ + send("play"); + }; + + self.pause = (data)=>{ + send("pause"); + }; + + self.chan_next = (data)=>{ + send("chan_next"); + }; + + self.chan_prev = (data)=>{ + send("chan_prev"); + }; + + self.play_next = (path)=>{ + + }; + + self.play_now = (path)=>{ + + }; + + return self; +})(); \ No newline at end of file diff --git a/control.html b/control.html new file mode 100644 index 0000000..9747101 --- /dev/null +++ b/control.html @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/serve-videos b/serve-videos index 9e01951..e576a09 100755 --- a/serve-videos +++ b/serve-videos @@ -85,14 +85,51 @@ module ATV puts "disconnect: #{client}" end - def on_message(client, data) - # nothing to do, we are stream only + def on_message(client, cmd) + cmd = JSON.parse(cmd) + Cmds[cmd["cmd"]].call(cmd["data"]) end def send(client, data) client.write JSON.dump(data) end + Cmds = { + "skip" => lambda do |data| + cfg = $channels[$channel] + cfg[:play][:start_time] = secs_since_midnight() + cfg[:play][:curr] = cfg[:play][:next] + cfg[:play][:next] = next_show($channel) + update_program() + end, + + "play" => lambda do |data| + pp data + end, + + "pause" => lambda do |data| + pp data + end, + + "chan_prev" => lambda do |data| + $channel -= 1 + if $channel < 0 + $channel = $channels.length - 1 + end + puts $channel + update_program() + end, + + "chan_next" => lambda do |data| + $channel += 1 + if $channel >= $channels.length + $channel = 0 + end + puts $channel + update_program() + end, + } + extend self end -- 2.52.0