class DiPS::DiPSClient

Public Class Methods

new(serverUrl) click to toggle source
# File lib/dipsclient.rb, line 11
def initialize serverUrl
        $dipsSubscriptions = Hash.new
        @clientId = SecureRandom.uuid
        @diPSservice = "#{serverUrl}/?clientid=#{@clientId}" #We send the clientid to the server
        @ws = WebSocket::Client::Simple.connect @diPSservice, {
                                                  :proxy => {
                                                    :origin  => 'http://console.org',
                                                    :headers => {'User-Agent' => 'ruby'}
                                                  }
                                                }
        #Process check if we have a scubscription for that event
        #if so, launch the proper callback
        @ws.on :message do |msg|
                begin
                        #Parse the message as an object
                        event = JSON.parse(msg.data)
                         eName = event["EventName"]
                        if !$dipsSubscriptions[eName].nil?
                                $dipsSubscriptions[eName].call(event["PayLoad"])
                        end                                
                rescue Exception => e
                        #p e
                end                 
        end

        @ws.on :open do
              #do nothing
        end

        @ws.on :close do |e|
              #do nothing
        end

        @ws.on :error do |e|
          p e
        end
end

Public Instance Methods

Disconnect() click to toggle source
# File lib/dipsclient.rb, line 49
def Disconnect
        @ws.close
end
Publish(eventName, eventParameter) click to toggle source

publish and event and send some parameter to the subscribers

# File lib/dipsclient.rb, line 82
def Publish eventName, eventParameter
        #create the event
        parameter = JSON.generate(eventParameter)
        event = { "EventName" => eventName , "ClientId" => @clientId, "MessageType" => "publish", "EventParameter" => parameter      }
        #send the event
        @ws.send JSON.generate(event)
end
Subscribe(eventName, &callback) click to toggle source

Subscribe to some event, a callback is executed when the event occurs

# File lib/dipsclient.rb, line 54
def Subscribe eventName, &callback
        if $dipsSubscriptions[eventName].nil?
                $dipsSubscriptions[eventName] = callback    
                #send the subscription to the server
                event = { "EventName" => eventName , "ClientId" => @clientId, "MessageType" => "subscribe" }
                msg = JSON.generate(event)

                #send the event
                @ws.send msg
        else
                raise "You can only have one subscription to an event"
        end
end
Unsubscribe(eventName) click to toggle source

unscubscribe from some event

# File lib/dipsclient.rb, line 69
def Unsubscribe eventName
        if !$dipsSubscriptions[eventName].nil?
                $dipsSubscriptions.delete(eventName)
                #send the unsubscription to the server
                event = { "EventName" => eventName , "ClientId" => @clientId, "MessageType" => "unsubscribe" }
                #send the event
                @ws.send JSON.generate(event)
        else
                raise "You are not subscribed to that event"
        end
end