tubesock 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +1 -0
- data/lib/tubesock.rb +83 -0
- data/lib/tubesock/hijack.rb +22 -0
- data/lib/tubesock/version.rb +3 -0
- data/tubesock.gemspec +26 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3b47058bf07fdf65340f9ee65ee32cdc39a28b5b
|
4
|
+
data.tar.gz: cb112f9c8bf14fab05f7b100b7b8cb8ff11cd003
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 41b5e67e9984af275cd40f36277f866495fabd0da6fb1b4a7258ef493d1f3b9562e09e1c2e494877a99af36ad0ea7ef4c79e646a6f79279372e419abec06430c
|
7
|
+
data.tar.gz: 0284696e54e55969f29e6e6ac4350bbda3b5fc0d42ad6f6af35804d6bb85706b593f37c3fe364b5ce55eda0ae654d2535a04f5573d6bc24089139783cfdfe40f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Nick Gauthier
|
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,79 @@
|
|
1
|
+
# Tubesock
|
2
|
+
|
3
|
+
Tubesock lets you use websockets from rack and rails 4+ by using Rack's new hijack interface to access the underlying socket connection.
|
4
|
+
|
5
|
+
In contrast to other websocket libraries, Tubesock does not use a reactor (read: no eventmachine). Instead, it leverages Rails 4's new full-stack concurrency support. Note that this means you must use a concurrent server. We recommend Puma.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'tubesock'
|
13
|
+
# currently, the ability to setup a websocket from rack is only
|
14
|
+
# available on my fork. If/when the PR is merged this will become a gem dependency
|
15
|
+
gem 'websocket', github: "ngauthier/websocket-ruby", ref: "8fc3bbc8f336fb5ccac95b8707e8146e86a8002d"
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install tubesock
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Rack
|
29
|
+
|
30
|
+
To use Tubesock with rack, you need to hijack the rack environment and then return an asynchronous response. For example:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
def self.call(env)
|
34
|
+
if websocket?(env)
|
35
|
+
tubesock = hijack(env)
|
36
|
+
tubesock.listen
|
37
|
+
tubesock.onmessage do |message|
|
38
|
+
puts "Got #{message}"
|
39
|
+
end
|
40
|
+
[ -1, {}, [] ]
|
41
|
+
else
|
42
|
+
[404, {'Content-Type' => 'text/plain'}, ['Not Found']]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
NOTE: I have not gotten the above to work just yet with just puma or thin. For now, check out the rails example.
|
48
|
+
|
49
|
+
### Rails 4+
|
50
|
+
|
51
|
+
On Rails 4 there is a module you can use called `Tubesock::Hijack`. In a controller:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class ChatController < ApplicationController
|
55
|
+
include Tubesock::Hijack
|
56
|
+
|
57
|
+
def chat
|
58
|
+
hijack do |tubesock|
|
59
|
+
tubesock.onopen do
|
60
|
+
tubesock.send_data message: "Hello, friend"
|
61
|
+
end
|
62
|
+
|
63
|
+
tubesock.onmessage do |data|
|
64
|
+
tubesock.send_data message: "You said: #{data[:message]}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
For a full example, check out [Tubesock Example](http://github.com/ngauthier/tubesock-example).
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/tubesock.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "tubesock/version"
|
2
|
+
require "tubesock/hijack" if defined?(Rails)
|
3
|
+
|
4
|
+
class Tubesock
|
5
|
+
def initialize(socket, version)
|
6
|
+
@socket = socket
|
7
|
+
@version = version
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.hijack(env)
|
11
|
+
env['rack.hijack'].call
|
12
|
+
socket = env['rack.hijack_io']
|
13
|
+
|
14
|
+
handshake = WebSocket::Handshake::Server.new
|
15
|
+
handshake.headers["sec-websocket-version"] = env["HTTP_SEC_WEBSOCKET_VERSION"]
|
16
|
+
handshake.headers["sec-websocket-draft"] = env["HTTP_SEC_WEBSOCKET_DRAFT"]
|
17
|
+
handshake.headers["sec-websocket-key"] = env["HTTP_SEC_WEBSOCKET_KEY"]
|
18
|
+
handshake.headers["sec-websocket-extensions"] = env["HTTP_SEC_WEBSOCKET_EXTENSIONS"]
|
19
|
+
handshake.headers["host"] = env["HTTP_HOST"]
|
20
|
+
handshake.path = env["REQUEST_PATH"]
|
21
|
+
handshake.query = env["QUERY_STRING"]
|
22
|
+
handshake.set_version
|
23
|
+
|
24
|
+
socket.write handshake.to_s
|
25
|
+
|
26
|
+
self.new socket, handshake.version
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.websocket?(env)
|
30
|
+
env['REQUEST_METHOD'] == 'GET' and
|
31
|
+
env['HTTP_CONNECTION'] and
|
32
|
+
env['HTTP_CONNECTION'].split(/\s*,\s*/).include?('Upgrade') and
|
33
|
+
env['HTTP_UPGRADE'].downcase == 'websocket'
|
34
|
+
end
|
35
|
+
|
36
|
+
def send_data data, type = :text
|
37
|
+
frame = WebSocket::Frame::Outgoing::Server.new(version: @version, data: JSON.dump(data), type: type)
|
38
|
+
@socket.write frame.to_s
|
39
|
+
rescue IOError
|
40
|
+
close
|
41
|
+
end
|
42
|
+
|
43
|
+
def onopen(&block)
|
44
|
+
@openhandler = block
|
45
|
+
end
|
46
|
+
|
47
|
+
def onmessage(&block)
|
48
|
+
@messagehandler = block
|
49
|
+
end
|
50
|
+
|
51
|
+
def onclose(&block)
|
52
|
+
@closehandler = block
|
53
|
+
end
|
54
|
+
|
55
|
+
def listen
|
56
|
+
Thread.new do
|
57
|
+
Thread.current.abort_on_exception = true
|
58
|
+
framebuffer = WebSocket::Frame::Incoming::Server.new(version: @version)
|
59
|
+
running = true
|
60
|
+
while running
|
61
|
+
data, addrinfo = @socket.recvfrom(2000)
|
62
|
+
running = false if data == ""
|
63
|
+
framebuffer << data
|
64
|
+
while frame = framebuffer.next
|
65
|
+
data = frame.data
|
66
|
+
if data == ""
|
67
|
+
running = false
|
68
|
+
else
|
69
|
+
@messagehandler.call(HashWithIndifferentAccess.new(JSON.load(data))) if @messagehandler
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
@closehandler.call if @closehandler
|
74
|
+
@socket.close
|
75
|
+
end
|
76
|
+
@openhandler.call if @openhandler
|
77
|
+
end
|
78
|
+
|
79
|
+
def close
|
80
|
+
@socket.close if @socket.open?
|
81
|
+
@closehandler.call
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
class Tubesock
|
3
|
+
module Hijack
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
def hijack
|
8
|
+
if Tubesock.websocket?(env)
|
9
|
+
rocket = Tubesock.hijack(env)
|
10
|
+
yield rocket
|
11
|
+
rocket.listen
|
12
|
+
render text: nil, status: -1
|
13
|
+
else
|
14
|
+
render text: "Not found", status: :not_found
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/tubesock.gemspec
ADDED
@@ -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 'tubesock/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tubesock"
|
8
|
+
spec.version = Tubesock::VERSION
|
9
|
+
spec.authors = ["Nick Gauthier"]
|
10
|
+
spec.email = ["ngauthier@gmail.com"]
|
11
|
+
spec.description = %q{Websocket interface on Rack Hijack w/ Rails support}
|
12
|
+
spec.summary = %q{Handle websocket connections via Rack and Rails 4 using concurrency}
|
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_dependency "rack", "> 1.5.0"
|
22
|
+
spec.add_dependency "websocket" # TODO specify the new version when my PR is merged
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tubesock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Gauthier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>'
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.5.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>'
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.5.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: websocket
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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
|
+
description: Websocket interface on Rack Hijack w/ Rails support
|
70
|
+
email:
|
71
|
+
- ngauthier@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/tubesock.rb
|
82
|
+
- lib/tubesock/hijack.rb
|
83
|
+
- lib/tubesock/version.rb
|
84
|
+
- tubesock.gemspec
|
85
|
+
homepage: ''
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.0.0
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Handle websocket connections via Rack and Rails 4 using concurrency
|
109
|
+
test_files: []
|