public
anonymous / gist:4567230
Created

  • Download Gist
gistfile1.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
-module(test).
 
-behaviour(gen_server).
-export([start_link/3]).
%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
 
-record(state, {
% campfire room id
room = 0,
% campfire api token
token = <<>>,
% campfire sub_domain
domain = <<>>,
% request id
req_id = null
}).
 
start_link(Room, Token, Domain) ->
gen_server:start_link(?MODULE, [Room, Token, Domain], []).
init([Room, Token, Domain]) ->
% Start http stream
gen_server:cast(self(), stream),
% init state
{ok, #state{room = Room, token = Token, domain = Domain}}.
handle_call(_Request, _From, State) ->
{reply, ignored, State}.
 
handle_cast(stream, State) ->
% First of all leave from campfire room
ok = leave_room(State#state.domain, State#state.room, State#state.token),
% Now try to join to the room
ok = join_room(State#state.domain, State#state.room, State#state.token),
% Make url for stream
Url = "https://streaming.campfirenow.com/room/" ++ integer_to_list(State#state.room) ++ "/live.json",
% Start campfire stream
{_, ReqId} = ibrowse:send_req(Url, [{"Content-Type", "application/json"}], get,
[], [{basic_auth, {binary_to_list(State#state.token), "x"}}, {stream_to, {self(), once}}]),
{noreply, State#state{req_id = ReqId}};
 
handle_cast(Msg, State) ->
{noreply, State}.
 
handle_info({ibrowse_async_headers, ReqId, _Status, _Headers}, State) ->
% Next stream
ibrowse:stream_next(ReqId),
% return
{noreply, State};
 
%% @doc
handle_info({ibrowse_async_headers, ReqId, Body}, State) ->
ibrowse:stream_next(ReqId),
{noreply, State};
 
%% @doc Timeout request error
handle_info({ibrowse_async_response, _OldReqId, {error, req_timedout}}, State) ->
% Make url for new Stream
Url = "https://streaming.campfirenow.com/room/" ++ integer_to_list(State#state.room) ++ "/live.json",
% Start new stream
{_, ReqId} = ibrowse:send_req(Url, [{"Content-Type", "application/json"}], get,
[], [{basic_auth, {binary_to_list(State#state.token), "x"}}, {stream_to, {self(), once}}]),
% Save new request id
{noreply, State#state{req_id = ReqId}};
 
%% @doc
handle_info({ibrowse_async_response, ReqId, Data}, State) ->
io:format("Data ~p~n", [Data]),
ok = ibrowse:stream_next(ReqId),
% Parse response
case Data of
" " ->
{noreply, State};
_ ->
% Return state
{noreply, State}
end;
 
handle_info({ibrowse_async_response_end, ReqId}, State) ->
{noreply, State};
 
%% @doc connection timeout
handle_info({error, {conn_failed, {error, _}}}, State) ->
{noreply, State};
 
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% Internal functions
leave_room(Domain, Room, Token) ->
% Make url
Url = "https://" ++ binary_to_list(Domain) ++ ".campfirenow.com/room/" ++ integer_to_list(Room) ++ "/leave.json",
% Make content type
ContentType = [{"Content-Type", "application/json"}],
% send leave request
ibrowse:send_req(Url, ContentType, post, [], [{basic_auth, {binary_to_list(Token), "x"}}]),
% return
ok.
 
%% @doc Join to room
join_room(Domain, Room, Token) ->
% Make url
Url = "https://" ++ binary_to_list(Domain) ++ ".campfirenow.com/room/" ++ integer_to_list(Room) ++ "/join.json",
% Make content type
ContentType = [{"Content-Type", "application/json"}],
% Send join request
ibrowse:send_req(Url, ContentType, post, [], [{basic_auth, {binary_to_list(Token), "x"}}]),
% return
ok.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.