-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drab.Socket instead of Drab.Endpoint
- Loading branch information
Showing
9 changed files
with
45 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,16 @@ | ||
defmodule Drab.Endpoint do | ||
@moduledoc ~S""" | ||
To enable drab, use the following clause in your projects `lib/endpoint.ex`: | ||
use Drab.Endpoint | ||
Depreciated. To enable Drab, `use Drab.Socket` in your `UserSocket` module. | ||
""" | ||
|
||
defmacro __using__(_options) do | ||
IO.warn """ | ||
Injecting Drab into Endpoint is depreciated, Drab.Endpoint will be removed. | ||
To enable Drab, `use Drab.Socket` in your `UserSocket` module. | ||
""", Macro.Env.stacktrace(__ENV__) | ||
quote do | ||
# Module.register_attribute __MODULE__, :additional_channels, accumulate: true | ||
|
||
# unquote do | ||
# if options[:add_channels] do | ||
# options[:add_channels] |> Enum.map(fn {channel, module} -> | ||
# quote bind_quoted: [channel: channel, module: module] do | ||
# # socket unquote(channel), unquote(module) | ||
# @additional_channels {channel, module} | ||
# end | ||
# end) | ||
# end | ||
# end | ||
|
||
socket Drab.config.socket, Drab.Socket | ||
# do | ||
# channel "drab:*", Drab.Channel | ||
# end | ||
|
||
|
||
# socket Drab.config.socket, Drab.Socket | ||
end | ||
end | ||
|
||
# defmacro __before_compile__(env) do | ||
# additional_channels = Module.get_attribute(env.module, :additional_channels) | ||
# quote do | ||
# def __additional_channels__, do: unquote(additional_channels) | ||
# end | ||
|
||
# end | ||
|
||
|
||
# defmacro drab(channel, module) do | ||
# quote do | ||
# @additional_channels {unquote(channel), unquote(module)} | ||
# end | ||
# end | ||
|
||
# def __additional_channels__() do | ||
# @additional_channels | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,39 @@ | ||
defmodule Drab.Socket do | ||
# @external_resouce Mix.Project.config[:config_path] | ||
@external_resource "/Users/grych/Dropbox/elixir/phoenix/drabrella/apps/drab_poc/config/config.exs" | ||
@moduledoc false | ||
IO.puts Mix.Project.config[:config_path] | ||
@moduledoc """ | ||
Drab operates on websockets. To enable it, you should inject the Drab.Channel into your Socket module | ||
(by default it is `UserSocket` in `web/channels/user_socket.ex`): | ||
use Phoenix.Socket | ||
require Logger | ||
defmodule MyApp.UserSocket do | ||
use Phoenix.Socket | ||
use Drab.Socket | ||
end | ||
## Channels | ||
channel "drab:*", Drab.Channel | ||
IO.puts " ADD CHANNELS #{inspect Drab.config.additional_channels}" | ||
Drab.config.additional_channels |> Enum.map(fn {name, module} -> | ||
IO.puts "XXXXXx" | ||
IO.puts name | ||
IO.puts inspect(module) | ||
case name do | ||
"drab:" <> _ -> | ||
Logger.error """ | ||
Channel name #{name} is restricted. This config entry for `additional_channels` will be ignored. | ||
""" | ||
_ -> | ||
channel name, module | ||
end | ||
end) | ||
This creates a channel "__drab:*" used by all Drab operations. | ||
By default, Drab uses auto-generated socket with "/socket" path. In case of using different path, use config: | ||
config :drab, | ||
socket: "/my/socket" | ||
""" | ||
|
||
## Transports | ||
transport :websocket, Phoenix.Transports.WebSocket | ||
# transport :longpoll, Phoenix.Transports.LongPoll | ||
defmacro __using__(_options) do | ||
quote do | ||
channel "__drab:*", Drab.Channel | ||
|
||
# Socket params are passed from the client and can | ||
# be used to verify and authenticate a user. After | ||
# verification, you can put default assigns into | ||
# the socket that will be set for all channels, ie | ||
# | ||
# {:ok, assign(socket, :user_id, verified_user_id)} | ||
# | ||
# To deny connection, return `:error`. | ||
# | ||
# See `Phoenix.Token` documentation for examples in | ||
# performing token verification on connect. | ||
def connect(%{"drab_return" => controller_and_action_token}, socket) do | ||
case Phoenix.Token.verify(socket, "controller_and_action", controller_and_action_token) do | ||
{:ok, controller_and_action} -> | ||
[controller, action] = String.split(controller_and_action, "#") | ||
{:ok , socket | ||
|> assign(:controller, String.to_existing_atom(controller)) | ||
|> assign(:action, String.to_existing_atom(action)) | ||
} | ||
{:error, _reason} -> :error | ||
end | ||
end | ||
|
||
def connect(%{"drab_return" => controller_and_action_token}, socket) do | ||
case Phoenix.Token.verify(socket, "controller_and_action", controller_and_action_token) do | ||
{:ok, controller_and_action} -> | ||
[controller, action] = String.split(controller_and_action, "#") | ||
{:ok , socket | ||
|> assign(:controller, String.to_existing_atom(controller)) | ||
|> assign(:action, String.to_existing_atom(action)) | ||
} | ||
{:error, _reason} -> :error | ||
end | ||
end | ||
def connect(_params, _socket), do: :error | ||
|
||
# Socket id's are topics that allow you to identify all sockets for a given user: | ||
# | ||
# def id(socket), do: "users_socket:#{socket.assigns.user_id}" | ||
# | ||
# Would allow you to broadcast a "disconnect" event and terminate | ||
# all active sockets and channels for a given user: | ||
# | ||
# DrabPoc.Endpoint.broadcast("users_socket:#{user.id}", "disconnect", %{}) | ||
# | ||
# Returning `nil` makes this socket anonymous. | ||
def id(_socket), do: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters