celluloid-websocket 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cedcb4a302c02dba995a888f608bd1a925267660
4
+ data.tar.gz: 04158005fe81ce1264066d51249ffe7bac99aaeb
5
+ SHA512:
6
+ metadata.gz: 16c9a762d83d2a4d95cd2d0db1b17d897ada642879f40e27167897054dfd99c9ff6bfb3632433fd25a8010ca1c1633f832561599645b81a8d3ba6f49d8051b5b
7
+ data.tar.gz: ad9a96ec44fc288cfefd8d6077fca526a010dfe4b9dc8a617d88b61346b332022e25eea844566ec32f98646f64a55188808b7858f2f5cf1d803af15d3135a6e7
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.pid
19
+ *.log
20
+ *.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in celluloid-websocket-rack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tinco Andringa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Celluloid::WebSocket
2
+
3
+ Lets you make a websocket rack application using Celluloid.
4
+
5
+ With thanks to Tony Arcieri for his awesome Celluloid libraries.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'celluloid-websocket'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install celluloid-websocket
20
+
21
+ ## Usage
22
+
23
+ Simply inherit your Rack app from Celluloid::WebSocket::Rack, and implement the `on_open` method and using the `read` and `write` functions to implement a protocol.
24
+
25
+ ```
26
+ require 'celluloid/websocket/rack'
27
+
28
+ class WebSocketEcho < Celluloid::WebSocket::Rack
29
+ def on_open
30
+ @counter = 0
31
+
32
+ while(true)
33
+ message = read
34
+ @counter += 1
35
+ write("#{@counter}: #{message}")
36
+ end
37
+ end
38
+ end
39
+
40
+ run(WebSocketEcho)
41
+ ```
42
+
43
+ You can try it out by cloning this repository running `bundle` and then:
44
+
45
+ ```
46
+ passenger start -R examples/message_counter.ru
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'celluloid/websocket/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "celluloid-websocket"
8
+ spec.version = Celluloid::WebSocket::VERSION
9
+ spec.authors = ["Tinco Andringa"]
10
+ spec.email = ["mail@tinco.nl"]
11
+ spec.description = "Lets you make a websocket rack application using Celluloid."
12
+ spec.summary = "This is basically a small wrapper around Tony Arcieri's Reel::Websocket class."
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "http"
25
+ spec.add_dependency "websocket-driver", '>= 0.5.1'
26
+ spec.add_dependency "rack"
27
+ spec.add_dependency "celluloid"
28
+ spec.add_dependency "celluloid-io"
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'celluloid/websocket/rack'
2
+
3
+ class WebSocketEcho < Celluloid::WebSocket::Rack
4
+ def on_open
5
+ @counter = 0
6
+
7
+ while(true)
8
+ message = read
9
+
10
+ @counter += 1
11
+ write("#{@counter}: #{message}")
12
+ end
13
+ end
14
+
15
+ def on_error(*args)
16
+ puts args.inspect
17
+ end
18
+ end
19
+
20
+ run(WebSocketEcho)
@@ -0,0 +1,35 @@
1
+ require 'forwardable'
2
+ require 'celluloid/io'
3
+
4
+ module Celluloid
5
+ module IO
6
+ # Wraps a socket as gotten from the Rack hijack API
7
+ class RackSocket < Stream
8
+ extend Forwardable
9
+
10
+ def_delegators :@socket, :read_nonblock, :write_nonblock, :close, :close_read, :close_write, :closed?
11
+
12
+ def initialize(socket)
13
+ super()
14
+ @socket = socket
15
+ end
16
+
17
+ def to_io
18
+ @socket
19
+ end
20
+
21
+ # Receives a message
22
+ def recv(maxlen, flags=nil)
23
+ raise NotImplementedError, "flags not supported" if flags && !flags.zero?
24
+ readpartial(maxlen)
25
+ end
26
+
27
+ # Send a message
28
+ def send(msg, flags, dest_sockaddr = nil)
29
+ raise NotImplementedError, "dest_sockaddr not supported" if dest_sockaddr
30
+ raise NotImplementedError, "flags not supported" unless flags.zero?
31
+ write(msg)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,103 @@
1
+ require 'forwardable'
2
+ require 'websocket/driver'
3
+ require 'uri'
4
+ require 'rack/request'
5
+
6
+ module Celluloid
7
+ class WebSocket
8
+ extend Forwardable
9
+
10
+ attr_accessor :socket, :env
11
+
12
+ def initialize(env, socket)
13
+ @env = env
14
+ @socket = socket
15
+
16
+ driver_env = DriverEnvironment.new(env, socket)
17
+
18
+ @driver = ::WebSocket::Driver.rack(driver_env)
19
+
20
+ @driver.on(:close) do
21
+ @socket.close
22
+ end
23
+
24
+ @message_stream = MessageStream.new(socket, @driver)
25
+
26
+ @driver.start
27
+ rescue EOFError
28
+ close
29
+ end
30
+
31
+ def read
32
+ @message_stream.read
33
+ end
34
+
35
+ def closed?
36
+ @socket.closed?
37
+ end
38
+
39
+ def write(msg)
40
+ if msg.is_a? String
41
+ @driver.text(msg)
42
+ elsif msg.is_a? Array
43
+ @driver.binary(msg)
44
+ else
45
+ raise "Can only send byte array or string over driver."
46
+ end
47
+ rescue IOError, Errno::ECONNRESET, Errno::EPIPE
48
+ cancel_timer!
49
+ raise SocketError, "error writing to socket"
50
+ rescue
51
+ cancel_timer!
52
+ raise
53
+ end
54
+ alias_method :<<, :write
55
+
56
+ def close
57
+ @driver.close
58
+ @socket.close
59
+ end
60
+
61
+ private
62
+
63
+ class DriverEnvironment
64
+ extend Forwardable
65
+
66
+ attr_reader :env, :url, :socket
67
+
68
+ def_delegators :socket, :write
69
+
70
+ def initialize(env, socket)
71
+ @env = env
72
+
73
+ secure = ::Rack::Request.new(env).ssl?
74
+ scheme = secure ? 'wss:' : 'ws:'
75
+ @url = scheme + '//' + env['HTTP_HOST'] + env['REQUEST_URI']
76
+
77
+ @socket = socket
78
+ end
79
+ end
80
+
81
+ class MessageStream
82
+ BUFFER_SIZE = 16384
83
+
84
+ def initialize(socket, driver)
85
+ @socket = socket
86
+ @driver = driver
87
+ @message_buffer = []
88
+
89
+ @driver.on :message do |message|
90
+ @message_buffer.push(message.data)
91
+ end
92
+ end
93
+
94
+ def read
95
+ while @message_buffer.empty?
96
+ buffer = @socket.readpartial(BUFFER_SIZE)
97
+ @driver.parse(buffer)
98
+ end
99
+ @message_buffer.shift
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,42 @@
1
+ require 'celluloid'
2
+ require 'celluloid/io'
3
+ require 'celluloid/websocket'
4
+ require 'celluloid/io/rack_socket'
5
+ require 'rack/request'
6
+ require 'forwardable'
7
+
8
+ module Celluloid
9
+ class WebSocket
10
+ class Rack
11
+ extend Forwardable
12
+ include Celluloid::IO
13
+ include Celluloid::Logger
14
+
15
+ finalizer :shutdown
16
+
17
+ def_delegators :@websocket, :socket, :env, :addr, :peeraddr, :read, :read_every, :write, :<<, :closed?, :close, :cancel_timer!
18
+
19
+ def self.call(env)
20
+ if env['HTTP_UPGRADE'].nil? || env['HTTP_UPGRADE'].downcase != 'websocket'
21
+ return [400, {}, "No Upgrade header or Upgrade not for websocket."]
22
+ end
23
+
24
+ env['rack.hijack'].call
25
+ socket = Celluloid::IO::RackSocket.new(env['rack.hijack_io'].to_io)
26
+
27
+ actor = new()
28
+ actor.async.initialize_websocket(env, socket)
29
+ [200,{},""]
30
+ end
31
+
32
+ def initialize_websocket(req, socket)
33
+ @websocket = WebSocket.new(req, socket)
34
+ on_open if respond_to? :on_open
35
+ end
36
+
37
+ def shutdown
38
+ @websocket.close if @websocket
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module Celluloid
2
+ class WebSocket
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: celluloid-websocket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tinco Andringa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: http
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: websocket-driver
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: celluloid
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: celluloid-io
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Lets you make a websocket rack application using Celluloid.
112
+ email:
113
+ - mail@tinco.nl
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - celluloid-websocket.gemspec
124
+ - examples/message_counter.ru
125
+ - lib/celluloid/io/rack_socket.rb
126
+ - lib/celluloid/websocket.rb
127
+ - lib/celluloid/websocket/rack.rb
128
+ - lib/celluloid/websocket/version.rb
129
+ homepage: ''
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.2.2
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: This is basically a small wrapper around Tony Arcieri's Reel::Websocket class.
153
+ test_files: []