sinatra-websocketio 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/lib/em-websocketio-client/client.rb +62 -0
- data/lib/em-websocketio-client.rb +6 -0
- data/lib/sinatra-websocketio/version.rb +1 -1
- data/lib/sinatra-websocketio/websocketio.rb +3 -1
- data/sample/Gemfile +1 -0
- data/sample/Gemfile.lock +9 -0
- data/sample/bin/cui_chat_client.rb +38 -0
- data/sinatra-websocketio.gemspec +1 -0
- metadata +21 -2
data/History.txt
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
module EventMachine
|
2
|
+
module WebSocketIO
|
3
|
+
class Client
|
4
|
+
include EventEmitter
|
5
|
+
attr_reader :url, :session, :running
|
6
|
+
attr_accessor :timeout
|
7
|
+
|
8
|
+
def initialize(url)
|
9
|
+
raise ArgumentError, "invalid websocket URL \"#{url}\"" unless url =~ /^ws:\/\/.+/
|
10
|
+
@url = url
|
11
|
+
@running = false
|
12
|
+
@__reconnect_timer_id = nil
|
13
|
+
@ws = nil
|
14
|
+
self.on :__session_id do |session_id|
|
15
|
+
@session = session_id
|
16
|
+
self.emit :connect, session_id
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def connect
|
21
|
+
url = @session ? "#{@url}/session=#{@session}" : @url
|
22
|
+
@ws = EventMachine::WebSocketClient.connect url
|
23
|
+
@running = true
|
24
|
+
|
25
|
+
@ws.stream do |msg|
|
26
|
+
data = JSON.parse msg
|
27
|
+
self.emit data['type'], data['data']
|
28
|
+
end
|
29
|
+
|
30
|
+
@ws.callback do
|
31
|
+
self.push :__session_id
|
32
|
+
end
|
33
|
+
|
34
|
+
@ws.errback do |err|
|
35
|
+
self.emit :error, err
|
36
|
+
end
|
37
|
+
|
38
|
+
@ws.disconnect do
|
39
|
+
self.emit :disconnect
|
40
|
+
if @running
|
41
|
+
@__reconnect_timer_id = EM::add_timer 10 do
|
42
|
+
connect
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
return self
|
48
|
+
end
|
49
|
+
|
50
|
+
def close
|
51
|
+
@running = false
|
52
|
+
@ws.close_connection if @ws
|
53
|
+
EM::cancel_timer @__reconnect_timer_id
|
54
|
+
end
|
55
|
+
|
56
|
+
def push(type, data={})
|
57
|
+
@ws.send_msg({:type => type, :data => data, :session => self.session}.to_json)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/sample/Gemfile
CHANGED
data/sample/Gemfile.lock
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
+
addressable (2.3.3)
|
4
5
|
backports (3.0.3)
|
5
6
|
daemons (1.1.9)
|
6
7
|
em-websocket (0.5.0)
|
7
8
|
eventmachine (>= 0.12.9)
|
8
9
|
http_parser.rb (~> 0.5.3)
|
10
|
+
em-websocket-client (0.1.1)
|
11
|
+
eventmachine
|
12
|
+
libwebsocket
|
9
13
|
event_emitter (0.2.3)
|
10
14
|
eventmachine (1.0.1)
|
11
15
|
foreman (0.61.0)
|
@@ -14,6 +18,9 @@ GEM
|
|
14
18
|
tilt
|
15
19
|
http_parser.rb (0.5.3)
|
16
20
|
json (1.7.7)
|
21
|
+
libwebsocket (0.1.7.1)
|
22
|
+
addressable
|
23
|
+
websocket
|
17
24
|
rack (1.5.2)
|
18
25
|
rack-protection (1.4.0)
|
19
26
|
rack
|
@@ -37,6 +44,7 @@ GEM
|
|
37
44
|
rack (>= 1.0.0)
|
38
45
|
thor (0.17.0)
|
39
46
|
tilt (1.3.4)
|
47
|
+
websocket (1.0.7)
|
40
48
|
|
41
49
|
PLATFORMS
|
42
50
|
ruby
|
@@ -44,6 +52,7 @@ PLATFORMS
|
|
44
52
|
DEPENDENCIES
|
45
53
|
backports (< 3.1.0)
|
46
54
|
em-websocket
|
55
|
+
em-websocket-client
|
47
56
|
event_emitter
|
48
57
|
foreman
|
49
58
|
haml
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
$:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
|
5
|
+
require 'eventmachine'
|
6
|
+
require 'em-websocketio-client'
|
7
|
+
|
8
|
+
name = `whoami`.strip || 'shokai'
|
9
|
+
|
10
|
+
EM::run do
|
11
|
+
client = EM::WebSocketIO::Client.new('ws://localhost:8080').connect
|
12
|
+
|
13
|
+
client.on :connect do |session|
|
14
|
+
puts "connect!! (session_id:#{session})"
|
15
|
+
end
|
16
|
+
|
17
|
+
client.on :chat do |data|
|
18
|
+
puts "<#{data['name']}> #{data['message']}"
|
19
|
+
end
|
20
|
+
|
21
|
+
client.on :error do |err|
|
22
|
+
STDERR.puts err
|
23
|
+
end
|
24
|
+
|
25
|
+
client.on :disconnect do
|
26
|
+
puts "disconnected!!"
|
27
|
+
end
|
28
|
+
|
29
|
+
EM::defer do
|
30
|
+
loop do
|
31
|
+
line = STDIN.gets.strip
|
32
|
+
next if line.empty?
|
33
|
+
client.push :chat, {:message => line, :name => name}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
data/sinatra-websocketio.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-websocketio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -123,6 +123,22 @@ dependencies:
|
|
123
123
|
- - ! '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: em-websocket-client
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :runtime
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
126
142
|
description: Node.js like WebSocket I/O plugin for Sinatra.
|
127
143
|
email:
|
128
144
|
- hashimoto@shokai.org
|
@@ -136,6 +152,8 @@ files:
|
|
136
152
|
- LICENSE.txt
|
137
153
|
- README.md
|
138
154
|
- Rakefile
|
155
|
+
- lib/em-websocketio-client.rb
|
156
|
+
- lib/em-websocketio-client/client.rb
|
139
157
|
- lib/js/event_emitter.js
|
140
158
|
- lib/js/websocketio.js
|
141
159
|
- lib/sinatra-websocketio.rb
|
@@ -148,6 +166,7 @@ files:
|
|
148
166
|
- sample/Gemfile.lock
|
149
167
|
- sample/Procfile
|
150
168
|
- sample/README.md
|
169
|
+
- sample/bin/cui_chat_client.rb
|
151
170
|
- sample/config.ru
|
152
171
|
- sample/main.rb
|
153
172
|
- sample/public/js/index.js
|