Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
add start functions, extend documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bwegh committed Jul 26, 2015
1 parent 5a93131 commit 85732a7
Show file tree
Hide file tree
Showing 8 changed files with 517 additions and 497 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -7,6 +7,7 @@ COMPILE_FIRST = erwa_middleware.erl

DEPS = cowboy ranch wamper
dep_cowboy = git https://github.com/ninenines/cowboy.git master
dep_ranch = git https://github.com/ninenines/ranch.git master
dep_wamper = git https://github.com/bwegh/wamper master


Expand Down
21 changes: 8 additions & 13 deletions README.md
Expand Up @@ -56,8 +56,8 @@ Erwa has the following features:

Router
======
The router implementation in Erwa uses the great [ranch](https://github.com/extend/ranch)
and [cowboy](https://github.com/extend/cowboy) from [Loïc Hoguin (essen)](https://github.com/essen)
The router implementation in Erwa uses the great [ranch](https://github.com/ninenines/ranch)
and [cowboy](https://github.com/ninenines/cowboy) from [Loïc Hoguin (essen)](https://github.com/essen)
to handle the incomming connections and the webserver part for the websockets.
Erwa has two modules to work either as a protocol for ranch on incomming TCP connections, or
as websocket handler with cowboy on incomming websocket connections.
Expand All @@ -66,25 +66,20 @@ All you need to do to get a simple WAMP router up and running is to add a dispat
ranch and/or cowboy:

A WAMP router on websockets:
* using erwa_in_handler as the websocket handler, by dispatching a certain path to conditions
* starting cowboy on a certain port (here 8080) and add the dispatch rule
```Erlang
%% a rule to dispatch incomming connections to any host with the path /wamp to the erwa_in_handler
Dispatch = cowboy_router:compile([ {'_', [ {"/wamp", erwa_in_handler, []}, ]} ]),
%% fire up cowboy with the dispatch rule for the wamp connection
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],[{env, [{dispatch, Dispatch}]}]),
%% start erwa to handle any incomming connections to any host at the path /wamp
%% start it with 100 parallel acceptors on port 8080
ok = erwa:start_websocket("/wamp", 8080, 100).
```
In the examples directory you can find the simple_router which includes just the above
and starts a WAMP router, including a simple javascript client,
using [wampy.js](https://github.com/KSDaemon/wampy.js).

The other possibility is to start Erwa as a TCP router:
Erwa implements a protocol for ranch in the erwa_in_handler modules.
So starting and tcp router is done by starting ranch with
erwa_in_handler as the protocol:
```Erlang
%% start ranch with the wamp protocol by using erwa_in_handler on port 555
{ok,_} = ranch:start_listener(erwa_tcp, 5, ranch_tcp, [{port,5555}], erwa_in_handler, []),
%% start erwa listening for raw tcp connections on port 5555
%% starting it with 5 parallel acceptors
ok = erwa:start_socket(5555,5).
```
This is also included in the simple_router example in the examples directory.

Expand Down
1 change: 1 addition & 0 deletions src/erwa.app.src
Expand Up @@ -35,6 +35,7 @@
kernel,
stdlib,
wamper,
mnesia,
crypto
]},
{mod, {erwa_app, []}},
Expand Down
30 changes: 28 additions & 2 deletions src/erwa.erl
Expand Up @@ -37,8 +37,10 @@
-export([get_routing_for_realm/1]).



-export([get_version/0]).
-export([start_websocket/3]).
-export([start_websocket/4]).
-export([start_socket/2]).


%% @doc returns the version string for the application, used as agent description
Expand All @@ -50,7 +52,31 @@ get_version() ->
end,
<< <<"Erwa-">>/binary, Ver/binary >>.


%% @doc start router listening on websocket
-spec start_websocket( Path :: string(), Port :: integer(), Acceptors ::
integer() ) -> ok.
start_websocket(Path, Port, Acceptors) ->
start_websocket(Path, Port, Acceptors, []).

%% @doc start router listening on websocket with custom handlers added
-spec start_websocket( Path :: string(), Port :: integer(), Acceptors ::
non_neg_integer(), Handlers :: [term()] ) -> ok.
start_websocket(Path, Port, Acceptors, Handlers) ->
Dispatch = cowboy_router:compile([{'_',[ {Path, erwa_in_ws,
[] } | Handlers] }]),
{ok, _} = cowboy:start_http(erwa_http, Acceptors, [{port, Port}], [{env,
[{dispatch,
Dispatch}]}]),
ok.


%% @doc start the router listening for raw tcp connections
-spec start_socket(Port :: non_neg_integer(), Acceptors :: non_neg_integer()) ->
ok.
start_socket(Port, Acceptors) ->
{ok, _} = ranch:start_listener(erwa_tcp, Acceptors, ranch_tcp, [{port, Port}],
erwa_in_tcp, []),
ok.


%% for router
Expand Down

0 comments on commit 85732a7

Please sign in to comment.