ws-io 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 jugyo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,31 @@
1
+ = ws-io
2
+
3
+ The bridge between WebSocket and IO
4
+
5
+ == Install
6
+
7
+ gem install ws-io
8
+
9
+ == Example
10
+
11
+ require "ws-io"
12
+
13
+ WsIo.start(['*'], 8080) do
14
+ system '/bin/sh'
15
+ end
16
+
17
+ See also the ./examples.
18
+
19
+ == Contributing to ws-io
20
+
21
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
22
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
23
+ * Fork the project
24
+ * Start a feature/bugfix branch
25
+ * Commit and push until you are happy with your contribution
26
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2011 jugyo. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "ws-io"
16
+ gem.homepage = "http://github.com/jugyo/ws-io"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{The bridge between WebSocket and IO}
19
+ gem.description = %Q{It provides a bridge between WebSocket and IO}
20
+ gem.email = "jugyo.org@gmail.com"
21
+ gem.authors = ["jugyo"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'web-socket-ruby', '> 0.1.0'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "ws-io #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,60 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js?v=1"></script>
5
+ <script type="text/javascript" charset="utf-8">
6
+ $(function() {
7
+ var ws = new WebSocket("ws://127.0.0.1:8080");
8
+ ws.onmessage = function(e) {
9
+ $("#result").append($("<div>").html(e.data));
10
+ $("body").scrollTop(document.height);
11
+ };
12
+ ws.onclose = function() {
13
+ $("#result").append($("<div class='alert'>").text("closed"));
14
+ };
15
+ ws.onerror = function() {
16
+ $("#result").append($("<div class='alert'>").text("error!"));
17
+ };
18
+
19
+ $.eval = function() {
20
+ ws.send($("#input").val());
21
+ $("#input").val('').focus();
22
+ }
23
+
24
+ $('#input').keyup(function(e) {
25
+ if(e.keyCode == 13) {
26
+ $.eval()
27
+ }
28
+ });
29
+
30
+ $("#input").focus();
31
+ });
32
+ </script>
33
+ <style type="text/css" media="screen">
34
+ body {
35
+ padding: 10px;
36
+ margin: 0;
37
+ }
38
+ pre {
39
+ font-size: 13px;
40
+ font-family: monospace;
41
+ margin: 0;
42
+ padding: 0;
43
+ }
44
+ #input {
45
+ font-size: 13px;
46
+ font-family: monospace;
47
+ margin: 0;
48
+ padding: 0;
49
+ border: none;
50
+ outline: none;
51
+ }
52
+ .alert {
53
+ color: red;
54
+ }
55
+ </style>
56
+ </head>
57
+ <body>
58
+ <pre><span id="result"></span><span id="prompt">&gt; </span><input type="text" id="input" style="width: 400px" /></pre>
59
+ </body>
60
+ </html>
data/examples/sh.rb ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.dirname(__FILE__) + "/../lib"
3
+ require "ws-io"
4
+
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 }
data/lib/ws-io.rb ADDED
@@ -0,0 +1,112 @@
1
+ require 'web_socket'
2
+ require 'erb'
3
+
4
+ class WsIo
5
+ class << self
6
+ attr_accessor :ws
7
+
8
+ if ENV['WSIO_DEBUG']
9
+ require 'g'
10
+ else
11
+ def g(*args);end
12
+ end
13
+
14
+ def start(domains = ["*"], port = 8080)
15
+ fake_io
16
+
17
+ threads = []
18
+
19
+ threads << Thread.start do
20
+ @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
+ begin
37
+ yield
38
+ ensure
39
+ stop_server
40
+ end
41
+ end
42
+
43
+ threads << Thread.start do
44
+ loop do
45
+ break if @ws && @ws.instance_variable_get(:@socket).closed?
46
+ if @ws
47
+ begin
48
+ @ws.send(escape(output))
49
+ rescue
50
+ # ignore!
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ threads.each do |thread|
57
+ thread.join
58
+ end
59
+ rescue SignalException, StandardError
60
+ unfake_io
61
+ rescue Exception
62
+ unfake_io
63
+ raise
64
+ end
65
+
66
+ def fake_io
67
+ @in_read, @in_write = IO.pipe
68
+ @stdin = STDIN.clone
69
+ STDIN.reopen(@in_read)
70
+ @stdout = STDOUT.clone
71
+ @stderr = STDERR.clone
72
+ @out_read, @out_write = IO.pipe
73
+ STDOUT.reopen(@out_write)
74
+ STDERR.reopen(@out_write)
75
+ g 'fake_io'
76
+ end
77
+
78
+ def unfake_io
79
+ STDIN.reopen(@stdin)
80
+ STDOUT.reopen(@stdout)
81
+ STDERR.reopen(@stderr)
82
+ @in_read.close
83
+ @in_write.close
84
+ @out_write.close
85
+ @out_read.close
86
+ g 'unfake_io'
87
+ end
88
+
89
+ def stop_server
90
+ ws.close
91
+ @server.tcp_server.close
92
+ g 'stop_server'
93
+ end
94
+
95
+ def stop
96
+ stop_server
97
+ unfake_io
98
+ end
99
+
100
+ def input(msg)
101
+ @in_write.puts(msg + "\n")
102
+ end
103
+
104
+ def output
105
+ @out_read.gets
106
+ end
107
+
108
+ def escape(output)
109
+ ERB::Util.html_escape(output.gsub(/\e\[[^m]+m/, '')) if output
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'ws-io'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "WsIo" do
4
+ pending
5
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ws-io
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - jugyo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-09 00:00:00 +09:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.2
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description: The bridge between WebSocket and IO
61
+ email: jugyo.org@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .rspec
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - examples/index.html
79
+ - examples/sh.rb
80
+ - lib/ws-io.rb
81
+ - spec/spec_helper.rb
82
+ - spec/ws-io_spec.rb
83
+ has_rdoc: true
84
+ homepage: http://github.com/jugyo/ws-io
85
+ licenses:
86
+ - MIT
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: -624885764972321820
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.5.0
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: The bridge between WebSocket and IO
114
+ test_files:
115
+ - examples/sh.rb
116
+ - spec/spec_helper.rb
117
+ - spec/ws-io_spec.rb