Class: RComet::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(uri_or_string) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
# File 'lib/rcomet/client.rb', line 10

def initialize( uri_or_string )
  @uri = uri_or_string
  @uri = URI.parse(@uri) if @uri.class == String
  @clientId = nil
  # @interval = nil
  @connection = nil
  @subscriptions = {}
end

Instance Method Details

#connectObject

Request Response MUST include: * channel MUST include: * channel * clientId * successful * connectionType * clientId MAY include: * ext MAY include: * error * id * advice * ext * id * timestamp



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
# File 'lib/rcomet/client.rb', line 28

def connect
  @connection.kill unless @connection.nil?
  @connection = Thread.new {
    faild = false
    while true
      id = RComet.random(32)
      message = {
        "channel" => RComet::Channel::CONNECT,
        "clientId" => @clientId,
        "connectionType" => "long-polling",
        "id" => id
      }
      r = send( message )
        
      if r[0]["id"] == id and r[0]["successful"] == true
        @subscriptions[r[1]["channel"]].call( r[1]["data"])
      elsif r[0]["successful"] == false
        faild = true
        break
      end
    end
    
    if faild
      handshake()
      connect()
    end
  }
end

#disconnectObject

Request Response MUST include: * channel MUST include: * channel * clientId * successful MAY include: * ext * clientId * id MAY include: * error * ext * id



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rcomet/client.rb', line 64

def disconnect
  unless @connection.nil?
    @connection.kill 
    message = {
      "channel" => RComet::Channel::DISCONNECT,
      "clientId" => @clientId,
      "id" => RComet.random(32)
    }
    r = send( message )
    ## TODO : Check response
  end
end

#handshakeObject

Request MUST include: * channel * version * supportedConnectionTypes MAY include: * minimumVersion * ext * id

Success Response Failed Response MUST include: * channel MUST include: * channel * version * successful * supportedConnectionTypes * error * clientId MAY include: * supportedConnectionTypes * successful * advice MAY include: * minimumVersion * version * advice * minimumVersion * ext * ext * id * id * authSuccessful



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rcomet/client.rb', line 96

def handshake
  id = RComet.random(32)
  message = {
    "channel" => RComet::Channel::HANDSHAKE,
    "version" => RComet::BAYEUX_VERSION,
    "supportedConnectionTypes" => [ "long-polling", "callback-polling" ],
    "id" => id
  }
  
  response = send( message )[0]
  if response["successful"] and response["id"] == id
    @clientId = response["clientId"]
    # @interval = response["advice"]["interval"]
  else
    raise
  end
end

#publish(channel, data) ⇒ Object

Request Response MUST include: * channel MUST include: * channel * data * successful MAY include: * clientId MAY include: * id * id * error * ext * ext



120
121
122
123
124
125
126
127
128
129
# File 'lib/rcomet/client.rb', line 120

def publish( channel, data )
  message = {
    "channel" => channel,
    "data" => data, 
    "clientId" => @clientId,
    "id" => RComet.random(32)
  }
  r = send(message)[0]
  ## TODO : Check response
end

#subscribe(channel, &block) ⇒ Object

Request Response MUST include: * channel MUST include: * channel * clientId * successful * subscription * clientId MAY include: * ext * subscription * id MAY include: * error * advice * ext * id * timestamp



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rcomet/client.rb', line 141

def subscribe( channel, &block )
  @subscriptions[channel] = block if block_given?

  message = {
    "channel" => RComet::Channel::SUBSCRIBE,
    "clientId" => @clientId,
    "subscription" => channel,
    "id" => RComet.random(32)
  }
  
  r = send(message)
  ## TODO : Check response
end

#unsubscribe(channels) ⇒ Object

Request Response MUST include: * channel MUST include: * channel * clientId * successful * subscription * clientId MAY include: * ext * subscription * id MAY include: * error * advice * ext * id * timestamp



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rcomet/client.rb', line 165

def unsubscribe( channels )
  channels = [channels] unless channels.class == Array
  channels.each do |c|
    @subscriptions.delete(c)
  end
  message = {
    "channel" => RComet::Channel::UNSUBSCRIBE,
    "clientId" => @clientId,
    "subscription" => channels,
    "id" => RComet.random(32)
  }
  
  r = send(message)
  ## TODO : Check response
end