-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add subscription #47
Add subscription #47
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[submodule "protos"] | ||
path = protos | ||
url = https://github.com/jellyfish-dev/protos.git | ||
branch = master | ||
branch = actually-subscribe-for-notifications | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -15,13 +15,11 @@ defmodule JellyfishWeb.ServerSocket do | |||||||
PeerCrashed, | ||||||||
PeerDisconnected, | ||||||||
RoomCrashed, | ||||||||
RoomNotFound, | ||||||||
RoomsState, | ||||||||
RoomState, | ||||||||
RoomStateRequest | ||||||||
SubscribeRequest, | ||||||||
SubscriptionResponse | ||||||||
} | ||||||||
|
||||||||
alias Jellyfish.ServerMessage.RoomState.{Component, Config, Peer} | ||||||||
alias Jellyfish.ServerMessage.SubscriptionResponse.{RoomNotFound, RoomsState, RoomState} | ||||||||
|
||||||||
@heartbeat_interval 30_000 | ||||||||
|
||||||||
|
@@ -45,7 +43,6 @@ defmodule JellyfishWeb.ServerSocket do | |||||||
case ServerMessage.decode(encoded_message) do | ||||||||
%ServerMessage{content: {:auth_request, %AuthRequest{token: token}}} -> | ||||||||
if token == Application.fetch_env!(:jellyfish, :server_api_token) do | ||||||||
:ok = Phoenix.PubSub.subscribe(Jellyfish.PubSub, "server") | ||||||||
Process.send_after(self(), :send_ping, @heartbeat_interval) | ||||||||
|
||||||||
encoded_message = | ||||||||
|
@@ -82,12 +79,11 @@ defmodule JellyfishWeb.ServerSocket do | |||||||
end | ||||||||
|
||||||||
def handle_in({encoded_message, [opcode: :binary]}, state) do | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick; might as well annotate the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbh, I prefer to make the annotations only once - above the first callback clause, but if everyone prefers adding annotations before each callback I will change this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be clearer this way, GPT4 seems to agree, but ultimately it's your decision |
||||||||
with %ServerMessage{content: request} <- ServerMessage.decode(encoded_message), | ||||||||
{:room_state_request, %RoomStateRequest{content: {_variant, option}}} <- request do | ||||||||
room_state = get_room_state(option) | ||||||||
|
||||||||
with %ServerMessage{content: {:subscribe_request, request}} <- | ||||||||
ServerMessage.decode(encoded_message), | ||||||||
{:ok, response} <- handle_subscribe(request) do | ||||||||
reply = | ||||||||
%ServerMessage{content: room_state} | ||||||||
%ServerMessage{content: response} | ||||||||
|> ServerMessage.encode() | ||||||||
|
||||||||
{:reply, :ok, {:binary, reply}, state} | ||||||||
|
@@ -115,6 +111,28 @@ defmodule JellyfishWeb.ServerSocket do | |||||||
{:stop, :closed, {1003, "operation not allowed"}, state} | ||||||||
end | ||||||||
|
||||||||
defp handle_subscribe(%SubscribeRequest{ | ||||||||
id: id, | ||||||||
event_type: | ||||||||
{:server_notification, | ||||||||
%SubscribeRequest.ServerNotification{room_id: {_variant, option}}} | ||||||||
}) do | ||||||||
:ok = Phoenix.PubSub.subscribe(Jellyfish.PubSub, "server_notification") | ||||||||
|
||||||||
room_state = get_room_state(option) | ||||||||
|
||||||||
msg = | ||||||||
{:subscription_response, | ||||||||
%SubscriptionResponse{ | ||||||||
id: id, | ||||||||
content: room_state | ||||||||
}} | ||||||||
|
||||||||
{:ok, msg} | ||||||||
end | ||||||||
|
||||||||
defp handle_subscribe(request), do: request | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😠, pls remove |
||||||||
|
||||||||
@impl true | ||||||||
def handle_info(:send_ping, state) do | ||||||||
Process.send_after(self(), :send_ping, @heartbeat_interval) | ||||||||
|
@@ -162,7 +180,7 @@ defmodule JellyfishWeb.ServerSocket do | |||||||
:ok | ||||||||
end | ||||||||
|
||||||||
defp get_room_state(:ALL) do | ||||||||
defp get_room_state(:OPTION_ALL) do | ||||||||
rooms = | ||||||||
RoomService.list_rooms() | ||||||||
|> Enum.map(&to_room_state_message/1) | ||||||||
|
@@ -185,24 +203,33 @@ defmodule JellyfishWeb.ServerSocket do | |||||||
components = | ||||||||
room.components | ||||||||
|> Map.values() | ||||||||
|> Enum.map(&%Component{id: &1.id, type: to_proto_type(&1.type)}) | ||||||||
|> Enum.map( | ||||||||
&%RoomState.Component{ | ||||||||
id: &1.id, | ||||||||
type: to_proto_type(&1.type) | ||||||||
} | ||||||||
) | ||||||||
|
||||||||
peers = | ||||||||
room.peers | ||||||||
|> Map.values() | ||||||||
|> Enum.map( | ||||||||
&%Peer{id: &1.id, type: to_proto_type(&1.type), status: to_proto_status(&1.status)} | ||||||||
&%RoomState.Peer{ | ||||||||
id: &1.id, | ||||||||
type: to_proto_type(&1.type), | ||||||||
status: to_proto_status(&1.status) | ||||||||
} | ||||||||
) | ||||||||
|
||||||||
config = struct!(Config, room.config) | ||||||||
config = struct!(RoomState.Config, room.config) | ||||||||
|
||||||||
%RoomState{id: room.id, config: config, peers: peers, components: components} | ||||||||
end | ||||||||
|
||||||||
defp to_proto_type(Jellyfish.Component.HLS), do: :HLS | ||||||||
defp to_proto_type(Jellyfish.Component.RTSP), do: :HLS | ||||||||
defp to_proto_type(Jellyfish.Peer.WebRTC), do: :WEBRTC | ||||||||
defp to_proto_type(Jellyfish.Component.HLS), do: :TYPE_HLS | ||||||||
defp to_proto_type(Jellyfish.Component.RTSP), do: :TYPE_RTSP | ||||||||
defp to_proto_type(Jellyfish.Peer.WebRTC), do: :TYPE_WEBRTC | ||||||||
|
||||||||
defp to_proto_status(:disconnected), do: :DISCONNECTED | ||||||||
defp to_proto_status(:connected), do: :CONNECTED | ||||||||
defp to_proto_status(:disconnected), do: :STATUS_DISCONNECTED | ||||||||
defp to_proto_status(:connected), do: :STATUS_CONNECTED | ||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to revert this 🙏