首页 文章

启动websocket应用程序时出错

提问于
浏览
0

我正在尝试运行牛仔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"}
    }
]}.

非常感谢帮助解决此错误 .

问候

1 回答

  • 1

    错误消息显示函数 erws_app:start/2 尝试调用函数 websocket_sup:start_link/0 但不存在此函数 .

    您已将管理程序模块命名为 erws_sup ,因此将 erws_app.erl 中的所有 websocket_sup:start_link() 替换为 erws_sup:start_link() .

相关问题