mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Convert the websocket example to a release
This commit is contained in:
parent
6a90d00cee
commit
834056402a
9 changed files with 35 additions and 62 deletions
14
examples/websocket/Makefile
Normal file
14
examples/websocket/Makefile
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
PROJECT = websocket
|
||||||
|
|
||||||
|
DEPS = cowboy
|
||||||
|
dep_cowboy = pkg://cowboy master
|
||||||
|
|
||||||
|
.PHONY: release clean-release
|
||||||
|
|
||||||
|
release: clean-release all
|
||||||
|
relx
|
||||||
|
|
||||||
|
clean-release:
|
||||||
|
rm -rf _rel
|
||||||
|
|
||||||
|
include ../../erlang.mk
|
|
@ -1,17 +1,19 @@
|
||||||
Cowboy websocket
|
Websocket example
|
||||||
================
|
=================
|
||||||
|
|
||||||
To compile this example you need rebar in your PATH.
|
To try this example, you need GNU `make`, `git` and
|
||||||
|
[relx](https://github.com/erlware/relx) in your PATH.
|
||||||
|
|
||||||
Type the following command:
|
To build the example, run the following command:
|
||||||
```
|
|
||||||
$ rebar get-deps compile
|
``` bash
|
||||||
|
$ make
|
||||||
```
|
```
|
||||||
|
|
||||||
You can then start the Erlang node with the following command:
|
To start the release in the foreground:
|
||||||
```
|
|
||||||
./start.sh
|
``` bash
|
||||||
|
$ ./_rel/bin/websocket_example console
|
||||||
```
|
```
|
||||||
|
|
||||||
Then point your browser to the indicated URL to open a websocket client.
|
Then point your browser at [http://localhost:8080](http://localhost:8080).
|
||||||
Not all browsers support websockets. It was tested with Chromium.
|
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
{deps, [
|
|
||||||
{cowboy, ".*",
|
|
||||||
{git, "git://github.com/extend/cowboy.git", "master"}},
|
|
||||||
{mimetypes, ".*",
|
|
||||||
{git, "git://github.com/spawngrid/mimetypes.git", "master"}}
|
|
||||||
]}.
|
|
2
examples/websocket/relx.config
Normal file
2
examples/websocket/relx.config
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
{release, {websocket_example, "1"}, [websocket]}.
|
||||||
|
{extended_start_script, true}.
|
|
@ -1,26 +0,0 @@
|
||||||
%% Feel free to use, reuse and abuse the code in this file.
|
|
||||||
|
|
||||||
-module(toppage_handler).
|
|
||||||
|
|
||||||
-export([init/3]).
|
|
||||||
-export([handle/2]).
|
|
||||||
-export([terminate/3]).
|
|
||||||
|
|
||||||
init(_Transport, Req, []) ->
|
|
||||||
{ok, Req, undefined}.
|
|
||||||
|
|
||||||
handle(Req, State) ->
|
|
||||||
Html = get_html(),
|
|
||||||
{ok, Req2} = cowboy_req:reply(200,
|
|
||||||
[{<<"content-type">>, <<"text/html">>}],
|
|
||||||
Html, Req),
|
|
||||||
{ok, Req2, State}.
|
|
||||||
|
|
||||||
terminate(_Reason, _Req, _State) ->
|
|
||||||
ok.
|
|
||||||
|
|
||||||
get_html() ->
|
|
||||||
{ok, Cwd} = file:get_cwd(),
|
|
||||||
Filename =filename:join([Cwd, "priv", "html_ws_client.html"]),
|
|
||||||
{ok, Binary} = file:read_file(Filename),
|
|
||||||
Binary.
|
|
|
@ -1,13 +0,0 @@
|
||||||
%% Feel free to use, reuse and abuse the code in this file.
|
|
||||||
|
|
||||||
-module(websocket).
|
|
||||||
|
|
||||||
%% API.
|
|
||||||
-export([start/0]).
|
|
||||||
|
|
||||||
start() ->
|
|
||||||
ok = application:start(crypto),
|
|
||||||
ok = application:start(cowlib),
|
|
||||||
ok = application:start(ranch),
|
|
||||||
ok = application:start(cowboy),
|
|
||||||
ok = application:start(websocket).
|
|
|
@ -12,11 +12,15 @@
|
||||||
start(_Type, _Args) ->
|
start(_Type, _Args) ->
|
||||||
Dispatch = cowboy_router:compile([
|
Dispatch = cowboy_router:compile([
|
||||||
{'_', [
|
{'_', [
|
||||||
{"/", toppage_handler, []},
|
{"/", cowboy_static, [
|
||||||
|
{directory, {priv_dir, websocket, []}},
|
||||||
|
{file, <<"index.html">>},
|
||||||
|
{mimetypes, [{<<".html">>, [<<"text/html">>]}]}
|
||||||
|
]},
|
||||||
{"/websocket", ws_handler, []},
|
{"/websocket", ws_handler, []},
|
||||||
{"/static/[...]", cowboy_static, [
|
{"/static/[...]", cowboy_static, [
|
||||||
{directory, {priv_dir, websocket, [<<"static">>]}},
|
{directory, {priv_dir, websocket, [<<"static">>]}},
|
||||||
{mimetypes, {fun mimetypes:path_to_mimes/2, default}}
|
{mimetypes, [{<<".js">>, [<<"application/javascript">>]}]}
|
||||||
]}
|
]}
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
erl -pa ebin deps/*/ebin -s websocket \
|
|
||||||
-eval "io:format(\"Point your browser at http://localhost:8080/ to use a simple websocket client~n\")."
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue