From 07b717104b6d97650dcbb895b38093ae2760a531 Mon Sep 17 00:00:00 2001 From: Feng Date: Wed, 26 Aug 2015 21:47:21 +0800 Subject: [PATCH] emqttd_ws_client:subscribe/2 api --- src/emqttd_ws_client.erl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/emqttd_ws_client.erl b/src/emqttd_ws_client.erl index 87f0263dd..6b4a001b2 100644 --- a/src/emqttd_ws_client.erl +++ b/src/emqttd_ws_client.erl @@ -34,7 +34,7 @@ -include("emqttd_protocol.hrl"). %% API Exports --export([start_link/1, ws_loop/3]). +-export([start_link/1, ws_loop/3, subscribe/2]). -behaviour(gen_server). @@ -61,6 +61,9 @@ start_link(Req) -> packet_opts = PktOpts, parser = emqttd_parser:new(PktOpts)}). +subscribe(CPid, TopicTable) -> + gen_server:cast(CPid, {subscribe, TopicTable}). + %%------------------------------------------------------------------------------ %% @private %% @doc Start WebSocket client. @@ -112,6 +115,10 @@ init([WsPid, Req, ReplyChannel, PktOpts]) -> handle_call(_Req, _From, State) -> {reply, error, State}. +handle_cast({subscribe, TopicTable}, State = #client_state{proto_state = ProtoState}) -> + {ok, ProtoState1} = emqttd_protocol:handle({subscribe, TopicTable}, ProtoState), + {noreply, State#client_state{proto_state = ProtoState1}, hibernate}; + handle_cast({received, Packet}, State = #client_state{proto_state = ProtoState}) -> case emqttd_protocol:received(Packet, ProtoState) of {ok, ProtoState1} -> @@ -136,10 +143,6 @@ handle_info({redeliver, {?PUBREL, PacketId}}, State = #client_state{proto_state {ok, ProtoState1} = emqttd_protocol:redeliver({?PUBREL, PacketId}, ProtoState), {noreply, State#client_state{proto_state = ProtoState1}}; -handle_info({subscribe, TopicTable}, State = #client_state{proto_state = ProtoState}) -> - {ok, ProtoState1} = emqttd_protocol:handle({subscribe, TopicTable}, ProtoState), - {noreply, State#client_state{proto_state = ProtoState1}}; - handle_info({stop, duplicate_id, _NewPid}, State = #client_state{proto_state = ProtoState}) -> lager:error("Shutdown for duplicate clientid: ~s", [emqttd_protocol:clientid(ProtoState)]), stop({shutdown, duplicate_id}, State);