Skip to content

Commit

Permalink
Include time in EOT message
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jan 23, 2025
1 parent 7894941 commit bc05282
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions lib/asciinema/streaming/live_stream_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ defmodule Asciinema.Streaming.LiveStreamServer do
Logger.info("stream/#{state.stream_id}: terminating (#{inspect(reason)})")
Logger.debug("stream/#{state.stream_id}: state: #{inspect(state)}")

publish(state.stream_id, :end)
time = current_stream_time(state.last_stream_time, state.last_event_time) || 0.0
publish(state.stream_id, :end, %{time: time})
Streaming.update_live_stream(state.stream, online: false)

:ok
Expand All @@ -222,7 +223,7 @@ defmodule Asciinema.Streaming.LiveStreamServer do
%{state | last_stream_time: time, last_event_time: Timex.now()}
end

defp publish(stream_id, event, data \\ nil) do
defp publish(stream_id, event, data) do
update = %Update{
stream_id: stream_id,
event: event,
Expand Down Expand Up @@ -271,6 +272,8 @@ defmodule Asciinema.Streaming.LiveStreamServer do
%{state | shutdown_timer: timer}
end

defp current_stream_time(nil, nil), do: nil

defp current_stream_time(last_stream_time, last_event_time) do
last_stream_time + Timex.diff(Timex.now(), last_event_time, :microseconds)
end
Expand Down
12 changes: 8 additions & 4 deletions lib/asciinema_web/live_stream_consumer_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do
{:reply, marker_message(time, label), state}
end

def websocket_info(%LiveStreamServer.Update{event: :end}, state) do
{:reply, eot_message(), state}
def websocket_info(%LiveStreamServer.Update{event: :end} = update, state) do
%{time: time} = update.data

{:reply, eot_message(time), state}
end

def websocket_info(:client_ping, state) do
Expand Down Expand Up @@ -310,10 +312,12 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do
{:binary, msg}
end

defp eot_message do
defp eot_message(time) do
msg = <<
# message type: EOT
0x04::8
0x04::8,
# current stream time
time::little-64
>>

{:binary, msg}
Expand Down

0 comments on commit bc05282

Please sign in to comment.