Created
March 20, 2012 20:07
-
-
Save denen99/2140767 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% snippet from pubsub_srv.erl | |
%----------- | |
start_link() -> | |
{ok,PID} = eredis_sub:start_link(), | |
register(eredispid,PID), | |
_Receiver = spawn_link(fun () -> | |
eredis_sub:controlling_process(PID), | |
receiver(PID) | |
end), | |
io:format("Eredis server started~n"), | |
{ok, PID}. | |
receiver(Sub) -> | |
receive | |
Msg -> | |
io:format("received ~p~n", [Msg]), | |
eredis_sub:ack_message(Sub), | |
?MODULE:receiver(Sub) | |
end. | |
subscribe_channel(Channel) -> | |
io:format("received request to subscribe to ~p~n",[Channel]), | |
Sub = whereis(eredispid), | |
io:format("Found PID ~p for subscription~n",[Sub]), | |
eredis_sub:subscribe(Sub, [Channel]). | |
%snippet from cowboy_callback.erl | |
%---------------------------------- | |
handle(Req,State) -> | |
case cowboy_http_req:qs_val(<<"channel">>,Req) of | |
{undefined,_Req2} -> | |
io:format("Sorry missing channel query param"), | |
{ok, Req3} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}],<<"Sorry, invalid channel">>,Req); | |
{Channel,_Req2} -> | |
io:format("Subscribing to channel ~p~n",[Channel]), | |
erltest1_pubsub_srv:subscribe_channel(Channel), | |
{ok, Req3} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}],<<"You subscribed to a channel!">>,Req); | |
_ -> | |
io:format("Dont know what to do~n"), | |
{ok, Req3} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}],<<"Sorry dont know what to do">>,Req) | |
end, | |
{ok, Req3, State}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment