Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2013 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4567230 to your computer and use it in GitHub Desktop.
Save anonymous/4567230 to your computer and use it in GitHub Desktop.
-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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment