utopia-websocket 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ad16603ab4f1a0acf1fea4afe634779c9a9b66a5
4
+ data.tar.gz: ef3d22a74c985d782a23e08a2663a80d6916e706
5
+ SHA512:
6
+ metadata.gz: 4fbb381b547523cf738df4e10f23abf021ba38bdbb23a9dd1b768cd0bfde6952c40b6288edfc0d37f223e3b77ec4649e97ff51c2ee4e92bc25f342d4a1da1411
7
+ data.tar.gz: 0e831028b1ca73a9764934820c50b143e0cfa6340ec32e2d0284b8c8033312713a28337751f3609a188ab007541ccac783eaab52c138705cec656f90ed7f4aff
@@ -0,0 +1,9 @@
1
+
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ if ENV['TRAVIS']
7
+ require 'coveralls'
8
+ Coveralls.wear!
9
+ end
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0"
4
+ - "2.1"
5
+ - "2.2"
6
+ env: COVERAGE=true
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in utopia-websocket.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'puma'
8
+ gem 'utopia'
9
+
10
+ gem 'rack-test'
11
+ gem 'simplecov'
12
+ gem 'coveralls', require: false
13
+ end
@@ -0,0 +1,100 @@
1
+ # Utopia::Websocket
2
+
3
+ A simple synchronous websocket server implementation. Designed for short term connections and best on thread-per-request application servers (e.g. puma).
4
+
5
+ [![Build Status](https://secure.travis-ci.org/ioquatix/utopia-websocket.png)](http://travis-ci.org/ioquatix/utopia-websocket)
6
+ [![Code Climate](https://codeclimate.com/github/ioquatix/utopia-websocket.png)](https://codeclimate.com/github/ioquatix/utopia-websocket)
7
+ [![Coverage Status](https://coveralls.io/repos/ioquatix/utopia-websocket/badge.svg)](https://coveralls.io/r/ioquatix/utopia-websocket)
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'utopia-websocket'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install utopia-websocket
23
+
24
+ ## Usage
25
+
26
+ Here is how to use within `Utopia::Controller`:
27
+
28
+ on 'list' do |request|
29
+ fail! unless Utopia::WebSocket?(request.env)
30
+
31
+ Utopia::WebSocket.open(request.env) do |connection|
32
+ read, write = IO.pipe
33
+
34
+ Process.spawn("ls -lah", :out => write)
35
+ write.close
36
+
37
+ read.each_line do |line|
38
+ connection.text(line)
39
+ end
40
+
41
+ connection.close
42
+ end
43
+
44
+ success!
45
+ end
46
+
47
+ `connection` is an instance of [`WebSocket::Driver`][1].
48
+
49
+ [1]: https://github.com/faye/websocket-driver-ruby
50
+
51
+ If you want to handle incoming messages, you must listen for these and then handle them:
52
+
53
+ on 'echo' do |request|
54
+ fail! unless Utopia::WebSocket?(request.env)
55
+
56
+ Utopia::WebSocket.open(request.env) do |connection|
57
+ connection.on(:message) do |event|
58
+ connection.text(event.data)
59
+ connection.close
60
+ end
61
+ end
62
+
63
+ success!
64
+ end
65
+
66
+ This implementation is currently not designed for many long-term connections as it will 'use' one request for the duration of the websocket life. However, this model is rather simple and easy to reason about. For more complex client/server interactions, a dedicated server using [Celluloid][2] might be more appropriate.
67
+
68
+ [2]: https://github.com/celluloid/celluloid
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
77
+
78
+ ## License
79
+
80
+ Released under the MIT license.
81
+
82
+ Copyright, 2015, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining a copy
85
+ of this software and associated documentation files (the "Software"), to deal
86
+ in the Software without restriction, including without limitation the rights
87
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
+ copies of the Software, and to permit persons to whom the Software is
89
+ furnished to do so, subject to the following conditions:
90
+
91
+ The above copyright notice and this permission notice shall be included in
92
+ all copies or substantial portions of the Software.
93
+
94
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
95
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
96
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
97
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
99
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
100
+ THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ["--require", "simplecov"] if ENV['COVERAGE']
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,87 @@
1
+ # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require "utopia/websocket/version"
22
+ require 'websocket/driver'
23
+
24
+ module Utopia
25
+ def self.WebSocket?(env)
26
+ ::WebSocket::Driver.websocket?(env)
27
+ end
28
+
29
+ module WebSocket
30
+ class Connection
31
+ READ_BUFFER_SIZE = 1024*8
32
+
33
+ attr_reader :env, :url
34
+
35
+ def initialize(env, io)
36
+ @env = env
37
+ @io = io
38
+
39
+ scheme = Rack::Request.new(env).ssl? ? 'wss:' : 'ws:'
40
+ @url = scheme + '//' + env['HTTP_HOST'] + env['REQUEST_URI']
41
+
42
+ @driver = ::WebSocket::Driver.rack(self)
43
+ @running = false
44
+ end
45
+
46
+ def write(string)
47
+ @io.write(string)
48
+ end
49
+
50
+ def read
51
+ @driver.parse(@io.readpartial(READ_BUFFER_SIZE))
52
+ end
53
+
54
+ def run(&handler)
55
+ @running = true
56
+
57
+ @driver.on(:close) do
58
+ @running = false
59
+ end
60
+
61
+ @driver.on(:open) do
62
+ yield @driver if block_given?
63
+ end
64
+
65
+ @driver.start
66
+
67
+ while @running
68
+ self.read
69
+ end
70
+ ensure
71
+ @io.close
72
+ end
73
+ end
74
+
75
+ def self.open(env)
76
+ if ::WebSocket::Driver.websocket?(env)
77
+ env['rack.hijack'].call
78
+
79
+ connection = Connection.new(env, env['rack.hijack_io'])
80
+
81
+ connection.run do |driver|
82
+ yield driver
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,54 @@
1
+ # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require "utopia/websocket/version"
22
+ require 'websocket/driver'
23
+
24
+ module Utopia
25
+ module WebSocket
26
+ # This is a basic synchronous websocket client:
27
+ class Client
28
+ attr :url
29
+ attr :driver
30
+
31
+ def initialize(url, socket)
32
+ @url = url
33
+ @socket = socket
34
+
35
+ @driver = ::WebSocket::Driver.client(self)
36
+
37
+ @driver.start
38
+ end
39
+
40
+ def write(data)
41
+ @socket.write(data)
42
+ end
43
+
44
+ def read
45
+ while true
46
+ data = @socket.readpartial(1024)
47
+ @driver.parse(data)
48
+ end
49
+ rescue EOFError
50
+ nil
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Utopia
2
+ module WebSocket
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,65 @@
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'utopia/websocket'
22
+ require 'utopia/websocket/client'
23
+
24
+ require 'rack/test'
25
+ require 'puma'
26
+
27
+ module Utopia::WebSocket::ConnectionSpec
28
+ describe Utopia::WebSocket::Connection do
29
+ include Rack::Test::Methods
30
+
31
+ before(:all) do
32
+ @app = Rack::Builder.parse_file(File.expand_path('../connection_spec.ru', __FILE__)).first
33
+
34
+ @server = Puma::Server.new(@app)
35
+
36
+ @server.add_tcp_listener('localhost', 8085)
37
+
38
+ @server.run
39
+
40
+ trap(:SIGINT) do
41
+ @server.stop if @server
42
+ exit
43
+ end
44
+ end
45
+
46
+ after(:all) do
47
+ @server.stop
48
+ @server = nil
49
+ end
50
+
51
+ it "should connect to the websocket server" do
52
+ client = Utopia::WebSocket::Client.new("ws://localhost:8085/list", TCPSocket.new('localhost', 8085))
53
+
54
+ events = []
55
+
56
+ client.driver.on(:message) do |event|
57
+ events << event
58
+ end
59
+
60
+ client.read
61
+
62
+ expect(events.size).to be > 0
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,7 @@
1
+
2
+ require 'utopia'
3
+ require 'utopia/websocket'
4
+
5
+ use Utopia::Controller, root: File.expand_path('../pages', __FILE__)
6
+
7
+ run lambda {|env| [404, {}, []]}
@@ -0,0 +1,19 @@
1
+
2
+ on 'list' do |request|
3
+ if Utopia::WebSocket?(request.env)
4
+ Utopia::WebSocket.open(request.env) do |connection|
5
+ read, write = IO.pipe
6
+
7
+ Process.spawn("ls -lah", :out => write)
8
+ write.close
9
+
10
+ read.each_line do |line|
11
+ connection.text(line)
12
+ end
13
+
14
+ connection.close
15
+ end
16
+
17
+ success!
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'utopia/websocket/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "utopia-websocket"
8
+ spec.version = Utopia::WebSocket::VERSION
9
+ spec.authors = ["Samuel Williams"]
10
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
11
+ spec.summary = %q{A basic rack.hijack websocket implementation with synchronous execution within rack.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "websocket-driver", "~> 0.5.1"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rspec", "~> 3.1.0"
24
+ spec.add_development_dependency "puma"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utopia-websocket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: websocket-driver
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: puma
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - samuel.williams@oriontransfer.co.nz
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".simplecov"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - lib/utopia/websocket.rb
96
+ - lib/utopia/websocket/client.rb
97
+ - lib/utopia/websocket/version.rb
98
+ - spec/utopia/websocket/connection_spec.rb
99
+ - spec/utopia/websocket/connection_spec.ru
100
+ - spec/utopia/websocket/pages/controller.rb
101
+ - utopia-websocket.gemspec
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A basic rack.hijack websocket implementation with synchronous execution within
126
+ rack.
127
+ test_files:
128
+ - spec/utopia/websocket/connection_spec.rb
129
+ - spec/utopia/websocket/connection_spec.ru
130
+ - spec/utopia/websocket/pages/controller.rb