Class: PusherClient::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher-client/websocket.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, params = {}) ⇒ WebSocket

Returns a new instance of WebSocket.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pusher-client/websocket.rb', line 8

def initialize(url, params = {})
  @hs ||= LibWebSocket::OpeningHandshake::Client.new(:url => url, :version => params[:version])
  @frame ||= LibWebSocket::Frame.new

  @socket = TCPSocket.new(@hs.url.host, @hs.url.port || 80)

  @socket.write(@hs.to_s)
  @socket.flush

  loop do
    data = @socket.getc
    next if data.nil?

    result = @hs.parse(data.chr)

    raise @hs.error unless result

    if @hs.done?
      @handshaked = true
      break
    end
  end
end

Instance Method Details

#closeObject



57
58
59
# File 'lib/pusher-client/websocket.rb', line 57

def close
  @socket.close
end

#receiveObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pusher-client/websocket.rb', line 40

def receive
  raise "no handshake!" unless @handshaked

  data = @socket.gets("\xff")
  @frame.append(data)

  messages = []
  while message = @frame.next
    messages << message
  end
  messages
end

#send(data) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/pusher-client/websocket.rb', line 32

def send(data)
  raise "no handshake!" unless @handshaked

  data = @frame.new(data).to_s
  @socket.write data
  @socket.flush
end

#socketObject



53
54
55
# File 'lib/pusher-client/websocket.rb', line 53

def socket
  @socket
end