Skip to content

Instantly share code, notes, and snippets.

@jcfrank
Last active November 3, 2022 07:15
Show Gist options
  • Save jcfrank/8f9356b2fed1dd3be2c1 to your computer and use it in GitHub Desktop.
Save jcfrank/8f9356b2fed1dd3be2c1 to your computer and use it in GitHub Desktop.

Revisions

  1. jcfrank revised this gist Jan 13, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions erlang-websocket-sample.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ First, create a folder for project, ex. myproject.
    `$ cd myproject/`
    Copy erlang.mk into it.
    `$ cp /my/erlang.mk/folder/erlang.mk .`
    `$ make -f erlang.mk bootstrap`
    `$ make -f erlang.mk bootstrap bootstrap-rel`
    Edit generated Makefile.
    ```makefile
    PROJECT = myproject
    @@ -65,7 +65,6 @@ websocket_handle(_Frame, Req, State) ->

    Run
    ---
    `$ make bootstrap-rel`
    `$ make`
    `$ cd _rel/myproject_release`
    `$ ./bin/myproject_release console`
  2. jcfrank revised this gist Jan 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion erlang-websocket-sample.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ This is a sample websocket app.
    new project
    -----------
    Use erlang.mk. Not that you have to, but it'd make things a bit easier.
    First, create a folder for project, ex. myproject.
    First, create a folder for project, ex. myproject.
    `$ cd myproject/`
    Copy erlang.mk into it.
    `$ cp /my/erlang.mk/folder/erlang.mk .`
  3. jcfrank revised this gist Jan 13, 2015. 1 changed file with 36 additions and 1 deletion.
    37 changes: 36 additions & 1 deletion erlang-websocket-sample.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,33 @@ Simple Cowboy websocket app
    Cowboy is such a simple web framework.
    This is a sample websocket app.

    new project
    -----------
    Use erlang.mk. Not that you have to, but it'd make things a bit easier.
    First, create a folder for project, ex. myproject.
    `$ cd myproject/`
    Copy erlang.mk into it.
    `$ cp /my/erlang.mk/folder/erlang.mk .`
    `$ make -f erlang.mk bootstrap`
    Edit generated Makefile.
    ```makefile
    PROJECT = myproject
    DEPS = cowboy
    include erlang.mk
    ```
    Edit generated src/myproject.app.src
    ```
    ...
    {applications: [
    kernel,
    stdlib,
    cowboy
    ]},
    ...
    ```
    Run make once, cowboy should be pulled to deps/ folder.
    `$ make`
    Then, we start adding cowboy code to main app file.
    myproject_app.erl
    -----------------
    Setup routing, direct "/websocket" to ws_handler.
    @@ -20,7 +47,8 @@ start(_Type, _Args) ->
    ),
    cowboy_websocket_sup:start_link().
    ```

    We route /websocket to ws_handler, now have make copy that websocket handler template.
    `$ make new t=cowboy_ws n=ws_handler`
    ws_handler.erl
    --------------
    Basically the template is good enough.
    @@ -34,3 +62,10 @@ websocket_handle({binary, Data}, Req, State) ->
    websocket_handle(_Frame, Req, State) ->
    {ok, Req, State}.
    ```

    Run
    ---
    `$ make bootstrap-rel`
    `$ make`
    `$ cd _rel/myproject_release`
    `$ ./bin/myproject_release console`
  4. jcfrank revised this gist Jan 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion erlang-websocket-sample.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Simple Cowboy websocket app
    ===========================
    Cow boy is such a simple web framework.
    Cowboy is such a simple web framework.
    This is a sample websocket app.

    myproject_app.erl
  5. jcfrank created this gist Jan 11, 2015.
    36 changes: 36 additions & 0 deletions erlang-websocket-sample.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    Simple Cowboy websocket app
    ===========================
    Cow boy is such a simple web framework.
    This is a sample websocket app.

    myproject_app.erl
    -----------------
    Setup routing, direct "/websocket" to ws_handler.
    ```erlang
    start(_Type, _Args) ->
    Dispatch = cowboy_router:compile([
    {'_', [
    {"/", cowboy_static, {priv_file, cowboy_websocket, "index.html"}},
    {"/static/[...]", cowboy_static, {priv_dir, cowboy_websocket, "static"}},
    {"/websocket", ws_handler, []}
    ]}
    ]),
    {ok, _} = cowboy:start_http(ws_listener, 100, [{port, 8080}],
    [{env, [{dispatch, Dispatch}]}]
    ),
    cowboy_websocket_sup:start_link().
    ```

    ws_handler.erl
    --------------
    Basically the template is good enough.
    ```erlang
    websocket_handle({text, Data}, Req, State) ->
    io:format("received: ~w~n", [Data]),
    {reply, {text, Data}, Req, State};
    websocket_handle({binary, Data}, Req, State) ->
    io:format("received: ~s~n", [Data]),
    {reply, {binary, Data}, Req, State};
    websocket_handle(_Frame, Req, State) ->
    {ok, Req, State}.
    ```