lib/ws-io.rb in ws-io-1.0.1 vs lib/ws-io.rb in ws-io-1.1.0

- old
+ new

@@ -1,75 +1,106 @@ -require 'web_socket' require 'erb' +require 'thread' +require 'tempfile' +require 'web_socket' +require "launchy" class WsIo class << self - attr_accessor :ws + attr_accessor :ws, :domains, :port if ENV['WSIO_DEBUG'] require 'g' else def g(*args);end end - def start(domains = ["*"], port = 8080) + def start(port = 8080, domains = ["*"]) + @port = port + @domains = domains + fake_io - threads = [] + m = Mutex.new + c = ConditionVariable.new - threads << Thread.start do + @server_thread = Thread.start do @server = WebSocketServer.new(:accepted_domains => domains, :port => port) - @server.run() do |ws| - if ws.path == "/" - ws.handshake() - WsIo.ws = ws - while data = ws.receive() - WsIo.input(data) - end - else - ws.handshake("404 Not Found") - end - stop_server - end - end - - threads << Thread.start do begin - yield - ensure - unfake_io - stop_server - end - end - - threads << Thread.start do + @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 - break if @ws && @ws.instance_variable_get(:@socket).closed? if @ws begin @ws.send(escape(output)) rescue # ignore! end end end end - threads.each do |thread| - thread.join + 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 + def after + yield + self + end + + def join + @server_thread.join if @server_thread + end + + 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 + def fake_io @in_read, @in_write = IO.pipe @stdin = STDIN.clone STDIN.reopen(@in_read) @stdout = STDOUT.clone @@ -82,20 +113,23 @@ def unfake_io STDIN.reopen(@stdin) STDOUT.reopen(@stdout) STDERR.reopen(@stderr) - @in_read.close - @in_write.close - @out_write.close - @out_read.close + [@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 def stop_server ws.close @server.tcp_server.close g 'stop_server' + rescue => e + g e end def stop stop_server unfake_io