首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Erlang Ranch Websocket客户端无法检测到断开的Internet连接

Erlang Ranch Websocket客户端无法检测到断开的Internet连接
EN

Stack Overflow用户
提问于 2019-09-27 14:52:17
回答 1查看 289关注 0票数 2

我用Gun编写了一个非常标准的websocket客户端。它按预期工作,连接、发送和接收消息等,一切都很正常。

然而,我发现它无法检测到断开的互联网连接。如果我从PC上拔下以太网电缆,Gun客户端不会执行任何操作。我没有收到任何类型的错误、“关闭”消息或任何类型的信息。然后,如果我重新连接以太网电缆,什么也不会发生。枪似乎停顿了一下,什么也没做。

理想情况下,如果连接中断,我需要来自Gun的某种消息。这样,我就可以相应地处理事情,并尝试重新连接。

我遗漏了什么?如何检测来自Gun的断开连接?

我的客户端代码是:

代码语言:javascript
复制
-module(test_client).
-behaviour(gen_server).

-include_lib("kernel/include/logger.hrl").


%% API.
-export([start_link/0]).

%% gen_server.
-export([init/1]).
-export([handle_call/3]).
-export([handle_cast/2]).
-export([handle_info/2]).
-export([terminate/2]).
-export([code_change/3]).

-record(state, {
    uri,
    port,
    path
}).

%% API.

-spec start_link() -> {ok, pid()}.
start_link() ->
    gen_server:start_link(?MODULE, [], []).

%% gen_server.

init([]) ->

    ?LOG_INFO(#{pid=>self(), module=>?MODULE, where=>init, msg=>started}),

    URI = "127.0.0.1",
    Port = 443,
    Path = "/ws",
    Opts = #{transport => tls, protocols => [http],retry => 5,retry_timeout => 2000},

    gen_server:cast(self(), connect),

    {ok,  #state{uri=URI, port=Port, path=Path, conn_opts=Opts}}.

handle_call(_Request, _From, State) ->
    {reply, ignored, State}.

handle_cast(connect, State0) ->
    {ok, ConnPid} = gun:open(State0#state.uri, State0#state.port, State0#state.conn_opts),
    _ = monitor(process, ConnPid),
    {noreply, State0#state{conn_pid=ConnPid};

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info({gun_up, ConnPID, http}, State) ->
    ?LOG_INFO(#{pid=>self(), module=>?MODULE, where=>info, msg=>gun_up, conn_pid=>ConnPID}),
    gun:ws_upgrade(ConnPid),
    {noreply, State#state{conn_pid=ConnPID}};

handle_info({gun_upgrade, ConnPID, ConnRef, _, _}, State) ->
    ?LOG_INFO(#{pid=>self(), module=>?MODULE, where=>info, msg=>gun_upgrade, conn_pid=>ConnPID, conn_ref=>ConnRef}),
    {noreply, State#state{conn_pid=ConnPID}};

handle_info({gun_down, ConnPID, ws, closed, _, _}, State) ->
    ?LOG_INFO(#{pid=>self(), module=>?MODULE, where=>info, msg=>gun_down, conn_pid=>ConnPID}),
    gun:close(ConnPID),
    gen_server:cast(self(), retry_connect),
    {noreply, State#state{conn_pid=null}};   

handle_info({gun_ws, _ConnPID, _ConnRef, RawMsg}, State) ->
    io:format("Receive: ~p~n", [RawMsg]),
    {noreply, State};

handle_info({gun_response, ConnPID, _ConnRef, _Err, Code, _Headers}, State0) ->
    ?LOG_ERROR(#{pid=>self(), module=>?MODULE, where=>info, msg=>gun_response, code=>Code}),
    gun:close(ConnPID),
    {noreply, State0#state{conn_pid=null}};

handle_info({gun_error, ConnPID, _StreamRef, Reason}, State0) ->
    ?LOG_ERROR(#{pid=>self(), module=>?MODULE, where=>info, msg=>gun_error, code=>Reason}),
    gun:close(ConnPID),
    {noreply, State0#state{conn_pid=null}};

handle_info({gun_error, ConnPID,Reason}, State0) ->
    ?LOG_ERROR(#{pid=>self(), module=>?MODULE, where=>info, msg=>gun_error, code=>Reason}),
    gun:close(ConnPID),
    {noreply, State0#state{conn_pid=null}};

handle_info({'DOWN', Mref, process, ConnPid, Reason}, State) ->
    ?LOG_ERROR(#{pid=>self(), module=>?MODULE, where=>info, msg=>monitor_down, code=>Reason}),
    demonitor(Mref),
    gun:close(ConnPid),
    {noreply, State#state{conn_pid=null}};   

handle_info(Info, State) ->
    ?LOG_ERROR(#{pid=>self(), module=>?MODULE, where=>info, status=>unknown, msg=>Info}),
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-28 19:19:22

看起来你需要实现一个ping/pong帧的处理程序,更多的信息,请看看RFC https://www.rfc-editor.org/rfc/rfc6455#section-5.5.2https://www.rfc-editor.org/rfc/rfc6455#section-5.5.3。因此,当服务器发送ping时,客户端应用程序,例如:浏览器应该向服务器端回复pong,你可以通过WebSocket来处理它。但是如果服务器发送ping,但没有从客户端获得pong响应-这将意味着连接丢失。希望这会对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58129275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档