diff --git a/appium_lib_core.gemspec b/appium_lib_core.gemspec index ed3f1e98..38cf2e1e 100644 --- a/appium_lib_core.gemspec +++ b/appium_lib_core.gemspec @@ -21,7 +21,6 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_dependency 'faye-websocket', '>= 0.11', '< 0.13' spec.add_dependency 'selenium-webdriver', '~> 4.21' spec.metadata['rubygems_mfa_required'] = 'true' diff --git a/lib/appium_lib_core/common.rb b/lib/appium_lib_core/common.rb index 34653cad..15eed762 100644 --- a/lib/appium_lib_core/common.rb +++ b/lib/appium_lib_core/common.rb @@ -18,4 +18,3 @@ require_relative 'common/command' require_relative 'common/base' require_relative 'common/wait' -require_relative 'common/ws/websocket' diff --git a/lib/appium_lib_core/common/ws/websocket.rb b/lib/appium_lib_core/common/ws/websocket.rb deleted file mode 100644 index d7b43d0a..00000000 --- a/lib/appium_lib_core/common/ws/websocket.rb +++ /dev/null @@ -1,169 +0,0 @@ -# frozen_string_literal: true - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require 'faye/websocket' -require 'eventmachine' - -module Appium - module Core - class WebSocket - attr_reader :client, :endpoint - - # 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. - # - # @param [String] url URL to establish web socket connection. If the URL has no port, the client use: - # +ws+: 80, +wss+: 443 ports. - # @param [Array] protocols 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 https://github.com/faye/faye-websocket-ruby/ 's one for client. - # @param [Hash] options Initialize options for Faye client. Read https://github.com/faye/faye-websocket-ruby#initialization-options - # for more details. Default is +{}+. - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.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. - # - def initialize(url:, protocols: nil, options: {}) - @endpoint = url - - @ws_thread = Thread.new do - # steep:ignore:start - 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 - # steep:ignore:end - end - end - - # Client - - # - # Sends a ping frame with an optional message and fires the callback when a matching pong is received. - # - # @param [String] message A message to send ping. - # @param [Block] callback - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.ping 'message' - # - def ping(message, &callback) - @client.ping message, &callback - end - - # Accepts either a String or an Array of byte-sized integers and sends a text or binary message over the connection - # to the other peer; binary data must be encoded as an Array. - # - # @param [String, Array] message A message to send a text or binary message over the connection - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.send 'happy testing' - # - def send(message) - @client.send message - end - - # Closes the connection, sending the given status code and reason text, both of which are optional. - # - # @param [Integer] code A status code to send to the peer with close signal. Default is nil. - # @param [String] reason A reason to send to the peer with close signal. Default is 'close from ruby_lib_core'. - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.close reason: 'a something special reason' - # - def close(code: nil, reason: 'close from ruby_lib_core') - if @client.nil? - ::Appium::Logger.warn 'Websocket was closed' - else - @client.close code, reason - end - @ws_thread.exit - end - - # Response from server - - # - # Fires when the socket connection is established. Event has no attributes. - # - # Default is just put a debug message. - # - def handle_open - ::Appium::Logger.debug("#{self.class} :open") - end - - # Standard out by default - # In general, users should customise only message_data - - # - # Fires when the socket receives a message. The message gas one +data+ attribute and this method can handle the data. - # The data is either a String (for text frames) or an Array of byte-sized integers (for binary frames). - # - # Default is just put a debug message and puts the result on standard out. - # In general, users should override this handler to handle messages from the peer. - # - def handle_message_data(data) - ::Appium::Logger.debug("#{self.class} :message #{data}") - $stdout << "#{data}\n" - end - - # - # Fires when there is a protocol error due to bad data sent by the other peer. - # This event is purely informational, you do not need to implement error recovery. - # - # Default is just put a error message. - # - def handle_error - ::Appium::Logger.error("#{self.class} :error") - end - - # - # Fires when either the client or the server closes the connection. The method gets +code+ and +reason+ attributes. - # They expose the status code and message sent by the peer that closed the connection. - # - # Default is just put a error message. - # The methods also clear +client+ instance and stop the eventmachine which is called in initialising this class. - # - def handle_close(code, reason) - ::Appium::Logger.debug("#{self.class} :close #{code} #{reason}") - @client = nil - # steep:ignore:start - EM.stop - # steep:ignore:end - end - end # module WebSocket - end # module Core -end # module Appium diff --git a/sig/gems/faye.rbs b/sig/gems/faye.rbs deleted file mode 100644 index 0cbce268..00000000 --- a/sig/gems/faye.rbs +++ /dev/null @@ -1,10 +0,0 @@ -module Faye - module WebSocket - class Client - def initialize: (String, String, Hash[Symbol, untyped]) -> void - def close: () -> void - def send: (String) -> void - def on: (String, Proc) -> void - end - end -end \ No newline at end of file diff --git a/sig/lib/appium_lib_core/common/ws/websocket.rbs b/sig/lib/appium_lib_core/common/ws/websocket.rbs deleted file mode 100644 index f8be4883..00000000 --- a/sig/lib/appium_lib_core/common/ws/websocket.rbs +++ /dev/null @@ -1,103 +0,0 @@ -module Appium - module Core - class WebSocket - @endpoint: untyped - - @ws_thread: untyped - - @client: untyped - - attr_reader client: untyped - - attr_reader endpoint: untyped - - # 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. - # - # @param [String] url URL to establish web socket connection. If the URL has no port, the client use: - # +ws+: 80, +wss+: 443 ports. - # @param [Array] protocols 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 https://github.com/faye/faye-websocket-ruby/ 's one for client. - # @param [Hash] options Initialize options for Faye client. Read https://github.com/faye/faye-websocket-ruby#initialization-options - # for more details. Default is +{}+. - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.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. - # - def initialize: (url: untyped, ?protocols: untyped?, ?options: ::Hash[untyped, untyped]) -> void - - # - # Sends a ping frame with an optional message and fires the callback when a matching pong is received. - # - # @param [String] message A message to send ping. - # @param [Block] callback - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.ping 'message' - # - def ping: (untyped message) { (?) -> untyped } -> untyped - - # Accepts either a String or an Array of byte-sized integers and sends a text or binary message over the connection - # to the other peer; binary data must be encoded as an Array. - # - # @param [String|Array] message A message to send a text or binary message over the connection - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.send 'happy testing' - # - def send: (untyped message) -> untyped - - # Closes the connection, sending the given status code and reason text, both of which are optional. - # - # @param [Integer] code A status code to send to the peer with close signal. Default is nil. - # @param [String] reason A reason to send to the peer with close signal. Default is 'close from ruby_lib_core'. - # - # @example - # ws = WebSocket.new(url: "ws://#{host}:#{port}/ws/session/#{@session_id}/appium/device/logcat") - # ws.close reason: 'a something special reason' - # - def close: (?code: untyped?, ?reason: ::String) -> untyped - - # - # Fires when the socket connection is established. Event has no attributes. - # - # Default is just put a debug message. - # - def handle_open: () -> untyped - - # - # Fires when the socket receives a message. The message gas one +data+ attribute and this method can handle the data. - # The data is either a String (for text frames) or an Array of byte-sized integers (for binary frames). - # - # Default is just put a debug message and puts the result on standard out. - # In general, users should override this handler to handle messages from the peer. - # - def handle_message_data: (untyped data) -> untyped - - # - # Fires when there is a protocol error due to bad data sent by the other peer. - # This event is purely informational, you do not need to implement error recovery. - # - # Default is just put a error message. - # - def handle_error: () -> untyped - - # - # Fires when either the client or the server closes the connection. The method gets +code+ and +reason+ attributes. - # They expose the status code and message sent by the peer that closed the connection. - # - # Default is just put a error message. - # The methods also clear +client+ instance and stop the eventmachine which is called in initialising this class. - # - def handle_close: (untyped code, untyped reason) -> untyped - end - end -end diff --git a/test/unit/common/websocket_test.rb b/test/unit/common/websocket_test.rb deleted file mode 100644 index c0344ebf..00000000 --- a/test/unit/common/websocket_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require 'test_helper' -require 'webmock/minitest' - -class AppiumLibCoreTest - class WebSocketTest < Minitest::Test - def test_connect_websocket - ws = ::Appium::Core::WebSocket.new(url: 'ws://localhost:9292') - assert_nil ws.client - end - end -end