socket.io-client-simple 0.0.2 → 0.0.3
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 +4 -4
- data/Gemfile.lock +3 -3
- data/History.txt +5 -0
- data/README.md +8 -0
- data/lib/socket.io-client-simple.rb +0 -5
- data/lib/socket.io-client-simple/client.rb +66 -20
- data/lib/socket.io-client-simple/version.rb +1 -1
- data/samples/sample.coffee +19 -0
- data/socket.io-client-simple.gemspec +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e667cd97164a9d1c7086a9e6809a610b3f9be00
|
4
|
+
data.tar.gz: a4199df86ebc2d43a2f745b0ad2f648ba040ee1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d998aa592b286f6d627e13aea6ab35937f4041adff9c13ac6e3776a774301c425e9f8e1b492e989d5e2de8ea4ec795688b74606bb9400d0f539f46015ccfbde
|
7
|
+
data.tar.gz: 9085050e33a16bdc965ecbc06f608763ae99a0d57b8bacaf3232e239ae0c8dec5aaddee640687cb8a5a265ab761e01d5e0daabc3efe2a31f85a32e808325ad93
|
data/Gemfile.lock
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
socket.io-client-simple (0.0.
|
4
|
+
socket.io-client-simple (0.0.3)
|
5
5
|
event_emitter
|
6
6
|
httparty
|
7
7
|
json
|
8
|
-
websocket-client-simple
|
8
|
+
websocket-client-simple (>= 0.0.6)
|
9
9
|
|
10
10
|
GEM
|
11
11
|
remote: https://rubygems.org/
|
@@ -19,7 +19,7 @@ GEM
|
|
19
19
|
multi_xml (0.5.5)
|
20
20
|
rake (10.1.1)
|
21
21
|
websocket (1.1.2)
|
22
|
-
websocket-client-simple (0.0.
|
22
|
+
websocket-client-simple (0.0.6)
|
23
23
|
event_emitter
|
24
24
|
websocket
|
25
25
|
|
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,7 @@ A simple ruby client for node.js's socket.io. Supports only WebSocket.
|
|
4
4
|
- https://github.com/shokai/ruby-socket.io-client-simple
|
5
5
|
- https://rubygems.org/gems/socket.io-client-simple
|
6
6
|
|
7
|
+
[](https://travis-ci.org/shokai/ruby-socket.io-client-simple)
|
7
8
|
|
8
9
|
## Install
|
9
10
|
|
@@ -53,6 +54,13 @@ sample.rb works with [samples/chat_server.js](https://github.com/shokai/ruby-soc
|
|
53
54
|
|
54
55
|
% ruby samples/sample.rb
|
55
56
|
|
57
|
+
## Test
|
58
|
+
|
59
|
+
% gem install bundler
|
60
|
+
% bundle install
|
61
|
+
% npm install
|
62
|
+
% bundle exec rake test
|
63
|
+
|
56
64
|
|
57
65
|
## Contributing
|
58
66
|
|
@@ -2,19 +2,65 @@ module SocketIO
|
|
2
2
|
module Client
|
3
3
|
module Simple
|
4
4
|
|
5
|
+
def self.connect(url, opts={})
|
6
|
+
client = Client.new(url, opts)
|
7
|
+
client.connect
|
8
|
+
client
|
9
|
+
end
|
10
|
+
|
5
11
|
class Client
|
6
12
|
include EventEmitter
|
7
13
|
alias_method :__emit, :emit
|
8
14
|
|
9
15
|
attr_reader :websocket, :session_id, :heartbeat_timeout,
|
10
16
|
:connection_timeout, :transports
|
17
|
+
attr_accessor :last_heartbeat_at, :reconnecting
|
11
18
|
|
12
19
|
def initialize(url, opts={})
|
13
20
|
@url = url
|
14
21
|
@opts = opts
|
15
|
-
|
22
|
+
@reconnecting = false
|
23
|
+
|
24
|
+
Thread.new do
|
25
|
+
loop do
|
26
|
+
sleep 5
|
27
|
+
next if !@last_heartbeat_at or !@heartbeat_timeout
|
28
|
+
if Time.now - @last_heartbeat_at > @heartbeat_timeout
|
29
|
+
@websocket.close
|
30
|
+
__emit :disconnect
|
31
|
+
reconnect
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
16
37
|
|
17
|
-
|
38
|
+
def connect
|
39
|
+
res = nil
|
40
|
+
begin
|
41
|
+
res = HTTParty.get "#{@url}/socket.io/1/"
|
42
|
+
rescue => e
|
43
|
+
@reconnecting = false
|
44
|
+
reconnect
|
45
|
+
return
|
46
|
+
end
|
47
|
+
raise res.body unless res.code == 200
|
48
|
+
|
49
|
+
arr = res.body.split(':')
|
50
|
+
@session_id = arr.shift
|
51
|
+
@heartbeat_timeout = arr.shift.to_i
|
52
|
+
@connection_timeout = arr.shift.to_i
|
53
|
+
@transports = arr.shift.split(',')
|
54
|
+
unless @transports.include? 'websocket'
|
55
|
+
raise Error, "server #{@url} does not supports websocket!!"
|
56
|
+
end
|
57
|
+
begin
|
58
|
+
@websocket = WebSocket::Client::Simple.connect "#{@url}/socket.io/1/websocket/#{@session_id}"
|
59
|
+
rescue => e
|
60
|
+
@reconnecting = false
|
61
|
+
reconnect
|
62
|
+
return
|
63
|
+
end
|
18
64
|
|
19
65
|
this = self
|
20
66
|
@websocket.on :message do |msg|
|
@@ -22,11 +68,16 @@ module SocketIO
|
|
22
68
|
code = code.to_i
|
23
69
|
case code
|
24
70
|
when 0
|
71
|
+
this.websocket.close if this.websocket.open?
|
25
72
|
this.__emit :disconnect
|
26
|
-
|
73
|
+
this.reconnect
|
74
|
+
when 1 ## socket.io connect
|
75
|
+
this.last_heartbeat_at = Time.now
|
76
|
+
this.reconnecting = false
|
27
77
|
this.__emit :connect
|
28
78
|
when 2
|
29
|
-
|
79
|
+
this.last_heartbeat_at = Time.now
|
80
|
+
send "2::" # socket.io heartbeat
|
30
81
|
when 3
|
31
82
|
when 4
|
32
83
|
when 5
|
@@ -38,28 +89,23 @@ module SocketIO
|
|
38
89
|
end
|
39
90
|
end
|
40
91
|
|
41
|
-
@websocket.
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
@websocket.send "1::#{opts[:path]}"
|
92
|
+
@websocket.send "1::#{@opts[:path]}"
|
93
|
+
return
|
46
94
|
end
|
47
95
|
|
48
|
-
def
|
49
|
-
|
50
|
-
|
96
|
+
def reconnect
|
97
|
+
return if @reconnecting
|
98
|
+
@reconnecting = true
|
99
|
+
sleep rand(20)+20
|
100
|
+
connect
|
101
|
+
end
|
51
102
|
|
52
|
-
|
53
|
-
@
|
54
|
-
@heartbeat_timeout = arr.shift.to_i
|
55
|
-
@connection_timeout = arr.shift.to_i
|
56
|
-
@transports = arr.shift.split(',')
|
57
|
-
unless @transports.include? 'websocket'
|
58
|
-
raise Error, "server #{@url} does not supports websocket!!"
|
59
|
-
end
|
103
|
+
def open?
|
104
|
+
@websocket and @websocket.open?
|
60
105
|
end
|
61
106
|
|
62
107
|
def emit(event_name, *data)
|
108
|
+
return unless open?
|
63
109
|
emit_data = {:name => event_name, :args => data}.to_json
|
64
110
|
@websocket.send "5:::#{emit_data}"
|
65
111
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env coffee
|
2
|
+
## npm insatll socket.io-client coffee-script
|
3
|
+
|
4
|
+
socket = require('socket.io-client').connect('http://localhost:3000')
|
5
|
+
|
6
|
+
socket.on 'connect', ->
|
7
|
+
console.log 'connect!!'
|
8
|
+
|
9
|
+
socket.on 'disconnect', ->
|
10
|
+
console.log 'disconnected'
|
11
|
+
|
12
|
+
socket.on 'chat', (data) ->
|
13
|
+
console.log "> " + data.msg
|
14
|
+
|
15
|
+
process.stdin.setEncoding 'utf8'
|
16
|
+
process.stdin.on 'data', (data) ->
|
17
|
+
msg = data.replace(/[\r\n]/g, '')
|
18
|
+
return if msg.length < 1
|
19
|
+
socket.emit 'chat', {msg: msg, at: Date.now()}
|
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency "minitest"
|
24
24
|
|
25
25
|
spec.add_dependency "json"
|
26
|
-
spec.add_dependency "websocket-client-simple"
|
26
|
+
spec.add_dependency "websocket-client-simple", '>= 0.0.6'
|
27
27
|
spec.add_dependency "httparty"
|
28
28
|
spec.add_dependency "event_emitter"
|
29
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socket.io-client-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sho Hashimoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 0.0.6
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 0.0.6
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: httparty
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/socket.io-client-simple/version.rb
|
130
130
|
- package.json
|
131
131
|
- samples/chat_server.js
|
132
|
+
- samples/sample.coffee
|
132
133
|
- samples/sample.rb
|
133
134
|
- socket.io-client-simple.gemspec
|
134
135
|
- test/server.rb
|