Class: WsIo
- Inherits:
-
Object
- Object
- WsIo
- Defined in:
- lib/ws-io.rb
Class Attribute Summary collapse
-
.domains ⇒ Object
Returns the value of attribute domains.
-
.port ⇒ Object
Returns the value of attribute port.
-
.ws ⇒ Object
Returns the value of attribute ws.
Class Method Summary collapse
- .after(&block) ⇒ Object
- .escape(output) ⇒ Object
- .fake_io ⇒ Object
- .input(msg) ⇒ Object
- .join ⇒ Object
- .open ⇒ Object
- .output ⇒ Object
- .start(port = 8080, domains = ["*"]) ⇒ Object
- .stop ⇒ Object
- .stop_server ⇒ Object
- .unfake_io ⇒ Object
Instance Method Summary collapse
Class Attribute Details
.domains ⇒ Object
Returns the value of attribute domains.
9 10 11 |
# File 'lib/ws-io.rb', line 9 def domains @domains end |
.port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/ws-io.rb', line 9 def port @port end |
.ws ⇒ Object
Returns the value of attribute ws.
9 10 11 |
# File 'lib/ws-io.rb', line 9 def ws @ws end |
Class Method Details
.after(&block) ⇒ Object
74 75 76 77 |
# File 'lib/ws-io.rb', line 74 def after(&block) @after_block = block self end |
.escape(output) ⇒ Object
145 146 147 |
# File 'lib/ws-io.rb', line 145 def escape(output) ERB::Util.html_escape(output.gsub(/\e\[[^m]+m/, '')) if output end |
.fake_io ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ws-io.rb', line 100 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
137 138 139 |
# File 'lib/ws-io.rb', line 137 def input(msg) @in_write.puts(msg + "\n") end |
.join ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/ws-io.rb', line 79 def join @server_thread.join if @server_thread rescue Exception => e g e rescue unfake_io stop_server end |
.open ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ws-io.rb', line 88 def open js_paths = %w(jquery.min.js autoresize.jquery.js).map do |js| 'file://localhost' + File.("../public/#{js}", __FILE__) end tempfile = Tempfile.open('ws-io') template = File.read(File.('../public/index.html.erb', __FILE__)) tempfile << ERB.new(template, nil, '-').result(binding) tempfile.flush Launchy::Browser.run(tempfile.path) self end |
.output ⇒ Object
141 142 143 |
# File 'lib/ws-io.rb', line 141 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 |
# File 'lib/ws-io.rb', line 17 def start(port = 8080, domains = ["*"]) @port = port @domains = domains fake_io Thread.abort_on_exception = true 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 c.signal # #### while data = ws.receive() # WsIo.input(data) # end # else # ## ## ## ## ######## ######## ## ## #### ws.handshake("404 Not Found") # ### ### ## ## ## ## ## ## #### end # #### #### ## ## ## ## ## ## #### stop_server # ## ### ## ## ## ## ###### ### ## end # ## ## ## ## ## ## ## ## end # ## ## ## ## ## ## ## ## #### end # ## ## ####### ## ######## ## ## #### # Thread.start do # m.synchronize { c.wait(m) } # <### @after_block.call if @after_block 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 end |
.stop ⇒ Object
132 133 134 135 |
# File 'lib/ws-io.rb', line 132 def stop stop_server unfake_io end |
.stop_server ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/ws-io.rb', line 124 def stop_server ws.close @server.tcp_server.close unless @server.tcp_server.closed? g 'stop_server' rescue => e g e end |
.unfake_io ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ws-io.rb', line 112 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 |