socket.io-client-simple 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2afed194d20af9d81e21b0048f1c056f12736ea1
4
- data.tar.gz: 6113d9b1854c2a2a9e97fe37f682be60fa634e52
3
+ metadata.gz: 3e667cd97164a9d1c7086a9e6809a610b3f9be00
4
+ data.tar.gz: a4199df86ebc2d43a2f745b0ad2f648ba040ee1e
5
5
  SHA512:
6
- metadata.gz: 0a7d3e056498915bb1ab326dc070962cc2d84127799af6f65670e3c7fa35b8684e574538d2aa420da0d4449493c6b1559e34af69df7fbdb74e34927710021551
7
- data.tar.gz: b3f08e4f17e8cb4075b2e16d29a77a1c486e8508383c315a20ede5cd8caeeb616922c955e8e4370e1ebed34ed99b92dfb9851762c16a7cbfa94434116f3430e4
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.2)
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.5)
22
+ websocket-client-simple (0.0.6)
23
23
  event_emitter
24
24
  websocket
25
25
 
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.0.3 2014-1-20
2
+
3
+ * reconnect on socket.io disconnect
4
+ * add Tests, use Travis-CI
5
+
1
6
  === 0.0.2 2014-1-18
2
7
 
3
8
  * bugfix "disconnect" event
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
+ [![Travis-CI Build Status](https://travis-ci.org/shokai/ruby-socket.io-client-simple.png)](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
 
@@ -10,11 +10,6 @@ require 'socket.io-client-simple/client'
10
10
  module SocketIO
11
11
  module Client
12
12
  module Simple
13
-
14
- def self.connect(url, opts={})
15
- Client.new(url, opts)
16
- end
17
-
18
13
  end
19
14
  end
20
15
  end
@@ -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
- configure
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
- @websocket = WebSocket::Client::Simple.connect "#{url}/socket.io/1/websocket/#{@session_id}"
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
- when 1
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
- send "2::" # heartbeat
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.on :close do
42
- this.__emit :disconnect
43
- end
44
-
45
- @websocket.send "1::#{opts[:path]}"
92
+ @websocket.send "1::#{@opts[:path]}"
93
+ return
46
94
  end
47
95
 
48
- def configure
49
- res = HTTParty.get "#{@url}/socket.io/1/"
50
- raise res.body unless res.code == 200
96
+ def reconnect
97
+ return if @reconnecting
98
+ @reconnecting = true
99
+ sleep rand(20)+20
100
+ connect
101
+ end
51
102
 
52
- arr = res.body.split(':')
53
- @session_id = arr.shift
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
@@ -1,7 +1,7 @@
1
1
  module SocketIO
2
2
  module Client
3
3
  module Simple
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
6
6
  end
7
7
  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.2
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-18 00:00:00.000000000 Z
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: '0'
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: '0'
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