首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在芝加哥老板中通过websocket返回boss_db数据时出错

在芝加哥老板中通过websocket返回boss_db数据时出错
EN

Stack Overflow用户
提问于 2014-11-27 05:01:22
回答 1查看 179关注 0票数 0

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

下面是我的代码和错误:

代码语言:javascript
复制
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“的示例请求的日志

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-11-27 05:26:33

我相信问题出在线路上。

代码语言:javascript
复制
WebSocketId ! {text, <<Questions>>},

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

代码语言:javascript
复制
WebSocketId ! {text, list_to_binary(Questions)},

而不是。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27158794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档