ws-io 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
data/bin/ws-irb ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.dirname(__FILE__) + "/../lib"
3
+ require "ws-io"
4
+ require 'irb'
5
+
6
+ WsIo.start {
7
+ IRB.start
8
+ }.open.join
data/examples/sh.rb CHANGED
@@ -1,14 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  $:.unshift File.dirname(__FILE__) + "/../lib"
3
+
3
4
  require "ws-io"
4
5
 
5
- threads = []
6
- threads << Thread.start do
7
- WsIo.start(['*'], 8080) do
8
- system '/bin/sh'
9
- end
10
- end
11
- threads << Thread.start do
12
- system 'open', File.expand_path('../index.html', __FILE__)
13
- end
14
- threads.each { |thread| thread.join }
6
+ WsIo.start {
7
+ system '/bin/sh'
8
+ }.open.join
@@ -4,7 +4,7 @@
4
4
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js?v=1"></script>
5
5
  <script type="text/javascript" charset="utf-8">
6
6
  $(function() {
7
- var ws = new WebSocket("ws://127.0.0.1:8080");
7
+ var ws = new WebSocket("ws://127.0.0.1:<%= port %>");
8
8
  ws.onmessage = function(e) {
9
9
  $("#result").append($("<div>").html(e.data));
10
10
  $("body").scrollTop(document.height);
data/lib/ws-io.rb CHANGED
@@ -1,9 +1,12 @@
1
- require 'web_socket'
2
1
  require 'erb'
2
+ require 'thread'
3
+ require 'tempfile'
4
+ require 'web_socket'
5
+ require "launchy"
3
6
 
4
7
  class WsIo
5
8
  class << self
6
- attr_accessor :ws
9
+ attr_accessor :ws, :domains, :port
7
10
 
8
11
  if ENV['WSIO_DEBUG']
9
12
  require 'g'
@@ -11,39 +14,40 @@ class WsIo
11
14
  def g(*args);end
12
15
  end
13
16
 
14
- def start(domains = ["*"], port = 8080)
17
+ def start(port = 8080, domains = ["*"])
18
+ @port = port
19
+ @domains = domains
20
+
15
21
  fake_io
16
22
 
17
- threads = []
23
+ m = Mutex.new
24
+ c = ConditionVariable.new
18
25
 
19
- threads << Thread.start do
26
+ @server_thread = Thread.start do
20
27
  @server = WebSocketServer.new(:accepted_domains => domains, :port => port)
21
- @server.run() do |ws|
22
- if ws.path == "/"
23
- ws.handshake()
24
- WsIo.ws = ws
25
- while data = ws.receive()
26
- WsIo.input(data)
27
- end
28
- else
29
- ws.handshake("404 Not Found")
30
- end
31
- stop_server
32
- end
33
- end
34
-
35
- threads << Thread.start do
36
28
  begin
37
- yield
38
- ensure
39
- unfake_io
40
- stop_server
41
- end
42
- end
43
-
44
- threads << Thread.start do
29
+ @server.run() do |ws|
30
+ if ws.path == "/"
31
+ ws.handshake()
32
+ WsIo.ws = ws
33
+ ws.send("connected")
34
+ c.signal # ####
35
+ while data = ws.receive() #
36
+ WsIo.input(data) #
37
+ end #
38
+ else # ## ## ## ## ######## ######## ## ## ####
39
+ ws.handshake("404 Not Found") # ### ### ## ## ## ## ## ## ####
40
+ end # #### #### ## ## ## ## ## ## ####
41
+ stop_server # ## ### ## ## ## ## ###### ### ##
42
+ end # ## ## ## ## ## ## ## ##
43
+ rescue => e # ## ## ## ## ## ## ## ## ####
44
+ g e # ## ## ####### ## ######## ## ## ####
45
+ end #
46
+ end #
47
+ #
48
+ Thread.start do #
49
+ m.synchronize { c.wait(m) } # <###
45
50
  loop do
46
- break if @ws && @ws.instance_variable_get(:@socket).closed?
47
51
  if @ws
48
52
  begin
49
53
  @ws.send(escape(output))
@@ -54,13 +58,23 @@ class WsIo
54
58
  end
55
59
  end
56
60
 
57
- threads.each do |thread|
58
- thread.join
61
+ Thread.start do
62
+ begin
63
+ yield
64
+ rescue => e
65
+ g e
66
+ ensure
67
+ unfake_io
68
+ stop_server
69
+ end
59
70
  end
71
+
72
+ self
60
73
  rescue SignalException, StandardError => e
61
74
  g e
62
75
  unfake_io
63
76
  stop_server
77
+ raise
64
78
  rescue Exception => e
65
79
  g e
66
80
  unfake_io
@@ -68,6 +82,23 @@ class WsIo
68
82
  raise
69
83
  end
70
84
 
85
+ def after
86
+ yield
87
+ self
88
+ end
89
+
90
+ def join
91
+ @server_thread.join if @server_thread
92
+ end
93
+
94
+ def open
95
+ tempfile = Tempfile.open('ws-io')
96
+ tempfile << ERB.new(File.read(File.expand_path('../index.html.erb', __FILE__))).result(binding)
97
+ tempfile.flush
98
+ Launchy::Browser.run(tempfile.path)
99
+ self
100
+ end
101
+
71
102
  def fake_io
72
103
  @in_read, @in_write = IO.pipe
73
104
  @stdin = STDIN.clone
@@ -84,17 +115,20 @@ class WsIo
84
115
  STDIN.reopen(@stdin)
85
116
  STDOUT.reopen(@stdout)
86
117
  STDERR.reopen(@stderr)
87
- @in_read.close
88
- @in_write.close
89
- @out_write.close
90
- @out_read.close
118
+ [@in_read, @in_write, @out_read, @out_write].each do |io|
119
+ io.close unless io.closed?
120
+ end
91
121
  g 'unfake_io'
122
+ rescue => e
123
+ g e
92
124
  end
93
125
 
94
126
  def stop_server
95
127
  ws.close
96
128
  @server.tcp_server.close
97
129
  g 'stop_server'
130
+ rescue => e
131
+ g e
98
132
  end
99
133
 
100
134
  def stop
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ws-io
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.1
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - jugyo
@@ -11,7 +11,7 @@ bindir: bin
11
11
  cert_chain: []
12
12
 
13
13
  date: 2011-02-10 00:00:00 +09:00
14
- default_executable:
14
+ default_executable: ws-irb
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: web-socket-ruby
@@ -70,8 +70,8 @@ dependencies:
70
70
  version_requirements: *id005
71
71
  description: It provides a bridge between WebSocket and IO
72
72
  email: jugyo.org@gmail.com
73
- executables: []
74
-
73
+ executables:
74
+ - ws-irb
75
75
  extensions: []
76
76
 
77
77
  extra_rdoc_files:
@@ -86,8 +86,9 @@ files:
86
86
  - README.rdoc
87
87
  - Rakefile
88
88
  - VERSION
89
- - examples/index.html
89
+ - bin/ws-irb
90
90
  - examples/sh.rb
91
+ - lib/index.html.erb
91
92
  - lib/ws-io.rb
92
93
  - spec/spec_helper.rb
93
94
  - spec/ws-io_spec.rb