rev-websocket 0.1.1 → 0.1.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/ChangeLog +5 -0
- data/README.rdoc +62 -1
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/examples/README.md +25 -0
- data/examples/echo +18 -0
- data/examples/echo.rb +31 -0
- data/examples/public/echo.css +11 -0
- data/examples/public/echo.html +60 -0
- data/examples/public/js/FABridge.js +604 -0
- data/examples/public/js/WebSocketMain.swf +0 -0
- data/examples/public/js/jquery.min.js +154 -0
- data/examples/public/js/json2.js +482 -0
- data/examples/public/js/swfobject.js +4 -0
- data/examples/public/js/web_socket.js +371 -0
- data/examples/public/shoutchat.css +63 -0
- data/examples/public/shoutchat.html +148 -0
- data/examples/shoutchat +18 -0
- data/examples/shoutchat.rb +81 -0
- data/lib/rev/websocket.rb +11 -0
- metadata +55 -16
- data/.gitignore +0 -1
data/examples/shoutchat
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
fork {
|
4
|
+
load 'shoutchat.rb'
|
5
|
+
exit 0
|
6
|
+
}
|
7
|
+
|
8
|
+
require 'webrick'
|
9
|
+
|
10
|
+
server = WEBrick::HTTPServer.new({
|
11
|
+
:Port => 8080,
|
12
|
+
:BindAddress => '127.0.0.1',
|
13
|
+
:DocumentRoot => File.dirname(__FILE__)+'/public'})
|
14
|
+
|
15
|
+
trap('INT') { server.shutdown }
|
16
|
+
|
17
|
+
server.start
|
18
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# Publisher/Subscriber-style message routing
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rev/websocket'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
class PubSub
|
8
|
+
def initialize
|
9
|
+
@subscriber = {}
|
10
|
+
@seqid = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def subscribe(&block)
|
14
|
+
sid = @seqid += 1
|
15
|
+
@subscriber[sid] = block
|
16
|
+
return sid
|
17
|
+
end
|
18
|
+
|
19
|
+
def unsubscribe(key)
|
20
|
+
@subscriber.delete(key)
|
21
|
+
end
|
22
|
+
|
23
|
+
def publish(data)
|
24
|
+
@subscriber.reject! {|sid,block|
|
25
|
+
begin
|
26
|
+
block.call(data)
|
27
|
+
false
|
28
|
+
rescue
|
29
|
+
true
|
30
|
+
end
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def size
|
35
|
+
@subscriber.size
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
$pubsub = PubSub.new
|
40
|
+
$record = []
|
41
|
+
|
42
|
+
class ShoutChatConnection < Rev::WebSocket
|
43
|
+
def on_open
|
44
|
+
@host = request['HTTP_HOST']
|
45
|
+
return unless @host
|
46
|
+
puts "connection opened: <#{@host}>"
|
47
|
+
|
48
|
+
@sid = $pubsub.subscribe {|data|
|
49
|
+
send_message data
|
50
|
+
}
|
51
|
+
$pubsub.publish(["count", $pubsub.size].to_json)
|
52
|
+
$record.each {|data| send_message data }
|
53
|
+
end
|
54
|
+
|
55
|
+
def on_message(data)
|
56
|
+
puts "broadcasting: <#{@host}> #{data}"
|
57
|
+
|
58
|
+
$pubsub.publish(data)
|
59
|
+
$record.push(data)
|
60
|
+
$record.shift while $record.size > 20
|
61
|
+
end
|
62
|
+
|
63
|
+
def on_close
|
64
|
+
return unless @host
|
65
|
+
puts "connection closed: <#{@host}>"
|
66
|
+
|
67
|
+
$pubsub.unsubscribe(@sid)
|
68
|
+
$pubsub.publish(["count", $pubsub.size].to_json)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
host = '0.0.0.0'
|
73
|
+
port = ARGV[0] || 8081
|
74
|
+
|
75
|
+
server = Rev::WebSocketServer.new(host, port, ShoutChatConnection)
|
76
|
+
server.attach(Rev::Loop.default)
|
77
|
+
|
78
|
+
puts "start on #{host}:#{port}"
|
79
|
+
|
80
|
+
Rev::Loop.default.run
|
81
|
+
|
data/lib/rev/websocket.rb
CHANGED
@@ -77,6 +77,13 @@ module Rev
|
|
77
77
|
return false if @data.empty?
|
78
78
|
|
79
79
|
data = @data.to_str
|
80
|
+
|
81
|
+
if data == "<policy-file-request/>\0"
|
82
|
+
write_policy_file
|
83
|
+
@state = :invalid_state
|
84
|
+
return false
|
85
|
+
end
|
86
|
+
|
80
87
|
begin
|
81
88
|
@http11_nbytes = @http11.execute(@request, data, @http11_nbytes)
|
82
89
|
rescue
|
@@ -201,6 +208,10 @@ module Rev
|
|
201
208
|
return true
|
202
209
|
end
|
203
210
|
|
211
|
+
def write_policy_file
|
212
|
+
write %[<cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>\0]
|
213
|
+
end
|
214
|
+
|
204
215
|
def ssl?
|
205
216
|
false
|
206
217
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rev-websocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- FURUHASHI Sadayuki
|
@@ -9,29 +15,41 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-17 00:00:00 +09:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: rev
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 3
|
33
|
+
- 2
|
23
34
|
version: 0.3.2
|
24
|
-
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
25
37
|
- !ruby/object:Gem::Dependency
|
26
38
|
name: thin
|
27
|
-
|
28
|
-
|
29
|
-
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
30
42
|
requirements:
|
31
43
|
- - ">="
|
32
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 17
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 2
|
49
|
+
- 7
|
33
50
|
version: 1.2.7
|
34
|
-
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
35
53
|
description: Rev-WebSocket is a WebSocket server implementation based on Rev, a high performance event-driven I/O library for Ruby. This library conforms to WebSocket draft-75 and draft-76.
|
36
54
|
email: frsyuki@users.sourceforge.jp
|
37
55
|
executables: []
|
@@ -42,11 +60,25 @@ extra_rdoc_files:
|
|
42
60
|
- ChangeLog
|
43
61
|
- README.rdoc
|
44
62
|
files:
|
45
|
-
- .gitignore
|
46
63
|
- ChangeLog
|
47
64
|
- README.rdoc
|
48
65
|
- Rakefile
|
49
66
|
- VERSION
|
67
|
+
- examples/README.md
|
68
|
+
- examples/echo
|
69
|
+
- examples/echo.rb
|
70
|
+
- examples/public/echo.css
|
71
|
+
- examples/public/echo.html
|
72
|
+
- examples/public/js/FABridge.js
|
73
|
+
- examples/public/js/WebSocketMain.swf
|
74
|
+
- examples/public/js/jquery.min.js
|
75
|
+
- examples/public/js/json2.js
|
76
|
+
- examples/public/js/swfobject.js
|
77
|
+
- examples/public/js/web_socket.js
|
78
|
+
- examples/public/shoutchat.css
|
79
|
+
- examples/public/shoutchat.html
|
80
|
+
- examples/shoutchat
|
81
|
+
- examples/shoutchat.rb
|
50
82
|
- lib/rev-websocket.rb
|
51
83
|
- lib/rev/websocket.rb
|
52
84
|
- lib/rev/websocket/spec.rb
|
@@ -60,23 +92,30 @@ rdoc_options:
|
|
60
92
|
require_paths:
|
61
93
|
- lib
|
62
94
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
63
96
|
requirements:
|
64
97
|
- - ">="
|
65
98
|
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
66
102
|
version: "0"
|
67
|
-
version:
|
68
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
69
105
|
requirements:
|
70
106
|
- - ">="
|
71
107
|
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
72
111
|
version: "0"
|
73
|
-
version:
|
74
112
|
requirements: []
|
75
113
|
|
76
114
|
rubyforge_project:
|
77
|
-
rubygems_version: 1.3.
|
115
|
+
rubygems_version: 1.3.7
|
78
116
|
signing_key:
|
79
117
|
specification_version: 3
|
80
118
|
summary: WebSocket server based on Rev
|
81
|
-
test_files:
|
82
|
-
|
119
|
+
test_files:
|
120
|
+
- examples/echo.rb
|
121
|
+
- examples/shoutchat.rb
|
data/.gitignore
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
*.gemspec
|