-
-
Save bismark/c827cfc6b4b145d5038cca889f747b83 to your computer and use it in GitHub Desktop.
Phoenix Socket module with overridable callbacks
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
defmodule MyApp.Socket do | |
@moduledoc """ | |
This module extracts the __using__ macro from Phoenix.Socket and makes the | |
Phoenix.Socket.Transport callbacks overridable | |
""" | |
defmacro __using__(opts) do | |
quote do | |
## User API | |
import Phoenix.Socket | |
@behaviour Phoenix.Socket | |
@before_compile Phoenix.Socket | |
Module.register_attribute(__MODULE__, :phoenix_channels, accumulate: true) | |
@phoenix_socket_options unquote(opts) | |
## Callbacks | |
@behaviour Phoenix.Socket.Transport | |
@doc false | |
def child_spec(opts) do | |
Phoenix.Socket.__child_spec__(__MODULE__, opts, @phoenix_socket_options) | |
end | |
@doc false | |
def connect(map), do: Phoenix.Socket.__connect__(__MODULE__, map, @phoenix_socket_options) | |
@doc false | |
def init(state), do: Phoenix.Socket.__init__(state) | |
@doc false | |
def handle_in(message, state), do: Phoenix.Socket.__in__(message, state) | |
@doc false | |
def handle_info(message, state), do: Phoenix.Socket.__info__(message, state) | |
@doc false | |
def terminate(reason, state), do: Phoenix.Socket.__terminate__(reason, state) | |
defoverridable Phoenix.Socket.Transport | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment