Class: Buttplug::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/buttplugrb.rb

Overview

Our Client for a buttplug.io server

Instance Method Summary collapse

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 
    messageWatch={}
    ws = Faye::WebSocket::Client.new(@location)
    tickLoop=EM.tick_loop do #Should improve response times~
      eventQueue.pop{|msg|
        ws.send msg[1]
        messageWatch[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|
      message=JSON::parse(event.data)[0]
      message.each{|key,value|
        #We don't really care about the key just yet ... We are going to just care about finding our ID
        if(messageWatch.keys.include?(value["Id"]))
          messageWatch[value["Id"]]<<{key => value}#And now we care about our key!
          puts messageWatch[value["Id"]].object_id
          messageWatch.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

#generateIDObject

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

#listDevicesObject

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(message)
  @eventQueue.push(message)
  while(message.length<3) do
    sleep 0.01
  end
  return message[3]
end

#startScanningObject

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

#stopAllDevicesObject

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

#stopScanningObject

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