Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renaming Websocket -> Raw Websocket. Adding cowboy_dispatch macro

  • Loading branch information...
commit 7c58eebb40ae44dfbe7cde55181a3dbb0fc66643 1 parent ff7cce3
@jeregrine authored
View
13 lib/phoenix/adapters/cowboy.ex
@@ -6,4 +6,17 @@ defmodule Phoenix.Adapters.Cowboy do
Dict.put(options, :dispatch, [{:_, dispatch}])
end
+
+ defmacro __using__(_options) do
+ quote do
+ Module.register_attribute __MODULE__, :dispatch_options, accumulate: true,
+ persist: false
+ import unquote(__MODULE__)
+ end
+ end
+ defmacro cowboy_dispatch(path, handler, options \\ []) do
+ quote do
+ @dispatch_options {unquote(path), unquote(handler), unquote(options)}
+ end
+ end
end
View
3  lib/phoenix/examples/controller.ex
@@ -58,7 +58,8 @@ defmodule Phoenix.Examples.Controllers.Files do
end
defmodule Phoenix.Examples.Controllers.Echo do
- use Phoenix.Controller.Websocket
+ use Phoenix.Websocket.RawHandler
+
def stream(data, socket, _state) do
reply socket, data
end
View
2  lib/phoenix/examples/router.ex
@@ -12,6 +12,6 @@ defmodule Router do
resources "comments", Comments
end
- websocket "/echo", Eco
+ raw_websocket "/echo", Eco
end
end
View
3  lib/phoenix/router/router.ex
@@ -7,7 +7,8 @@ defmodule Phoenix.Router do
defmacro __using__(plug_adapter_options \\ []) do
quote do
use Phoenix.Router.Mapper
- use Phoenix.Router.WebsocketMapper
+ use Phoenix.Adapters.Cowboy
+ import Phoenix.Router.RawWebsocketMapper
import unquote(__MODULE__)
@before_compile unquote(__MODULE__)
View
14 lib/phoenix/router/websocket_mapper.ex
@@ -1,15 +1,7 @@
-defmodule Phoenix.Router.WebsocketMapper do
- defmacro __using__(_options) do
+defmodule Phoenix.Router.RawWebsocketMapper do
+ defmacro raw_websocket(path, handler, options \\ []) do
quote do
- Module.register_attribute __MODULE__, :dispatch_options, accumulate: true,
- persist: false
- import unquote(__MODULE__)
- end
- end
-
- defmacro websocket(path, handler, options \\ []) do
- quote do
- @dispatch_options {unquote(path), unquote(handler), unquote(options) }
+ cowboy_dispatch unquote(path), unquote(handler), unquote(options)
end
end
end
View
10 lib/phoenix/controller/websocket.ex → lib/phoenix/websocket/raw_websocket.ex
@@ -1,4 +1,4 @@
-defmodule Phoenix.Controller.Websocket do
+defmodule Phoenix.Websocket.RawHandler do
@moduledoc """
This module is a convenience for setting up a basic cowboy websocket.
@@ -7,11 +7,11 @@ defmodule Phoenix.Controller.Websocket do
Below is an example of an EchoServer websocket.
defmodule Router do
use Phoenix.Router
- websocket "echo", Websocket
+ raw_websocket "echo", Websocket
end
defmodule Websocket do
- use Phoenix.Controller.Websocket
+ use Phoenix.Websocket.RawHandler
def receive(conn, data, state) do
conn.send(data)
end
@@ -24,7 +24,7 @@ defmodule Phoenix.Controller.Websocket do
closed(data, conn, state)
Each function is an alias for the websocket handler functions, for more detailed information on
- connections and what you should return from these functions check out the cowboy websocket documentation.
+ connections and what you should/could return from these functions check out the cowboy websocket documentation.
http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_websocket_handler/
Things to keep in mind:
@@ -44,7 +44,7 @@ defmodule Phoenix.Controller.Websocket do
defrecord Socket, conn: nil, pid: nil
- import Phoenix.Controller.Websocket
+ import unquote(__MODULE__)
@doc false
def init({unquote(transport), :http}, req, opts) do
View
8 test/phoenix/websocket_test.exs → .../phoenix/websocket/raw_websocket_test.exs
@@ -1,21 +1,21 @@
defmodule Phoenix.Controller.WebsocketTest do
use ExUnit.Case, async: true
- alias Phoenix.Controller.Websocket
+ import Phoenix.Websocket.RawHandler
defrecord Socket, conn: nil, pid: nil
test "verify correct return from terminate" do
- Websocket.terminate(Socket.new(pid: self()))
+ terminate(Socket.new(pid: self()))
assert_received :shutdown
end
test "verify correct return from hibernate" do
- Websocket.hibernate(Socket.new(pid: self))
+ hibernate(Socket.new(pid: self))
assert_received :hibernate
end
test "verify basic reply" do
- Websocket.reply(Socket.new(pid: self), {:text, "hello"})
+ reply(Socket.new(pid: self), {:text, "hello"})
assert_received {:send, {:text, "hello"}, []}
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.