Class: WEBSocket::Base::Socket

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

Instance Method Summary collapse

Instance Method Details

#<<(s) ⇒ Object



77
78
79
# File 'lib/WEBSocket/socket.rb', line 77

def << s
  @socket << from_text_to_frame(s.to_s)
end

#acquire_ownership(type) ⇒ Object



4
5
# File 'lib/WEBSocket/socket.rb', line 4

def acquire_ownership type
end

#connect(host, port) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/WEBSocket/socket.rb', line 7

def connect host, port
  @handshake = WebSocket::Handshake::Client.new :url => "ws://#{host}:#{port}"
  @socket.write @handshake.to_s
  msg = @socket.readpartial 4096
  @handshake << msg
  @status = :connected
end

#evented?Boolean

Returns:

  • (Boolean)


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

def evented?
  false
end

#from_frame_to_text(str) ⇒ Object



19
20
21
22
23
# File 'lib/WEBSocket/socket.rb', line 19

def from_frame_to_text str
  frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
  frame << str
  frame.next.to_s
end

#from_text_to_frame(str) ⇒ Object



25
26
27
28
# File 'lib/WEBSocket/socket.rb', line 25

def from_text_to_frame str
  frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => str, :type => :text
  frame.to_s
end

#read(length = nil, buffer = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/WEBSocket/socket.rb', line 30

def read length = nil, buffer = nil
  s = nil
  if length
    if length.kind_of? Numeric
      s = @socket.read length + 4, buffer
    elsif buffer.nil?
      s = @socket.read nil, length
    end
  else
    s = @socket.read nil, nil
  end
  from_frame_to_text s
end

#read_nonblock(length, buffer = nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/WEBSocket/socket.rb', line 44

def read_nonblock length, buffer = nil
  buffer ||= ''

  @socket.read_nonblock length + 4, buffer
  from_frame_to_text buffer
end

#readpartial(length, buffer = nil) ⇒ Object



51
52
53
54
# File 'lib/WEBSocket/socket.rb', line 51

def readpartial length, buffer = nil
  s = @socket.readpartial length + 4, buffer
  from_frame_to_text s
end

#release_ownership(type) ⇒ Object



56
57
# File 'lib/WEBSocket/socket.rb', line 56

def release_ownership type
end

#to_ioObject



59
60
61
# File 'lib/WEBSocket/socket.rb', line 59

def to_io
  @socket
end

#wait_readableObject



63
64
# File 'lib/WEBSocket/socket.rb', line 63

def wait_readable
end

#wait_writableObject



66
67
# File 'lib/WEBSocket/socket.rb', line 66

def wait_writable
end

#write(s) ⇒ Object



69
70
71
# File 'lib/WEBSocket/socket.rb', line 69

def write s
  @socket.write from_text_to_frame(s.to_s)
end

#write_nonblock(s) ⇒ Object



73
74
75
# File 'lib/WEBSocket/socket.rb', line 73

def write_nonblock s
  @socket.write_nonblock from_text_to_frame(s.to_s)
end