fix channel

This commit is contained in:
anlicheng 2026-04-20 20:16:46 +08:00
parent 1bb9c33ded
commit d32cd25f43
2 changed files with 13 additions and 13 deletions

View File

@ -31,7 +31,7 @@ start_http_server() ->
Dispatcher = cowboy_router:compile([ Dispatcher = cowboy_router:compile([
{'_', [ {'_', [
{"/ws", ws_channel, []}, {"/ws", service_channel, []},
{"/files/[...]", cowboy_static, {dir, "/usr/local/code/downloads"}}, {"/files/[...]", cowboy_static, {dir, "/usr/local/code/downloads"}},
{"/upload", upload_channel, []} {"/upload", upload_channel, []}
]} ]}

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
%%% Created : 11. 1 2021 12:17 %%% Created : 11. 1 2021 12:17
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(ws_channel). -module(service_channel).
-author("licheng5"). -author("licheng5").
-include("efka_tables.hrl"). -include("efka_tables.hrl").
-include("service_pb.hrl"). -include("service_pb.hrl").
@ -36,7 +36,7 @@ init(Req, Opts) ->
{cowboy_websocket, Req, Opts}. {cowboy_websocket, Req, Opts}.
websocket_init(_State) -> websocket_init(_State) ->
logger:debug("[ws_channel] get a new connection"), logger:debug("[service_channel] get a new connection"),
%% true %% true
{ok, #state{}}. {ok, #state{}}.
@ -45,15 +45,15 @@ websocket_handle(ping, State) ->
websocket_handle({binary, <<?FRAME_REQUEST, PacketBin/binary>>}, State) -> websocket_handle({binary, <<?FRAME_REQUEST, PacketBin/binary>>}, State) ->
Request = service_pb:decode_msg(PacketBin, 'ServiceRequest'), Request = service_pb:decode_msg(PacketBin, 'ServiceRequest'),
logger:debug("[ws_channel] get request: ~p", [Request]), logger:debug("[service_channel] get request: ~p", [Request]),
handle_request(Request, State); handle_request(Request, State);
websocket_handle({binary, <<?FRAME_CAST, PacketBin/binary>>}, State) -> websocket_handle({binary, <<?FRAME_CAST, PacketBin/binary>>}, State) ->
Cast = service_pb:decode_msg(PacketBin, 'ServiceCast'), Cast = service_pb:decode_msg(PacketBin, 'ServiceCast'),
logger:debug("[ws_channel] get cast: ~p", [Cast]), logger:debug("[service_channel] get cast: ~p", [Cast]),
handle_cast(Cast, State); handle_cast(Cast, State);
websocket_handle(Info, State) -> websocket_handle(Info, State) ->
logger:error("[ws_channel] get a unknown message: ~p, channel will closed", [Info]), logger:error("[service_channel] get a unknown message: ~p, channel will closed", [Info]),
{ok, State}. {ok, State}.
%% %%
@ -61,22 +61,22 @@ websocket_info({topic_broadcast, Topic, Content}, State = #state{}) ->
Packet = service_pb:encode_msg(#'ServiceCast'{ Packet = service_pb:encode_msg(#'ServiceCast'{
body = {topic_event, #'ServiceCast.TopicEvent'{topic = Topic, content = Content}} body = {topic_event, #'ServiceCast.TopicEvent'{topic = Topic, content = Content}}
}), }),
logger:debug("[ws_channel] will publish topic: ~p", [Topic]), logger:debug("[service_channel] will publish topic: ~p", [Topic]),
{reply, {binary, <<?FRAME_CAST, Packet/binary>>}, State}; {reply, {binary, <<?FRAME_CAST, Packet/binary>>}, State};
%% service进程关闭 %% service进程关闭
websocket_info({'DOWN', _Ref, process, ServicePid, Reason}, State = #state{service_pid = ServicePid}) -> websocket_info({'DOWN', _Ref, process, ServicePid, Reason}, State = #state{service_pid = ServicePid}) ->
logger:debug("[ws_channel] container_pid: ~p, exited: ~p", [ServicePid, Reason]), logger:debug("[service_channel] container_pid: ~p, exited: ~p", [ServicePid, Reason]),
{stop, State#state{service_pid = undefined}}; {stop, State#state{service_pid = undefined}};
%% %%
websocket_info({stop, Reason}, State) -> websocket_info({stop, Reason}, State) ->
logger:debug("[ws_channel] the channel will be closed with reason: ~p", [Reason]), logger:debug("[service_channel] the channel will be closed with reason: ~p", [Reason]),
{stop, State}; {stop, State};
%% %%
websocket_info(Info, State) -> websocket_info(Info, State) ->
logger:debug("[ws_channel] channel get unknown info: ~p", [Info]), logger:debug("[service_channel] channel get unknown info: ~p", [Info]),
{ok, State}. {ok, State}.
%% %%
@ -87,7 +87,7 @@ terminate(Reason, _Req, State = #state{service_id = ServiceId, is_registered = I
false -> false ->
ok ok
end, end,
logger:debug("[ws_channel] channel close with reason: ~p, state is: ~p", [Reason, State]), logger:debug("[service_channel] channel close with reason: ~p, state is: ~p", [Reason, State]),
ok. ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -114,7 +114,7 @@ handle_request(#'ServiceRequest'{packet_id = PacketId, request = {register, #'Se
{reply, {binary, result_reply_packet(PacketId, <<"ok">>)}, {reply, {binary, result_reply_packet(PacketId, <<"ok">>)},
State#state{service_id = ServiceId, service_pid = ServicePid, is_registered = true}}; State#state{service_id = ServiceId, service_pid = ServicePid, is_registered = true}};
{error, Error} -> {error, Error} ->
logger:warning("[ws_channel] service_id: ~p, attach_channel get error: ~p", [ServiceId, Error]), logger:warning("[service_channel] service_id: ~p, attach_channel get error: ~p", [ServiceId, Error]),
{reply, {binary, error_reply_packet(PacketId, -1, <<"attach channel failed">>)}, State} {reply, {binary, error_reply_packet(PacketId, -1, <<"attach channel failed">>)}, State}
end; end;