Class: Buttplug::Client
- Inherits:
-
Object
- Object
- Buttplug::Client
- Defined in:
- lib/buttplugrb.rb
Overview
Our Client for a buttplug.io server
Instance Method Summary collapse
-
#generateID ⇒ Object
Does exactly what it says on the tin, generates a random id for our messages.
-
#initialize(serverLocation) ⇒ Client
constructor
Creates a new client for buttplug.io.
-
#listDevices ⇒ Object
Lists all devices available to the server.
-
#sendMessage(message) ⇒ Object
Sends a message to our buttplug server.
-
#startScanning ⇒ Object
Tells our server to start scanning for new devices.
-
#stopAllDevices ⇒ Object
Stops all devices currently controlled by the server.
-
#stopScanning ⇒ Object
Tells our server to stop scanning for new devices.
Constructor Details
#initialize(serverLocation) ⇒ Client
Creates a new client for buttplug.io
Arguments:
-
serverLocation (string) - Where our buttplug.io server is hosted. this will tend to be:
"wss://localhost:12345/buttplug"
Returns:
-
A shiney new buttplug client ready for some action
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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/buttplugrb.rb', line 22 def initialize(serverLocation) @location=serverLocation #Ok Explanation time! # * @EventQueue - The events we are triggering on the server, Expected to be an array, with the first element being the message Id, and the second being the message itself! @eventQueue=EM::Queue.new @eventMachine=Thread.new{EM.run{ eventQueue=@eventQueue ={} ws = Faye::WebSocket::Client.new(@location) tickLoop=EM.tick_loop do #Should improve response times~ eventQueue.pop{|msg| ws.send msg[1] [msg[0]]=msg p [Time.now, :message_send, msg[1]] } end ws.on :open do |event| p [Time.now, :open] ws.send '[{"RequestServerInfo": {"Id": 1, "ClientName": "roboMegumin", "MessageVersion": 1}}]' end ws.on :message do |event| =JSON::parse(event.data)[0] .each{|key,value| #We don't really care about the key just yet ... We are going to just care about finding our ID if(.keys.include?(value["Id"])) [value["Id"]]<<{key => value}#And now we care about our key! puts [value["Id"]].object_id .delete(value["Id"]) p [Time.now, :message_recieved, [{key => value}]] next elsif(key=="ServerInfo") p [Time.now, :server_info, value] end } end ws.on :close do |event| p [Time.now, :close, event.code, event.reason] ws = nil end EM.add_periodic_timer(0.5){ ws.send "[{\"Ping\": {\"Id\": #{generateID()}}}]" } }} @eventMachine.run end |
Instance Method Details
#generateID ⇒ Object
Does exactly what it says on the tin, generates a random id for our messages
Returns:
-
a number between 2 and 4294967295
130 131 132 |
# File 'lib/buttplugrb.rb', line 130 def generateID() return rand(2..4294967295) end |
#listDevices ⇒ Object
Lists all devices available to the server
Returns:
-
An array of available devices from the server
Example:
client.listDevices()
[{"DeviceName"=>"XBox Compatible Gamepad (XInput)", "DeviceIndex"=>1, "DeviceMessages"=>{"SingleMotorVibrateCmd"=>{}, "VibrateCmd"=>{"FeatureCount"=>2}, "StopDeviceCmd"=>{}}}]
91 92 93 94 95 96 97 98 99 |
# File 'lib/buttplugrb.rb', line 91 def listDevices() id=generateID() deviceRequest=[id,"[{\"RequestDeviceList\": {\"Id\":#{id}}}]"] @eventQueue.push(deviceRequest) while(deviceRequest.length<3) do sleep 0.01#Just so we arn't occupying all the time on the system while we are waiting for our device list to come back. end return deviceRequest[2]["DeviceList"]["Devices"] end |
#sendMessage(message) ⇒ Object
Sends a message to our buttplug server
Arguments:
-
message (JSON formatted string) - The message we are sending to our server
Returns:
-
the Response from our server
117 118 119 120 121 122 123 |
# File 'lib/buttplugrb.rb', line 117 def sendMessage() @eventQueue.push() while(.length<3) do sleep 0.01 end return [3] end |
#startScanning ⇒ Object
Tells our server to start scanning for new devices
70 71 72 73 |
# File 'lib/buttplugrb.rb', line 70 def startScanning() id=generateID() @eventQueue.push([id,"[{\"StartScanning\":{\"Id\":#{id}}}]"]) end |
#stopAllDevices ⇒ Object
Stops all devices currently controlled by the server
103 104 105 106 107 |
# File 'lib/buttplugrb.rb', line 103 def stopAllDevices() id=generateID() deviceRequest=[id,"[{\"StopAllDevices\": {\"ID\":#{id}}}]"] @eventQueue.push(deviceRequest) end |
#stopScanning ⇒ Object
Tells our server to stop scanning for new devices
77 78 79 80 |
# File 'lib/buttplugrb.rb', line 77 def stopScanning() id=generateID() @eventQueue.push([id,"[{\"StopScanning\":{\"Id\":#{id}}}]"]) end |