Method: Appium::Core::WebSocket#initialize

Defined in:
lib/appium_lib_core/common/ws/websocket.rb

#initialize(url:, protocols: nil, options: {}) ⇒ WebSocket

A websocket client based on Faye::WebSocket::Client . Uses eventmachine to wait response from the peer. The eventmachine works on a thread. The thread will exit with close method.

Examples:

ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat")
ws.client #=> #<Faye::WebSocket::Client:.....> # An instance of Faye::WebSocket::Client
ws.message 'some message' #=> nil. Send a message to the peer.
ws.close #=> Kill the thread which run a eventmachine.

Parameters:

  • url (String)

    URL to establish web socket connection. If the URL has no port, the client use: ws: 80, wss: 443 ports.

  • protocols (Array) (defaults to: nil)

    An array of strings representing acceptable subprotocols for use over the socket. The driver will negotiate one of these to use via the Sec-WebSocket-Protocol header if supported by the other peer. Default is nil. The protocols is equal to github.com/faye/faye-websocket-ruby/ ‘s one for client.

  • options (Hash) (defaults to: {})

    Initialize options for Faye client. Read github.com/faye/faye-websocket-ruby#initialization-options for more details. Default is {}.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/appium_lib_core/common/ws/websocket.rb', line 42

def initialize(url:, protocols: nil, options: {})
  @endpoint = url

  @ws_thread = Thread.new do
    EM.run do
      @client ||= ::Faye::WebSocket::Client.new(url, protocols, options)

      @client.on :open do |_open|
        handle_open
      end

      @client.on :message do |message|
        handle_message_data(message.data)
      end

      @client.on :error do |_error|
        handle_error
      end

      @client.on :close do |close|
        handle_close(close.code, close.reason)
      end
    end
  end
end