Class: Rubame::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, handshake, server) ⇒ Client

Returns a new instance of Client.



101
102
103
104
105
106
107
108
109
# File 'lib/rubame/rubame.rb', line 101

def initialize(socket, handshake, server)
  @socket = socket
  @handshake = handshake
  @frame = WebSocket::Frame::Incoming::Server.new(:version => @handshake.version)
  @opened = false
  @messaged = []
  @closed = false
  @server = server
end

Instance Attribute Details

#closedObject

Returns the value of attribute closed.



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

def closed
  @closed
end

#frameObject

Returns the value of attribute frame.



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

def frame
  @frame
end

#handshakeObject

Returns the value of attribute handshake.



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

def handshake
  @handshake
end

#messagedObject

Returns the value of attribute messaged.



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

def messaged
  @messaged
end

#openedObject

Returns the value of attribute opened.



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

def opened
  @opened
end

#socketObject

Returns the value of attribute socket.



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

def socket
  @socket
end

Instance Method Details

#onclose(&blk) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/rubame/rubame.rb', line 147

def onclose(&blk)
  if @closed
    begin
      blk.call
    ensure
    end
  end
end

#onmessage(&blk) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubame/rubame.rb', line 135

def onmessage(&blk)
  if @messaged.size > 0
    begin
      @messaged.each do |x|
        blk.call(x.to_s)
      end
    ensure
      @messaged = []
    end
  end
end

#onopen(&blk) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/rubame/rubame.rb', line 125

def onopen(&blk)
  if @opened
    begin
      blk.call
    ensure
      @opened = false
    end
  end
end

#send(data) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/rubame/rubame.rb', line 115

def send(data)
  frame = WebSocket::Frame::Outgoing::Server.new(:version => @handshake.version, :data => data, :type => :text)
  begin
    @socket.write frame
    @socket.flush
  rescue
    @server.close(self) unless @closed
  end
end

#write(data) ⇒ Object



111
112
113
# File 'lib/rubame/rubame.rb', line 111

def write(data)
  @socket.write data
end