fix channel
This commit is contained in:
parent
1bb9c33ded
commit
d32cd25f43
@ -31,7 +31,7 @@ start_http_server() ->
|
||||
|
||||
Dispatcher = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/ws", ws_channel, []},
|
||||
{"/ws", service_channel, []},
|
||||
{"/files/[...]", cowboy_static, {dir, "/usr/local/code/downloads"}},
|
||||
{"/upload", upload_channel, []}
|
||||
]}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
%%% @end
|
||||
%%% Created : 11. 1月 2021 上午12:17
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(ws_channel).
|
||||
-module(service_channel).
|
||||
-author("licheng5").
|
||||
-include("efka_tables.hrl").
|
||||
-include("service_pb.hrl").
|
||||
@ -36,7 +36,7 @@ init(Req, Opts) ->
|
||||
{cowboy_websocket, Req, Opts}.
|
||||
|
||||
websocket_init(_State) ->
|
||||
logger:debug("[ws_channel] get a new connection"),
|
||||
logger:debug("[service_channel] get a new connection"),
|
||||
%% 初始状态为true
|
||||
{ok, #state{}}.
|
||||
|
||||
@ -45,15 +45,15 @@ websocket_handle(ping, State) ->
|
||||
|
||||
websocket_handle({binary, <<?FRAME_REQUEST, PacketBin/binary>>}, State) ->
|
||||
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);
|
||||
websocket_handle({binary, <<?FRAME_CAST, PacketBin/binary>>}, State) ->
|
||||
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);
|
||||
|
||||
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}.
|
||||
|
||||
%% 订阅的消息
|
||||
@ -61,22 +61,22 @@ websocket_info({topic_broadcast, Topic, Content}, State = #state{}) ->
|
||||
Packet = service_pb:encode_msg(#'ServiceCast'{
|
||||
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};
|
||||
|
||||
%% service进程关闭
|
||||
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}};
|
||||
|
||||
%% 处理关闭信号
|
||||
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};
|
||||
|
||||
%% 处理其他未知消息
|
||||
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}.
|
||||
|
||||
%% 进程关闭事件
|
||||
@ -87,7 +87,7 @@ terminate(Reason, _Req, State = #state{service_id = ServiceId, is_registered = I
|
||||
false ->
|
||||
ok
|
||||
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.
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
@ -114,7 +114,7 @@ handle_request(#'ServiceRequest'{packet_id = PacketId, request = {register, #'Se
|
||||
{reply, {binary, result_reply_packet(PacketId, <<"ok">>)},
|
||||
State#state{service_id = ServiceId, service_pid = ServicePid, is_registered = true}};
|
||||
{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}
|
||||
end;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user