Class: WebsocketTD::Websocket

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

Constant Summary collapse

IS_WINDOWS =
(RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, path, query, secure = false, port = nil) ⇒ Websocket

host

Host of request. Required if no :url param was provided.

path

Path of request. Should start with ‘/’. Default: ‘/’

query

Query for request. Should be in format “aaa=bbb&ccc=ddd”

secure

Defines protocol to use. If true then wss://, otherwise ws://. This option will not change default port - it should be handled by programmer.

port

Port of request. Default: nil



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/websocket_td.rb', line 22

def initialize(host, path, query, secure = false, port = nil)
  if port == nil
    port = secure ? 443 : 80
  end

  @handshake = WebSocket::Handshake::Client.new({
                                                    :host   => host,
                                                    :port   => port,
                                                    :secure => secure,
                                                    :path   => path,
                                                    :query  => query
                                                })

  @read_buffer = 2048
  @auto_pong   = true
  @closed      = false
  @opened      = false

  @on_open    = lambda {}
  @on_close   = lambda { |message|}
  @on_ping    = lambda { |message|}
  @on_error   = lambda { |error|}
  @on_message = lambda { |message|}

  tcp_socket = TCPSocket.new(host, port)
  if secure
    @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket)
    @socket.connect
  else
    @socket = tcp_socket
  end
  perform_handshake
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



56
57
58
# File 'lib/websocket_td.rb', line 56

def active
  @active
end

#auto_pongObject

Returns the value of attribute auto_pong.



56
57
58
# File 'lib/websocket_td.rb', line 56

def auto_pong
  @auto_pong
end

#on_error=(value) ⇒ Object (writeonly)

:on_open, :on_close



57
58
59
# File 'lib/websocket_td.rb', line 57

def on_error=(value)
  @on_error = value
end

#on_message=(value) ⇒ Object (writeonly)

:on_open, :on_close



57
58
59
# File 'lib/websocket_td.rb', line 57

def on_message=(value)
  @on_message = value
end

#on_ping=(value) ⇒ Object (writeonly)

:on_open, :on_close



57
58
59
# File 'lib/websocket_td.rb', line 57

def on_ping=(value)
  @on_ping = value
end

#read_bufferObject

Returns the value of attribute read_buffer.



56
57
58
# File 'lib/websocket_td.rb', line 56

def read_buffer
  @read_buffer
end

#read_threadObject (readonly)

Returns the value of attribute read_thread.



56
57
58
# File 'lib/websocket_td.rb', line 56

def read_thread
  @read_thread
end

#socketObject (readonly)

Returns the value of attribute socket.



56
57
58
# File 'lib/websocket_td.rb', line 56

def socket
  @socket
end

Instance Method Details

#on_close=(p) ⇒ Object

sets a Proc to be executed when the connection is closed and ready for writing

p

the Proc to execute



89
90
91
92
93
94
# File 'lib/websocket_td.rb', line 89

def on_close=(p)
  @on_close = p
  if @closed
    fire_on_close
  end
end

#on_open=(p) ⇒ Object

sets a Proc to be executed when the connection is opened and ready for writing

p

the Proc to execute



79
80
81
82
83
84
# File 'lib/websocket_td.rb', line 79

def on_open=(p)
  @on_open = p
  if @opened
    fire_on_open
  end
end

#send(data, type = :text) ⇒ Object

Send the data given by the data param if running on a posix system this uses Ruby’s fork method to send if on windows fork won’t be attempted.

data

the data to send

type

:text or :binary, defaults to :text



65
66
67
68
69
70
71
72
73
74
# File 'lib/websocket_td.rb', line 65

def send(data, type = :text)
  if IS_WINDOWS
    do_send(data, type) #fork not supported on windows
  else
    pid = fork do
      do_send(data, type)
    end
    Process.detach(pid)
  end
end