Phoenix.Endpoint.CowboyHandler

Phoenix.Endpoint.CowboyHandler

The Cowboy adapter for Phoenix.

It implements the required child_spec/3 function as well as the handler for the WebSocket transport.

Custom dispatch options

NOTE: This feature depends on the internals of Cowboy 1.0 API and how it integrates with Phoenix. Those may change any time, without backwards compatibility, specially when Cowboy 2.0 is out. We document this for clarity and for those with very special needs.

You can provide custom dispatch options in order to use Phoenix’s builtin Cowboy server with custom handlers. For example, to handle raw WebSockets as shown in Cowboy’s docs).

The options are passed to both :http and :https keys in the endpoint configuration. However, once you pass your custom dispatch options, you will need to manually wire all Phoenix endpoints, including the socket transports.

You will need the following rules:

  • Per websocket transport:

    {"/socket/websocket", Phoenix.Endpoint.CowboyWebSocket,
      {Phoenix.Transports.WebSocket,
        {MyApp.Endpoint, MyApp.UserSocket, :websocket}}}
  • Per longpoll transport:

    {"/socket/long_poll", Plug.Adapters.Cowboy.Handler,
      {Phoenix.Transports.LongPoll,
        {MyApp.Endpoint, MyApp.UserSocket, :longpoll}}}
  • For the endpoint:

    {:_, Plug.Adapters.Cowboy.Handler, {MyApp.Endpoint, []}}

For example:

config :myapp, MyApp.Endpoint,
  http: [dispatch: [
          {:_, [
              {"/foo", MyApp.CustomHandler, []},
              {"/bar", MyApp.AnotherHandler, []},
              {:_, Plug.Adapters.Cowboy.Handler, {MyApp.Endpoint, []}}
            ]}]]

It is also important to specify your handlers first, otherwise Phoenix will intercept the requests before they get to your handler.

Summary

Functions

child_spec(scheme, endpoint, config)

Generates a childspec to be used in the supervision tree

start_link(scheme, endpoint, arg)

Callback to start the Cowboy endpoint

Functions

child_spec(scheme, endpoint, config)

Generates a childspec to be used in the supervision tree.

start_link(scheme, endpoint, arg)

Callback to start the Cowboy endpoint.

© 2014 Chris McCord
Licensed under the MIT License.
https://hexdocs.pm/phoenix/Phoenix.Endpoint.CowboyHandler.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部