Class: WebSocketContext
- Inherits:
-
Object
- Object
- WebSocketContext
- Defined in:
- lib/websocketcontext.rb
Instance Attribute Summary collapse
-
#accepted ⇒ Object
Returns the value of attribute accepted.
-
#close_code ⇒ Object
Returns the value of attribute close_code.
-
#closed ⇒ Object
Returns the value of attribute closed.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#orig_meta ⇒ Object
Returns the value of attribute orig_meta.
-
#out_close_code ⇒ Object
Returns the value of attribute out_close_code.
-
#out_events ⇒ Object
Returns the value of attribute out_events.
Instance Method Summary collapse
- #accept ⇒ Object
- #can_recv ⇒ Object
- #close(code = nil) ⇒ Object
- #detach ⇒ Object
-
#initialize(id, meta, in_events) ⇒ WebSocketContext
constructor
A new instance of WebSocketContext.
- #is_opening ⇒ Object
- #recv ⇒ Object
- #send(message) ⇒ Object
- #send_control(message) ⇒ Object
- #subscribe(channel) ⇒ Object
- #unsubscribe(channel) ⇒ Object
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, , 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 = Marshal.load(Marshal.dump()) end |
Instance Attribute Details
#accepted ⇒ Object
Returns the value of attribute accepted.
13 14 15 |
# File 'lib/websocketcontext.rb', line 13 def accepted @accepted end |
#close_code ⇒ Object
Returns the value of attribute close_code.
17 18 19 |
# File 'lib/websocketcontext.rb', line 17 def close_code @close_code end |
#closed ⇒ Object
Returns the value of attribute closed.
15 16 17 |
# File 'lib/websocketcontext.rb', line 15 def closed @closed end |
#meta ⇒ Object
Returns the value of attribute meta.
12 13 14 |
# File 'lib/websocketcontext.rb', line 12 def @meta end |
#orig_meta ⇒ Object
Returns the value of attribute orig_meta.
11 12 13 |
# File 'lib/websocketcontext.rb', line 11 def @orig_meta end |
#out_close_code ⇒ Object
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_events ⇒ Object
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
#accept ⇒ Object
37 38 39 |
# File 'lib/websocketcontext.rb', line 37 def accept @accepted = true end |
#can_recv ⇒ Object
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 |
#detach ⇒ Object
104 105 106 |
# File 'lib/websocketcontext.rb', line 104 def detach() send_control(GripControl.('detach')) end |
#is_opening ⇒ Object
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 |
#recv ⇒ Object
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() @out_events.push(WebSocketEvent.new('TEXT', 'm:' + )) end |
#send_control(message) ⇒ Object
90 91 92 |
# File 'lib/websocketcontext.rb', line 90 def send_control() @out_events.push(WebSocketEvent.new('TEXT', 'c:' + )) end |
#subscribe(channel) ⇒ Object
94 95 96 97 |
# File 'lib/websocketcontext.rb', line 94 def subscribe(channel) send_control(GripControl.( '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.( 'unsubscribe', {'channel' => RailsSettings.get_prefix + channel})) end |