|
| 1 | +require 'faye/websocket' |
| 2 | +require 'eventmachine' |
| 3 | + |
| 4 | +module Appium |
| 5 | + module Core |
| 6 | + class WebSocket |
| 7 | + attr_reader :client, :endpoint |
| 8 | + |
| 9 | + # A websocket client based on Faye::WebSocket::Client . |
| 10 | + # Uses eventmachine to wait response from the peer. The eventmachine works on a thread. The thread will exit |
| 11 | + # with close method. |
| 12 | + # |
| 13 | + # @param [String] url: URL to establish web socket connection. If the URL has no port, the client use: |
| 14 | + # `ws`: 80, `wss`: 443 ports. |
| 15 | + # @param [Array] protocols: An array of strings representing acceptable subprotocols for use over the socket. |
| 16 | + # The driver will negotiate one of these to use via the Sec-WebSocket-Protocol header |
| 17 | + # if supported by the other peer. Default is nil. |
| 18 | + # The protocols is equal to https://github.com/faye/faye-websocket-ruby/ 's one for client. |
| 19 | + # @param [Hash] options: Initialize options for Faye client. Read https://github.com/faye/faye-websocket-ruby#initialization-options |
| 20 | + # for more details. Default is `{}`. |
| 21 | + # |
| 22 | + # @example |
| 23 | + # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") |
| 24 | + # ws.client #=> #<Faye::WebSocket::Client:.....> # An instance of Faye::WebSocket::Client |
| 25 | + # ws.message 'some message' #=> nil. Send a message to the peer. |
| 26 | + # ws.close #=> Kill the thread which run a eventmachine. |
| 27 | + # |
| 28 | + def initialize(url:, protocols: nil, options: {}) |
| 29 | + @endpoint = url |
| 30 | + |
| 31 | + @ws_thread = Thread.new do |
| 32 | + EM.run do |
| 33 | + @client ||= ::Faye::WebSocket::Client.new(url, protocols, options) |
| 34 | + |
| 35 | + @client.on :open do |_open| |
| 36 | + handle_open |
| 37 | + end |
| 38 | + |
| 39 | + @client.on :message do |message| |
| 40 | + handle_message_data(message.data) |
| 41 | + end |
| 42 | + |
| 43 | + @client.on :error do |_error| |
| 44 | + handle_error |
| 45 | + end |
| 46 | + |
| 47 | + @client.on :close do |close| |
| 48 | + handle_close(close.code, close.reason) |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + # Client |
| 55 | + |
| 56 | + # |
| 57 | + # Sends a ping frame with an optional message and fires the callback when a matching pong is received. |
| 58 | + # |
| 59 | + # @params [String] message A message to send ping. |
| 60 | + # @params [Block] &callback |
| 61 | + # |
| 62 | + # @example |
| 63 | + # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") |
| 64 | + # ws.ping 'message' |
| 65 | + # |
| 66 | + def ping(message, &callback) |
| 67 | + @client.ping message, &callback |
| 68 | + end |
| 69 | + |
| 70 | + # Accepts either a String or an Array of byte-sized integers and sends a text or binary message over the connection |
| 71 | + # to the other peer; binary data must be encoded as an Array. |
| 72 | + # |
| 73 | + # @params [String|Array] message A message to send a text or binary message over the connection |
| 74 | + # |
| 75 | + # @example |
| 76 | + # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") |
| 77 | + # ws.send 'happy testing' |
| 78 | + # |
| 79 | + def send(message) |
| 80 | + @client.send message |
| 81 | + end |
| 82 | + |
| 83 | + # Closes the connection, sending the given status code and reason text, both of which are optional. |
| 84 | + # |
| 85 | + # @params [Integer] code: A status code to send to the peer with close signal. Default is nil. |
| 86 | + # @params [String] reason: A reason to send to the peer with close signal. Default is 'close from ruby_lib_core'. |
| 87 | + # |
| 88 | + # @example |
| 89 | + # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") |
| 90 | + # ws.close reason: 'a something special reason' |
| 91 | + # |
| 92 | + def close(code: nil, reason: 'close from ruby_lib_core') |
| 93 | + if @client.nil? |
| 94 | + ::Appium::Logger.warn 'Websocket was closed' |
| 95 | + else |
| 96 | + @client.close code, reason |
| 97 | + end |
| 98 | + @ws_thread.exit |
| 99 | + end |
| 100 | + |
| 101 | + # Response from server |
| 102 | + |
| 103 | + # |
| 104 | + # Fires when the socket connection is established. Event has no attributes. |
| 105 | + # |
| 106 | + # Default is just put a debug message. |
| 107 | + # |
| 108 | + def handle_open |
| 109 | + ::Appium::Logger.debug %W(#{self.class} :open) |
| 110 | + end |
| 111 | + |
| 112 | + # Standard out by default |
| 113 | + # In general, users should customise only message_data |
| 114 | + |
| 115 | + # |
| 116 | + # Fires when the socket receives a message. The message gas one `data` attribute and this method can handle the data. |
| 117 | + # The data is either a String (for text frames) or an Array of byte-sized integers (for binary frames). |
| 118 | + # |
| 119 | + # Default is just put a debug message and puts the result on standard out. |
| 120 | + # In general, users should override this handler to handle messages from the peer. |
| 121 | + # |
| 122 | + def handle_message_data(data) |
| 123 | + ::Appium::Logger.debug %W(#{self.class} :message #{data}) |
| 124 | + $stdout << "#{data}\n" |
| 125 | + end |
| 126 | + |
| 127 | + # |
| 128 | + # Fires when there is a protocol error due to bad data sent by the other peer. |
| 129 | + # This event is purely informational, you do not need to implement error recovery. |
| 130 | + # |
| 131 | + # Default is just put a error message. |
| 132 | + # |
| 133 | + def handle_error |
| 134 | + ::Appium::Logger.error %W(#{self.class} :error) |
| 135 | + end |
| 136 | + |
| 137 | + # |
| 138 | + # Fires when either the client or the server closes the connection. The method gets `code` and `reason` attributes. |
| 139 | + # They expose the status code and message sent by the peer that closed the connection. |
| 140 | + # |
| 141 | + # Default is just put a error message. |
| 142 | + # The methods also clear `client` instance and stop the eventmachine which is called in initialising this class. |
| 143 | + # |
| 144 | + def handle_close(code, reason) |
| 145 | + ::Appium::Logger.debug %W(#{self.class} :close #{code} #{reason}) |
| 146 | + @client = nil |
| 147 | + EM.stop |
| 148 | + end |
| 149 | + end # module WebSocket |
| 150 | + end # module Core |
| 151 | +end # module Appium |
0 commit comments