0

我正在尝试通过 websocket 连接返回使用 boss_db 获得的数据。在此示例中,我想返回我获取的问题,您可以看到日志打印出问题,但是有一些错误导致终止,原因是:返回值错误:好的。

以下是我的代码和错误:

websocket/fan_games_game_websocket.erl

-module(fan_games_game_websocket, [Req, SessionId]).

-behaviour(boss_service_handler).

-record(state,{users}).

%% API
-export([
  init/0, 
  handle_incoming/4, 
  handle_join/3,
  handle_broadcast/2,
  handle_close/4, 
  handle_info/2,
  terminate/2
]).

init() ->
  io:format("~p (~p) starting...~n", [?MODULE, self()]),
  %timer:send_interval(1000, ping),
  {ok, #state{users=dict:new()}}.

handle_join(ServiceName, WebSocketId, State) ->
    error_logger:info_msg("~p ~p ~p", [ServiceName, WebSocketId, SessionId]),
    #state{users=Users} = State,
    {noreply, #state{users=dict:store(WebSocketId, SessionId, Users)}}.

handle_close(Reason, ServiceName, WebSocketId, State) ->
    #state{users=Users} = State,
    io:format("ServiceName ~p, WebSocketId ~p, SessiondId ~p, close for Reason ~p~n",
              [ServiceName, WebSocketId, SessionId, Reason]),
    {noreply, #state{users=dict:erase(WebSocketId, Users)}}.

handle_broadcast(Message, State) ->
  io:format("Broadcast Message ~p~n",[Message]),
  {noreply, State}.

handle_incoming(_ServiceName, WebSocketId, Message, State) ->
    error_logger:info_msg(Message),
    Questions = boss_db:find(question, []),
    error_logger:info_msg("~p~n", [Questions]),
    WebSocketId ! {text, list_to_binary(Questions)},
    {noreply, State}.

handle_info(state, State) ->
    #state{users=Users} = State,
  error_logger:info_msg("state:~p~n", [Users]),
  {noreply, State};

handle_info(ping, State) ->
  error_logger:info_msg("pong:~p~n", [now()]),
  {noreply, State};

handle_info(tic_tac, State) ->
    #state{users=Users} = State,
      Fun = fun(X) when is_pid(X)-> X ! {text, "tic tac"} end,
      All = dict:fetch_keys(Users),
      [Fun(E) || E <- All],
  {noreply, State};

handle_info(_Info, State) ->
  {noreply, State}.

terminate(_Reason, _State) ->
  ok.


question.erl
-module(question, [Id, GameId, Text]).
-has({answers, many}).
-belongs_to(game).

更新

这是我更新的日志以及您的建议:

以下是提交“a”的示例请求的日志

11:14:25.401 [info] a
fan_games_game_websocket (<0.299.0>) starting...
11:14:25.401 [info] [{question,"question-2","game-2","Who will have the most rushing yards in the first quarter?"}]

11:14:25.402 [error] ** Boss Service Handler fan_games_game_websocket terminating in handle_incoming/4
   for the reason error:badarg
ServiceUrl: "/websocket/game"
WebSocketId: <0.285.0>
SessionId  : undefined
Message    : <<"a">>
State    : {state,{dict,0,16,16,8,80,48,{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},{{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}}}}
** Stacktrace: [{erlang,list_to_binary,[[{question,"question-2","game-2","Who will have the most rushing yards in the first quarter?"}]],[]},{fan_games_game_websocket,handle_incoming,5,[{file,"/Users/blanecordes/Documents/Code/erlang/fan_game/fan_games/src/websocket/fan_games_game_websocket.erl"},{line,42}]},{boss_service_worker,handle_cast,2,[{file,"src/boss/boss_service_worker.erl"},{line,173}]},{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,604}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]


11:14:25.402 [error] gen_server fan_games_game_websocket terminated with reason: bad return value: ok
11:14:25.402 [error] CRASH REPORT Process <0.297.0> with 0 neighbours exited with reason: bad return value: ok in gen_server:terminate/6 line 744
11:14:25.402 [error] Supervisor {global,boss_service_sup} had child fan_games_game_websocket started with boss_service_worker:start_link(fan_games_game_websocket, <<"/websocket/game">>) at <0.297.0> exit with reason bad return value: ok in context child_terminated
4

1 回答 1

3

我相信问题出在这条线上

WebSocketId ! {text, <<Questions>>},

in handle_incoming/4,因为<<Questions>>不是正确的二进制文件。尝试将其更改为:

WebSocketId ! {text, list_to_binary(Questions)},

反而。

于 2014-11-26T21:26:32.940 回答