Module: Wands::JavaScript::WebSocket

Included in:
WebSocket
Defined in:
lib/wands/java_script/web_socket.rb

Overview

WebSocket class for when JavaScript is available in the browser. All methods are synchronous and blocking, compatible with the Socket class.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



13
# File 'lib/wands/java_script/web_socket.rb', line 13

def self.prepended(base) = base.singleton_class.prepend(ClassMethods)

Instance Method Details

#<<(event) ⇒ Object

Add an text flame into the buffer



67
# File 'lib/wands/java_script/web_socket.rb', line 67

def <<(event) = @buffer << event[:data]

#getsObject



50
# File 'lib/wands/java_script/web_socket.rb', line 50

def gets = @buffer.pop.to_s

#initialize(web_socket) ⇒ Object



44
45
46
47
48
# File 'lib/wands/java_script/web_socket.rb', line 44

def initialize(web_socket)
  @socket = web_socket
  @buffer = Queue.new
  @binary_message = "".b
end

#puts(str) ⇒ Object



52
53
54
55
56
# File 'lib/wands/java_script/web_socket.rb', line 52

def puts(str)
  raise "socket is closed" unless @socket[:readyState] == 1

  @socket.send(str)
end

#write(str) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/wands/java_script/web_socket.rb', line 58

def write(str)
  raise "socket is closed" unless @socket[:readyState] == 1

  array_buffer = ArrayBuffer.new
  array_buffer.write(str)
  @socket.send(array_buffer.to_js_array_buffer, binary: true)
end