8000 How to shutdown/close websocket? · Issue #93 · dominicletz/websockex · GitHub
[go: up one dir, main page]

Skip to content

How to shutdown/close websocket? #93

Closed

Description

@grantwest

I couldn't find in the documentation how to close a websocket when it is no longer of any use. Is this possible? Maybe the documentation just needs a small update?

Activity

  1. Azolo commented on Apr 15, 2021

    @Azolo
    Collaborator

    Yeah, there's not a specific place that tells that I guess.

    The answer is most of the callbacks have the option to return a {:close, state} tuple.

    https://hexdocs.pm/websockex/0.4.3/WebSockex.html#c:handle_cast/2

  2. Azolo commented on Apr 15, 2021

    @Azolo
    Collaborator

    This is definitely a situation where I don't know where to put that particular piece of documentation.

    If you have any ideas, let me know.

  3. vipul2774 commented on Mar 4, 2022

    @vipul2774

    I tried to return {:close, new_state} from handle_info/2 & handle_cast/2 but again it starts new socket connection. What am I missing? Thanks

  4. vipul2774 commented on Mar 5, 2022

    @vipul2774

    It was solved by using restart option.

    defmodule MyApp.SocketClient.Socket do
      use WebSockex, restart: :temporary
    
      def start_link(state) do
        WebSockex.start_link("ws://localhost:4001/socket/live", __MODULE__, state)
      end
    
      def handle_cast({:send, {type, msg} = frame}, state) do
        {:reply, frame, state}
      end
    
      def handle_info(:close_socket, {t_ref, state}) do
        :timer.cancel(t_ref)
        {:close, {nil, state}}
      end
    
      def terminate(_, state) do
        exit(:normal)
      end
    end
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0