我正在尝试运行牛仔 Web 套接字示例,但我一直遇到错误:
{error,
{bad_return,
{{erws_app,start,[normal,[]]},
{'EXIT',
{undef,
[{websocket_sup,start_link,[],[]},
{application_master,start_it_old,4,
[{file,"application_master.erl"},{line,269}]}]}}}}}
我用来启动应用程序的 erlang 命令:erl -pa ebin/ -pa deps/*/ebin
然后我使用 application:start(app) 启动所需的依赖应用程序。
我使用 rebar 来编译应用程序
我正在使用的代码:
erws_app.erl
-module(erws_app).
-behaviour(application).
%% Application callbacks
-export([start/2, stop/1, start_link/2]).
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/", cowboy_static, {priv_file, websocket, "index.html"}},
{"/websocket", ws_handler, []},
{"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
[{env, [{dispatch, Dispatch}]}]),
websocket_sup:start_link().
start_link(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/", cowboy_static, {priv_file, websocket, "index.html"}},
{"/websocket", ws_handler, []},
{"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
[{env, [{dispatch, Dispatch}]}]),
websocket_sup:start_link().
stop(_State) ->
ok.
erws_handler.erl
-module(erws_handler).
-export([init/3]).
-export([websocket_init/3]).
-export([websocket_handle/3]).
-export([websocket_info/3]).
-export([websocket_terminate/3]).
init({tcp, http}, _Req, _Opts) ->
{upgrade, protocol, cowboy_websocket}.
websocket_init(_TransportName, Req, _Opts) ->
erlang:start_timer(1000, self(), <<"Hello!">>),
{ok, Req, undefined_state}.
websocket_handle({text, Msg}, Req, State) ->
{reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
websocket_handle(_Data, Req, State) ->
{ok, Req, State}.
websocket_info({timeout, _Ref, Msg}, Req, State) ->
erlang:start_timer(1000, self(), <<"How' you doin'?">>),
{reply, {text, Msg}, Req, State};
websocket_info(_Info, Req, State) ->
{ok, Req, State}.
websocket_terminate(_Reason, _Req, _State) ->
ok.
erws_sup.erl
-module(erws_sup).
-behaviour(supervisor).
%% API.
-export([start_link/0]).
%% supervisor.
-export([init/1]).
%% API.
-spec start_link() -> {ok, pid()}.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% supervisor.
init([]) ->
Procs = [],
{ok, {{one_for_one, 10, 10}, Procs}}.
erws.app.src
{application, erws, [
{description, ""},
{vsn, "1"},
{registered, []},
{applications, [kernel,stdlib,crypto,cowboy,compiler,lager,syntax_tools]},
{mod, { erws_app, []}},
{env, []}
]}.
rebar.config
{erl_opts, [{parse_transform, lager_transform}]}.
{lib_dirs,["deps"]}.
{sub_dirs, ["rel"]}.
{deps, [
{'lager', ".*", {
git, "https://github.com/basho/lager.git", "2.0.2"}
},
{'ranch', ".*", {
git, "https://github.com/extend/ranch.git", "0.9.0"}
},
{'cowlib', ".*", {
git, "https://github.com/extend/cowlib.git", "0.4.0"}
},
{'cowboy', ".*", {
git, "https://github.com/extend/cowboy.git", "0.9.0"}
}
]}.
非常感谢帮助解决此错误。
问候