You've already forked algora-tv
							
							
				mirror of
				https://github.com/algora-io/tv.git
				synced 2025-10-30 23:07:56 +02:00 
			
		
		
		
	wip
This commit is contained in:
		| @@ -15,6 +15,21 @@ config :algora, | ||||
|   ecto_repos: [Algora.Repo.Local], | ||||
|   rtmp_port: 9006 | ||||
|  | ||||
| # dispatch = [ | ||||
| #   _: [ | ||||
| #     {"/chat", AlgoraWeb.ChatSocket, []}, | ||||
| #     {:_, Phoenix.Endpoint.Cowboy2Handler, {AlgoraWeb.Endpoint, []}} | ||||
| #   ] | ||||
| # ] | ||||
|  | ||||
| # dispatch = [ | ||||
| #   {:_, | ||||
| #    [ | ||||
| #      {"/chat", AlgoraWeb.ChatSocket, []}, | ||||
| #      {:_, Plug.Cowboy.Handler, {AlgoraWeb.Endpoint, []}} | ||||
| #    ]} | ||||
| # ] | ||||
|  | ||||
| # Configures the endpoint | ||||
| config :algora, AlgoraWeb.Endpoint, | ||||
|   url: [host: "localhost"], | ||||
| @@ -26,6 +41,9 @@ config :algora, AlgoraWeb.Endpoint, | ||||
|     layout: false | ||||
|   ] | ||||
|  | ||||
| # http: [dispatch: dispatch], | ||||
| # https: [dispatch: dispatch] | ||||
|  | ||||
| config :algora, AlgoraWeb.Embed.Endpoint, | ||||
|   url: [host: "localhost"], | ||||
|   secret_key_base: "H04mI/fsBvjCX3HO+P2bxFEM7PG3SaGTV+DE1f/BbTVG9oiOXSXsq+3tjDXxRXSe", | ||||
|   | ||||
| @@ -47,6 +47,11 @@ defmodule Algora.Application do | ||||
|       # Clustering setup | ||||
|       {DNSCluster, query: Application.get_env(:algora, :dns_cluster_query) || :ignore}, | ||||
|       # Start the Endpoints (http/https) | ||||
|       # Plug.Cowboy.child_spec( | ||||
|       #   scheme: :http, | ||||
|       #   plug: AlgoraWeb.Router, | ||||
|       #   options: [dispatch: dispatch(), port: 4000] | ||||
|       # ), | ||||
|       AlgoraWeb.Endpoint, | ||||
|       AlgoraWeb.Embed.Endpoint, | ||||
|       # Start the LL-HLS controller registry | ||||
| @@ -79,4 +84,13 @@ defmodule Algora.Application do | ||||
|     AlgoraWeb.Embed.Endpoint.config_change(changed, removed) | ||||
|     :ok | ||||
|   end | ||||
|  | ||||
|   defp dispatch do | ||||
|     [ | ||||
|       {:_, | ||||
|        [ | ||||
|          {"/ws/[...]", AlgoraWeb.ChatSocket, []} | ||||
|        ]} | ||||
|     ] | ||||
|   end | ||||
| end | ||||
|   | ||||
							
								
								
									
										60
									
								
								lib/algora_web/chat_socket.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								lib/algora_web/chat_socket.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| defmodule AlgoraWeb.ChatSocket do | ||||
|   @moduledoc """ | ||||
|   Simple Websocket handler that echos back any data it receives | ||||
|   """ | ||||
|  | ||||
|   # Tells the compiler we implement the `cowboy_websocket` | ||||
|   # behaviour. This will give warnings if our | ||||
|   # return types are notably incorrect or if we forget to implement a function. | ||||
|   # FUN FACT: when you `use MyAppWeb, :channel` in your normal Phoenix channel | ||||
|   #           implementations, this is done under the hood for you. | ||||
|   @behaviour :cowboy_websocket | ||||
|  | ||||
|   # entry point of the websocket socket. | ||||
|   # WARNING: this is where you would need to do any authentication | ||||
|   #          and authorization. Since this handler is invoked BEFORE | ||||
|   #          our Phoenix router, it will NOT follow your pipelines defined there. | ||||
|   # | ||||
|   # WARNING: this function is NOT called in the same process context as the rest of the functions | ||||
|   #          defined in this module. This is notably dissimilar to other gen_* behaviours. | ||||
|   @impl :cowboy_websocket | ||||
|   def init(req, opts), do: {:cowboy_websocket, req, opts} | ||||
|  | ||||
|   # as long as `init/2` returned `{:cowboy_websocket, req, opts}` | ||||
|   # this function will be called. You can begin sending packets at this point. | ||||
|   # We'll look at how to do that in the `websocket_handle` function however. | ||||
|   # This function is where you might want to  implement `Phoenix.Presence`, schedule an `after_join` message etc. | ||||
|   @impl :cowboy_websocket | ||||
|   def websocket_init(state), do: {[], state} | ||||
|  | ||||
|   # `websocket_handle` is where data from a client will be received. | ||||
|   # a `frame` will be delivered in one of a few shapes depending on what the client sent: | ||||
|   # | ||||
|   #     :ping | ||||
|   #     :pong | ||||
|   #     {:text, data} | ||||
|   #     {:binary, data} | ||||
|   # | ||||
|   # Similarly, the return value of this function is similar: | ||||
|   # | ||||
|   #     {[reply_frame1, reply_frame2, ....], state} | ||||
|   # | ||||
|   # where `reply_frame` is the same format as what is delivered. | ||||
|   @impl :cowboy_websocket | ||||
|   def websocket_handle(frame, state) | ||||
|  | ||||
|   # :ping is not handled for us like in Phoenix Channels. | ||||
|   # We must explicitly send :pong messages back. | ||||
|   def websocket_handle(:ping, state), do: {[:pong], state} | ||||
|  | ||||
|   # a message was delivered from a client. Here we handle it by just echoing it back | ||||
|   # to the client. | ||||
|   def websocket_handle({:text, message}, state), do: {[{:text, message}], state} | ||||
|  | ||||
|   # This function is where we will process all *other* messages that get delivered to the | ||||
|   # process mailbox. This function isn't used in this handler. | ||||
|   @impl :cowboy_websocket | ||||
|   def websocket_info(info, state) | ||||
|  | ||||
|   def websocket_info(_info, state), do: {[], state} | ||||
| end | ||||
		Reference in New Issue
	
	Block a user