socket.io 0.0.1 → 0.0.2
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.
- data/README.markdown +60 -4
- data/lib/socket.io.rb +11 -0
- data/lib/socket.io/actions.rb +1 -0
- data/lib/socket.io/actions/handshake_action.rb +23 -0
- data/lib/socket.io/version.rb +1 -1
- data/socket.io.gemspec +4 -2
- metadata +40 -13
data/README.markdown
CHANGED
@@ -1,8 +1,64 @@
|
|
1
|
-
|
1
|
+
# socket.io-ruby
|
2
2
|
|
3
|
-
|
3
|
+
[Socket.io] for the Ruby Kids. A plugin for [Cramp](http://cramp.in/). Socket.io is a library that makes writing cross-borwser websockets super easy and resilient.
|
4
4
|
|
5
|
-
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
This implementation Socket.io works within an existing [cramp](http://cramp.in/) app.
|
8
|
+
Cramp is similar in spirit to Rails but specializes in evented web apps. Simply
|
9
|
+
`gem install cramp` and run `cramp new myapp`.
|
10
|
+
|
11
|
+
Define a new Cramp::Action and include SocketIo::Websocket. Then define which
|
12
|
+
method should handle certain events and messages.
|
13
|
+
|
14
|
+
# app/actions/akbar_action.rb
|
15
|
+
|
16
|
+
class AkbarAction < Cramp::Websocket
|
17
|
+
include SocketIo::Websocket
|
18
|
+
|
19
|
+
on :message, :read_message
|
20
|
+
on :trap, :report_trap
|
21
|
+
|
22
|
+
# This is fired when the client sends a standard
|
23
|
+
# Socket.io message
|
24
|
+
def read_message(data)
|
25
|
+
puts "Our Bothan spies report #{data}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def report_trap(imperial_fleet)
|
29
|
+
puts "It's a trap!"
|
30
|
+
puts "Concentrate all firepower on #{imperial_fleet['super_stardestroyer']}"
|
31
|
+
return { 'move' => 'sector 227' }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
By default, Cramp uses HttpRouter in config/routes.rb to manage connections. SocketIo has a handy helper for that:
|
36
|
+
|
37
|
+
# config/routes.rb
|
38
|
+
|
39
|
+
require 'socket.io'
|
40
|
+
require 'http_router'
|
41
|
+
|
42
|
+
HttpRouter.new do
|
43
|
+
SocketIo.routes self, AkbarAction
|
44
|
+
end
|
45
|
+
|
46
|
+
Optionally, SocketIo.routes can take a path. The default is `/socket.io`.
|
47
|
+
|
48
|
+
Throw that in with some [Thin](http://code.macournoyer.com/thin/) or [Rainbows](http://rainbows.rubyforge.org), and you've got yourself a websocket server! Here's the client-side code to make it all happen:
|
49
|
+
|
50
|
+
<script type="text/javascript" src="socket.io-client.js"></script>
|
51
|
+
<script type="text/javascript">
|
52
|
+
var socket = io.connect('http://localhost/socket.io');
|
53
|
+
socket.on('connect', function () {
|
54
|
+
socket.send('the Emperor is building a new Death Star');
|
55
|
+
socket.emit('trap', { 'super_stardestroyer': 'Executor' }, function(instructions) {
|
56
|
+
alert('moving to ' + instructions['move']);
|
57
|
+
});
|
58
|
+
});
|
59
|
+
</script>
|
60
|
+
|
61
|
+
## Note on Patches/Pull Requests
|
6
62
|
|
7
63
|
* Fork the project.
|
8
64
|
* Make your feature addition or bug fix.
|
@@ -12,6 +68,6 @@ Description goes here.
|
|
12
68
|
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
69
|
* Send me a pull request. Bonus points for topic branches.
|
14
70
|
|
15
|
-
|
71
|
+
## Copyright
|
16
72
|
|
17
73
|
Copyright (c) 2011 Derek Kastner. See LICENSE for details.
|
data/lib/socket.io.rb
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
require 'socket.io/packet'
|
2
2
|
require 'socket.io/websocket'
|
3
|
+
require 'socket.io/actions'
|
3
4
|
|
4
5
|
module SocketIo
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def routes(router, handler, path = '/socket.io')
|
9
|
+
router.add("#{path}/:protocol_version").to(HandshakeAction)
|
10
|
+
router.add("#{path}/:protocol_version/websocket/:session_id").to(handler)
|
11
|
+
#router.add("#{path}/:protocol_version/flashsocket/:session_id").to(ImpactAction)
|
12
|
+
#router.add("#{path}/:protocol_version/xhr-polling/:session_id").to(ImpactXhrPollingAction)
|
13
|
+
#router.add("#{path}/:protocol_version/xhr-polling/:session_id/:verified").to(ImpactXhrPollingAction)
|
14
|
+
#router.add("#{path}/:protocol_version/xhr-polling/:session_id/send").to(ImpactXhrPollingAction)
|
15
|
+
end
|
5
16
|
end
|
6
17
|
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'socket.io/actions/handshake_action'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'cramp'
|
2
|
+
require 'uuid'
|
3
|
+
|
4
|
+
class HandshakeAction < Cramp::Action
|
5
|
+
HEARTBEAT_TIMEOUT = 27
|
6
|
+
CLOSING_CONNECTION_TIMEOUT = 10
|
7
|
+
#TRANSPORTS = 'websocket,xhr-polling,flashsocket'
|
8
|
+
TRANSPORTS = 'websocket'
|
9
|
+
|
10
|
+
def uuid
|
11
|
+
@uuid ||= UUID.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def session_id
|
15
|
+
uuid.generate
|
16
|
+
end
|
17
|
+
|
18
|
+
def start
|
19
|
+
body = [session_id, HEARTBEAT_TIMEOUT, CLOSING_CONNECTION_TIMEOUT, TRANSPORTS].join(':')
|
20
|
+
render body
|
21
|
+
finish
|
22
|
+
end
|
23
|
+
end
|
data/lib/socket.io/version.rb
CHANGED
data/socket.io.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
|
|
5
5
|
s.name = 'socket.io'
|
6
6
|
s.version = SocketIo::VERSION
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
|
-
s.date =
|
8
|
+
s.date = "2011-11-22"
|
9
9
|
s.authors = ['Derek Kastner']
|
10
10
|
s.email = 'dkastner@gmail.com'
|
11
11
|
s.homepage = 'http://github.com/dkastner/socket.io-ruby'
|
@@ -27,9 +27,11 @@ Gem::Specification.new do |s|
|
|
27
27
|
|
28
28
|
|
29
29
|
s.add_dependency 'cramp', '~> 0.15.1'
|
30
|
+
s.add_dependency 'uuid'
|
30
31
|
|
31
|
-
s.add_development_dependency '
|
32
|
+
s.add_development_dependency 'bueller'
|
32
33
|
s.add_development_dependency 'bundler'
|
33
34
|
s.add_development_dependency 'rake'
|
34
35
|
s.add_development_dependency 'rcov'
|
36
|
+
s.add_development_dependency 'rspec'
|
35
37
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socket.io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cramp
|
16
|
-
requirement: &
|
16
|
+
requirement: &2166458340 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: 0.15.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2166458340
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: uuid
|
27
|
+
requirement: &2166457160 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2166457160
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bueller
|
38
|
+
requirement: &2166455480 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,10 +43,10 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *2166455480
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: bundler
|
38
|
-
requirement: &
|
49
|
+
requirement: &2166453420 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *2166453420
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: rake
|
49
|
-
requirement: &
|
60
|
+
requirement: &2166452800 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,10 +65,21 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *2166452800
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: rcov
|
60
|
-
requirement: &
|
71
|
+
requirement: &2166451400 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *2166451400
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rspec
|
82
|
+
requirement: &2166450040 !ruby/object:Gem::Requirement
|
61
83
|
none: false
|
62
84
|
requirements:
|
63
85
|
- - ! '>='
|
@@ -65,7 +87,7 @@ dependencies:
|
|
65
87
|
version: '0'
|
66
88
|
type: :development
|
67
89
|
prerelease: false
|
68
|
-
version_requirements: *
|
90
|
+
version_requirements: *2166450040
|
69
91
|
description: A plugin for Cramp that enables websockets using the Socket.io protocol
|
70
92
|
email: dkastner@gmail.com
|
71
93
|
executables: []
|
@@ -81,6 +103,8 @@ files:
|
|
81
103
|
- README.markdown
|
82
104
|
- Rakefile
|
83
105
|
- lib/socket.io.rb
|
106
|
+
- lib/socket.io/actions.rb
|
107
|
+
- lib/socket.io/actions/handshake_action.rb
|
84
108
|
- lib/socket.io/packet.rb
|
85
109
|
- lib/socket.io/packet/ack_packet.rb
|
86
110
|
- lib/socket.io/packet/connect_packet.rb
|
@@ -112,6 +136,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
136
|
- - ! '>='
|
113
137
|
- !ruby/object:Gem::Version
|
114
138
|
version: '0'
|
139
|
+
segments:
|
140
|
+
- 0
|
141
|
+
hash: -4017494604457113772
|
115
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
143
|
none: false
|
117
144
|
requirements:
|