mirror of
https://github.com/algora-io/tv.git
synced 2025-02-14 01:59:50 +02:00
add raw websocket handler for chat messages (#55)
This commit is contained in:
parent
cfe3f03bf4
commit
099efd4721
@ -4,6 +4,15 @@ defmodule Algora.Chat.Message do
|
||||
alias Algora.Library.Video
|
||||
import Ecto.Changeset
|
||||
|
||||
@derive {Jason.Encoder,
|
||||
only: [
|
||||
:body,
|
||||
:platform,
|
||||
:sender_handle,
|
||||
:sender_name,
|
||||
:sender_avatar_url,
|
||||
:inserted_at
|
||||
]}
|
||||
schema "messages" do
|
||||
field :body, :string
|
||||
field :platform, :string, virtual: true
|
||||
|
@ -12,6 +12,8 @@ defmodule AlgoraWeb.Endpoint do
|
||||
|
||||
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
|
||||
|
||||
socket "/chat/:channel_handle", AlgoraWeb.Websockets.ChatSocket
|
||||
|
||||
# Serve at "/" the static files from "priv/static" directory.
|
||||
#
|
||||
# You should set gzip to true if you are running phx.digest
|
||||
|
67
lib/algora_web/websockets/chat_socket.ex
Normal file
67
lib/algora_web/websockets/chat_socket.ex
Normal file
@ -0,0 +1,67 @@
|
||||
defmodule AlgoraWeb.Websockets.ChatSocket do
|
||||
@moduledoc """
|
||||
`Phoenix.Socket.Transport` implementation for sending chat messages
|
||||
to the client connection.
|
||||
"""
|
||||
|
||||
@behaviour Phoenix.Socket.Transport
|
||||
|
||||
require Logger
|
||||
|
||||
alias Algora.{Accounts, Library, Chat}
|
||||
|
||||
@base_mount_path "/chat"
|
||||
|
||||
@spec base_mount_path :: String.t()
|
||||
def base_mount_path, do: @base_mount_path
|
||||
|
||||
@impl true
|
||||
def child_spec(_opts) do
|
||||
%{id: Task, start: {Task, :start_link, [fn -> :ok end]}, restart: :transient}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def connect(%{params: %{"channel_handle" => channel_handle}}) do
|
||||
{:ok, %{channel_handle: channel_handle}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(%{channel_handle: channel_handle}) do
|
||||
user = Accounts.get_user_by!(handle: channel_handle)
|
||||
channel = Library.get_channel!(user)
|
||||
video = Library.get_latest_video(user)
|
||||
|
||||
Library.subscribe_to_channel(channel)
|
||||
if video, do: Chat.subscribe_to_room(video)
|
||||
|
||||
{:ok, %{video: video}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_in({_message, _opts}, state) do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({Chat, %Chat.Events.MessageSent{message: message}}, state) do
|
||||
{:push, {:text, Jason.encode!(message)}, state}
|
||||
end
|
||||
|
||||
def handle_info(
|
||||
{Library, %Library.Events.LivestreamStarted{video: video}},
|
||||
state
|
||||
) do
|
||||
if state.video, do: Chat.unsubscribe_to_room(state.video)
|
||||
Chat.subscribe_to_room(video)
|
||||
{:ok, %{video: video}}
|
||||
end
|
||||
|
||||
def handle_info(_message, state) do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def terminate(_reason, _state) do
|
||||
:ok
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user