erlang:从 ssl 接收响应

erlang: receive response from ssl

我在 ejabberd 中添加了一个新模块 (-behavior(gen_mod)),我能够打开到服务器的 ssl 连接并使用 ssl 连接(ssl:send() 函数)直接发送消息。但是,此后我无法收到回复。

我错过了什么吗? erlang中ssl套接字如何异步接收响应?

任何指点将不胜感激。谢谢!

代码:仅添加代码的相关部分。

sendPacketToServer() ->
 case ssl:connect(Gateway, Port, Options, ?SSL_TIMEOUT) of
        {ok, Socket} ->
            ssl:controlling_process(Socket, self()),
            Packet = .....,
            Result = ssl:send(Socket, Packet),
            receiveMessage(),
            ssl:close(Socket),
            ?INFO_MSG("~n~n~n~n Successfully sent payload to the server, result: ~p for the user: ~p", [Result, Username]);
        {error, Reason} = Err ->
            ?ERROR_MSG("Unable to connect to the server: ~s for the user: ~p", [ssl:format_error(Reason), Username]),
            Err
 end
...
....
receiveMessage() ->
  receive ->
      {ssl, Msg, Data} ->  % incoming msg from SSL, send it to process
      ......
    {ssl_closed, Msg} ->  % incoming msg from SSL, send it to process
      .....
    {ssl_error, Msg} ->  % incoming msg from SSL, send it to process
      .....
    {ssl_passive, Msg} ->  % incoming msg from SSL, send it to process
      ....
  end.

为gen_server添加了以下代码:(执行以下操作时,我没有立即关闭套接字,但它仍然不起作用)。

start(Host, Opts) ->
        gen_mod:start_child(?MODULE, Host, Opts).
stop(Host) ->
        gen_mod:stop_child(?MODULE, Host).
init([ServerHost|_]) ->
    Opts = gen_mod:get_module_opts(ServerHost, ?MODULE),
    start(ServerHost, Opts),
    {ok, #state{host = ServerHost}}.

handle_call(Request, From, State) ->
    ?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
    {noreply, State}.
handle_cast(Msg, State) ->
    ?WARNING_MSG("Unexpected cast: ~p", [Msg]),
    {noreply, State}.
handle_info(Info, State) ->
    ?WARNING_MSG("Unexpected info: ~p", [Info]),
    {noreply, State}.
terminate(_Reason, State) ->
    ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}.

我能够通过使用 gen_server 模块来解决这个问题,因为它对任何消息都有明确的回调。