Skip to content

Commit c485e9c

Browse files
committed
replace erlang cowboy ws server with elixir equivalent
1 parent 03e2f6a commit c485e9c

7 files changed

Lines changed: 57 additions & 44 deletions

File tree

mix.exs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ defmodule MintWebSocket.MixProject do
1515
version: @version,
1616
elixir: "~> 1.8",
1717
elixirc_paths: elixirc_paths(Mix.env()),
18-
erlc_paths: erlc_paths(Mix.env()),
1918
start_permanent: Mix.env() == :prod,
2019
deps: deps(),
2120
test_coverage: [tool: ExCoveralls],
@@ -44,6 +43,11 @@ defmodule MintWebSocket.MixProject do
4443
defp deps do
4544
[
4645
{:mint, "~> 1.0"},
46+
# for running the :http2 tests (mix test --include http2)
47+
# {:mint,
48+
# git: "https://github.com/elixir-mint/mint.git",
49+
# ref: "488a6ba5fd418a52f697a8d5f377c629ea96af92",
50+
# override: true},
4751
{:ex_doc, "~> 0.24", only: [:dev], runtime: false},
4852
{:castore, ">= 0.0.0", only: [:dev]},
4953
{:jason, ">= 0.0.0", only: [:dev, :test]},
@@ -57,9 +61,6 @@ defmodule MintWebSocket.MixProject do
5761
defp elixirc_paths(:test), do: ["lib", "test/fixtures"]
5862
defp elixirc_paths(_), do: ["lib"]
5963

60-
defp erlc_paths(:test), do: ["src", "test/fixtures"]
61-
defp erlc_paths(_), do: ["src"]
62-
6364
defp package do
6465
[
6566
name: "mint_web_socket",

test/fixtures/h2cowboy/websocket_sup.erl

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/fixtures/h2cowboy/ws_h.erl

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/fixtures/websocket_handler.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule WebsocketHandler do
2+
@moduledoc """
3+
An example websocket handler for cowboy with compression enabled
4+
"""
5+
6+
@behaviour :cowboy_websocket
7+
8+
@impl :cowboy_websocket
9+
def init(req, state) do
10+
{:cowboy_websocket, req, state, %{compress: true}}
11+
end
12+
13+
@impl :cowboy_websocket
14+
def websocket_init(state), do: {[], state}
15+
16+
@impl :cowboy_websocket
17+
def websocket_handle({:text, msg}, state), do: {[text: msg], state}
18+
19+
def websocket_handle(_data, state), do: {[], state}
20+
21+
@impl :cowboy_websocket
22+
def websocket_info(_info, state), do: {[], state}
23+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule WebsocketSupervisor do
2+
@moduledoc """
3+
A supervisor for the WebsocketHandler
4+
"""
5+
6+
use Supervisor
7+
8+
def start_link(_opts) do
9+
dispatch =
10+
:cowboy_router.compile([
11+
{:_,
12+
[
13+
{'/', WebsocketHandler, []}
14+
]}
15+
])
16+
17+
{:ok, _} =
18+
:cowboy.start_clear(:http, [port: 7070], %{
19+
env: %{dispatch: dispatch},
20+
enable_connect_protocol: true
21+
})
22+
23+
Supervisor.start_link([], strategy: :one_for_one)
24+
end
25+
26+
def init(_opts), do: {:ok, nil}
27+
end

test/mint/web_socket_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ defmodule Mint.WebSocketTest do
5151

5252
describe "given an HTTP/2 connection to an echo server" do
5353
setup do
54+
start_supervised!(WebsocketSupervisor)
55+
5456
{:ok, conn} = Mint.HTTP.connect(:http, "localhost", 7070, protocols: [:http2])
5557

5658
[conn: conn]

test/test_helper.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
:websocket_sup.start_link()
21
ExUnit.configure(assert_receive_timeout: 500, exclude: [:http2, compression: :stress])
32
ExUnit.start()

0 commit comments

Comments
 (0)