我如何通过websocket从mnesia发送数据

时间:2014-01-15 14:32:00

标签: websocket erlang mnesia yaws

我有桌面艺术家的mnesia DB:

(gw@gw)227> lookup:for_test().
{atomic,["Baltic Baroque","Anna Luca","Karel Boehlee Trio",
     "Bill Evans","Dino Saluzzi and  Anja Lechner",
     "Bill Evans Trio with Stan Getz","Duke Pearson",
     "The John Butler Trio"]}
(gw@gw)228>

我想通过YAWS中的websocket将此列表发送给客户端,但我该怎么做呢?我心烦意乱......没有任何工作。请通过任何信息提供帮助。

祝你好运!

2 个答案:

答案 0 :(得分:0)

你必须将这个字符串列表转换为二进制文件,并且传递是通过web套接字,顺便说一下你在接收端做什么样的数据处理我的意思是你想把它作为JSON发送或者只是逗号分隔值?

答案 1 :(得分:0)

Yeeeees ......我解决了我的问题! 我的YAWS手柄

handle_message({text, <<"q2">>}) ->
Var555 = unicode:characters_to_binary(my_json4:handle()),
{reply, {text, <<Var555/binary>>}};

我的mnesia选择+转换为JSON。 my_json4.erl

Data = [{obj,
         [{art_id, Line#tmp.artist_id},
          {art,    unicode:characters_to_binary(Line#tmp.artist)},
          {alb_id, Line#tmp.album_id},
          {alb,    unicode:characters_to_binary(Line#tmp.album)},
          {path,   unicode:characters_to_binary(Line#tmp.albumpath)},
          {image,  unicode:characters_to_binary(Line#tmp.image)},
          {tracks, [unicode:characters_to_binary(X) ||X <- Line#tmp.tracks]}]}
        || Line <- Lines],
JsonData = {obj, [{data, Data}]},
rfc4627:encode(JsonData).

handle() ->
 QHandle = qlc:q( [ X ||
   X <- mnesia:table(artists),
   X#artists.artist_id == 2]
 ),
 Records = do(qlc:q([{tmp, X#artists.artist_id, X#artists.artist, A#albums.album_id, A#albums.album, A#albums.albumpath, A#albums.image, A#albums.tracklist} ||
    X <- QHandle,
    A <- mnesia:table(albums),
    A#albums.artist_id == X#artists.artist_id])
),
Json = convert_to_json(Records),
Json.


do(Q) ->
F = fun() ->
            qlc:e(Q)
    end,
{atomic, Value} = mnesia:transaction(F),
Value.
相关问题