fix ws client

This commit is contained in:
anlicheng 2025-09-19 21:07:31 +08:00
parent 1411d093da
commit d5e60c5109
3 changed files with 22 additions and 11 deletions

View File

@ -8,11 +8,3 @@
%%%-------------------------------------------------------------------
-author("anlicheng").
%%
-define(PACKET_REQUEST, 16#01).
%%
-define(PACKET_RESPONSE, 16#02).
%%
-define(PACKET_PUSH, 16#03).
-define(PACKET_PUB, 16#04).

View File

@ -28,6 +28,15 @@ start_link() ->
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
ChildSpecs = [
#{
id => 'efka_service_sup',
start => {'efka_service_sup', start_link, []},
restart => permanent,
shutdown => 2000,
type => supervisor,
modules => ['efka_service_sup']
},
#{
id => 'docker_events',
start => {'docker_events', start_link, []},

View File

@ -17,6 +17,15 @@
%%
-define(PENDING_TIMEOUT, 10 * 1000).
%%
-define(PACKET_REQUEST, 16#01).
%%
-define(PACKET_RESPONSE, 16#02).
%%
-define(PACKET_PUSH, 16#03).
-define(PACKET_PUB, 16#04).
-record(state, {
service_pid :: undefined | pid(),
is_registered = false :: boolean()
@ -36,6 +45,7 @@ websocket_init(_State) ->
websocket_handle({binary, <<?PACKET_REQUEST, Data/binary>>}, State) ->
Request = jiffy:decode(Data, [return_maps]),
lager:debug("[ws_channle] get request: ~p", [Request]),
handle_request(Request, State);
websocket_handle(Info, State) ->
@ -50,7 +60,7 @@ websocket_info({topic_broadcast, Topic, Content}, State = #state{}) ->
%% service进程关闭
websocket_info({'DOWN', _Ref, process, ServicePid, Reason}, State = #state{service_pid = ServicePid}) ->
lager:debug("[tcp_channel] container_pid: ~p, exited: ~p", [ServicePid, Reason]),
lager:debug("[ws_channel] container_pid: ~p, exited: ~p", [ServicePid, Reason]),
{stop, State#state{service_pid = undefined}};
websocket_info({timeout, _, {stop, Reason}}, State) ->
@ -102,12 +112,12 @@ handle_request(#{<<"id">> := Id, <<"method">> := <<"subscribe">>, <<"params">> :
%%
handle_request(#{<<"id">> := 0, <<"method">> := <<"metric_data">>,
<<"params">> := #{<<"device_uuid">> := DeviceUUID, <<"route_key">> := RouteKey, <<"metric">> := Metric}}, State = #state{service_pid = ServicePid, is_registered = true}) ->
efka_container:metric_data(ServicePid, DeviceUUID, RouteKey, Metric),
efka_service:metric_data(ServicePid, DeviceUUID, RouteKey, Metric),
{ok, State};
%% Event事件
handle_request(#{<<"id">> := 0, <<"method">> := <<"event">>, <<"params">> := #{<<"event_type">> := EventType, <<"body">> := Body}}, State = #state{service_pid = ServicePid, is_registered = true}) ->
efka_container:send_event(ServicePid, EventType, Body),
efka_service:send_event(ServicePid, EventType, Body),
{ok, State}.
-spec json_result(Id :: integer(), Result :: term()) -> binary().