Class: WebSocketContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, meta, in_events) ⇒ WebSocketContext

Returns a new instance of WebSocketContext.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/websocketcontext.rb', line 19

def initialize(id, meta, in_events)
  @id = id
  @in_events = in_events
  @read_index = 0
  @accepted = false
  @close_code = nil
  @closed = false
  @out_close_code = nil
  @out_events = []
  @orig_meta = meta
  @meta = Marshal.load(Marshal.dump(meta))
end

Instance Attribute Details

#acceptedObject

Returns the value of attribute accepted.



13
14
15
# File 'lib/websocketcontext.rb', line 13

def accepted
  @accepted
end

#close_codeObject

Returns the value of attribute close_code.



17
18
19
# File 'lib/websocketcontext.rb', line 17

def close_code
  @close_code
end

#closedObject

Returns the value of attribute closed.



15
16
17
# File 'lib/websocketcontext.rb', line 15

def closed
  @closed
end

#metaObject

Returns the value of attribute meta.



12
13
14
# File 'lib/websocketcontext.rb', line 12

def meta
  @meta
end

#orig_metaObject

Returns the value of attribute orig_meta.



11
12
13
# File 'lib/websocketcontext.rb', line 11

def orig_meta
  @orig_meta
end

#out_close_codeObject

Returns the value of attribute out_close_code.



16
17
18
# File 'lib/websocketcontext.rb', line 16

def out_close_code
  @out_close_code
end

#out_eventsObject

Returns the value of attribute out_events.



14
15
16
# File 'lib/websocketcontext.rb', line 14

def out_events
  @out_events
end

Instance Method Details

#acceptObject



37
38
39
# File 'lib/websocketcontext.rb', line 37

def accept
  @accepted = true
end

#can_recvObject



50
51
52
53
54
55
56
57
58
# File 'lib/websocketcontext.rb', line 50

def can_recv
  for n in @read_index..@in_events.length-1 do
    if ['TEXT', 'BINARY', 'CLOSE', 'DISCONNECT'].include?(
        @in_events[n].type)
      return true
    end
  end
  return false
end

#close(code = nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/websocketcontext.rb', line 41

def close(code=nil)
  @closed = true
  if !code.nil?
    @out_close_code = code
  else
    @out_close_code = 0
  end
end

#detachObject



104
105
106
# File 'lib/websocketcontext.rb', line 104

def detach()
  send_control(GripControl.websocket_control_message('detach'))
end

#is_openingObject



32
33
34
35
# File 'lib/websocketcontext.rb', line 32

def is_opening
  return (!@in_events.nil? and @in_events.length > 0 and 
      @in_events[0].type == 'OPEN')
end

#recvObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/websocketcontext.rb', line 60

def recv
  e = nil
  while e.nil? and @read_index < @in_events.length do
    if ['TEXT', 'BINARY', 'CLOSE', 'DISCONNECT'].include?(
        @in_events[@read_index].type)
      e = @in_events[@read_index]
    elsif @in_events[@read_index].type == 'PING'
      @out_events.push(WebSocketEvent.new('PONG'))
    end
    @read_index += 1
  end
  if e.nil?
    raise 'read from empty buffer'
  end
  if e.type == 'TEXT' or e.type == 'BINARY'
    return e.content
  elsif e.type == 'CLOSE'
    if !e.content.nil? and e.content.length == 2
      @close_code = e.content.unpack('S_')[0]
    end
    return nil
  else
    raise 'client disconnected unexpectedly'
  end
end

#send(message) ⇒ Object



86
87
88
# File 'lib/websocketcontext.rb', line 86

def send(message)
  @out_events.push(WebSocketEvent.new('TEXT', 'm:' + message))
end

#send_control(message) ⇒ Object



90
91
92
# File 'lib/websocketcontext.rb', line 90

def send_control(message)
  @out_events.push(WebSocketEvent.new('TEXT', 'c:' + message))
end

#subscribe(channel) ⇒ Object



94
95
96
97
# File 'lib/websocketcontext.rb', line 94

def subscribe(channel)
  send_control(GripControl.websocket_control_message(
      'subscribe', {'channel' => RailsSettings.get_prefix + channel}))
end

#unsubscribe(channel) ⇒ Object



99
100
101
102
# File 'lib/websocketcontext.rb', line 99

def unsubscribe(channel)
  send_control(GripControl.websocket_control_message(
      'unsubscribe', {'channel' => RailsSettings.get_prefix + channel}))
end