Class: WsIo

Inherits:
Object
  • Object
show all
Defined in:
lib/ws-io.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.domainsObject

Returns the value of attribute domains.



9
10
11
# File 'lib/ws-io.rb', line 9

def domains
  @domains
end

.portObject

Returns the value of attribute port.



9
10
11
# File 'lib/ws-io.rb', line 9

def port
  @port
end

.wsObject

Returns the value of attribute ws.



9
10
11
# File 'lib/ws-io.rb', line 9

def ws
  @ws
end

Class Method Details

.afterObject



85
86
87
88
# File 'lib/ws-io.rb', line 85

def after
  yield
  self
end

.escape(output) ⇒ Object



147
148
149
# File 'lib/ws-io.rb', line 147

def escape(output)
  ERB::Util.html_escape(output.gsub(/\e\[[^m]+m/, '')) if output
end

.fake_ioObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ws-io.rb', line 102

def fake_io
  @in_read, @in_write = IO.pipe
  @stdin = STDIN.clone
  STDIN.reopen(@in_read)
  @stdout = STDOUT.clone
  @stderr = STDERR.clone
  @out_read, @out_write = IO.pipe
  STDOUT.reopen(@out_write)
  STDERR.reopen(@out_write)
  g 'fake_io'
end

.input(msg) ⇒ Object



139
140
141
# File 'lib/ws-io.rb', line 139

def input(msg)
  @in_write.puts(msg + "\n")
end

.joinObject



90
91
92
# File 'lib/ws-io.rb', line 90

def join
  @server_thread.join if @server_thread
end

.openObject



94
95
96
97
98
99
100
# File 'lib/ws-io.rb', line 94

def open
  tempfile = Tempfile.open('ws-io')
  tempfile << ERB.new(File.read(File.expand_path('../index.html.erb', __FILE__))).result(binding)
  tempfile.flush
  Launchy::Browser.run(tempfile.path)
  self
end

.outputObject



143
144
145
# File 'lib/ws-io.rb', line 143

def output
  @out_read.gets
end

.start(port = 8080, domains = ["*"]) ⇒ Object



17
18
19
20
21
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ws-io.rb', line 17

def start(port = 8080, domains = ["*"])
  @port = port
  @domains = domains

  fake_io

  m = Mutex.new
  c = ConditionVariable.new

  @server_thread = Thread.start do
    @server = WebSocketServer.new(:accepted_domains => domains, :port => port)
    begin
      @server.run() do |ws|
        if ws.path == "/"
          ws.handshake()
          WsIo.ws = ws
          ws.send("connected")
          c.signal                      # ####
          while data = ws.receive()          #
            WsIo.input(data)                 #
          end                                #
        else                                 #  ##     ## ##     ## ######## ######## ##     ## ####
          ws.handshake("404 Not Found")      #  ###   ### ##     ##    ##    ##        ##   ##  ####
        end                                  #  #### #### ##     ##    ##    ##         ## ##   ####
        stop_server                          #  ## ### ## ##     ##    ##    ######      ###     ##
      end                                    #  ##     ## ##     ##    ##    ##         ## ##
    rescue => e                              #  ##     ## ##     ##    ##    ##        ##   ##  ####
      g e                                    #  ##     ##  #######     ##    ######## ##     ## ####
    end                                      #
  end                                        #
                                             #
  Thread.start do                            #
    m.synchronize { c.wait(m) }         # <###
    loop do
      if @ws
        begin
          @ws.send(escape(output))
        rescue
          # ignore!
        end
      end
    end
  end

  Thread.start do
    begin
      yield
    rescue => e
      g e
    ensure
      unfake_io
      stop_server
    end
  end

  self
rescue SignalException, StandardError => e
  g e
  unfake_io
  stop_server
  raise
rescue Exception => e
  g e
  unfake_io
  stop_server
  raise
end

.stopObject



134
135
136
137
# File 'lib/ws-io.rb', line 134

def stop
  stop_server
  unfake_io
end

.stop_serverObject



126
127
128
129
130
131
132
# File 'lib/ws-io.rb', line 126

def stop_server
  ws.close
  @server.tcp_server.close
  g 'stop_server'
rescue => e
  g e
end

.unfake_ioObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ws-io.rb', line 114

def unfake_io
  STDIN.reopen(@stdin)
  STDOUT.reopen(@stdout)
  STDERR.reopen(@stderr)
  [@in_read, @in_write, @out_read, @out_write].each do |io|
    io.close unless io.closed?
  end
  g 'unfake_io'
rescue => e
  g e
end

Instance Method Details

#g(*args) ⇒ Object



14
# File 'lib/ws-io.rb', line 14

def g(*args);end