socket.io-client-simple 0.0.6 → 1.0.0

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: 53637062fd1f888bca99a916ad53579a82184658
4
- data.tar.gz: a1938eb0b5cc6c5df5ff6d1e5e73c3fa2b6b6fa7
3
+ metadata.gz: 50a078ab4570719f46273add06d388b0ce58db48
4
+ data.tar.gz: 332855f1fe15af38a2594b020897ea6880025633
5
5
  SHA512:
6
- metadata.gz: 3f96621384ba80c5ee9528474b594a93056d331071443df72df1b4e313236e407eb32ac1a6d3824d2c6bf148b43715fad6ad17bda3e883c5667a25a86aa4239b
7
- data.tar.gz: 8af155de0ee260969380caaf35adf9a6706df5adfb531f92bacbf7068582344cb8d9507c92032da5ff1bf41669a159cb9749ac17682e0441605092f889c83138
6
+ metadata.gz: bf78878c9ca070fe802161173f70ee694ac809474247536236eea26e22e42e8d58bafcc9e210d7edeb8bd4542c1e0cf6b1074b6bf042677aad32e791670ab499
7
+ data.tar.gz: 5f3889fbbd2c2f5f2a5f5f2289d4c20675779f1ff9214372f5b2b010f3f5630881a8f83afefd82aef11f7ba810f302c9410a1f20fa530910e9aa37972d65111e
data/.gitignore CHANGED
@@ -18,3 +18,4 @@ test/tmp
18
18
  test/version_tmp
19
19
  tmp
20
20
  node_modules
21
+ *.log
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- socket.io-client-simple (0.0.6)
4
+ socket.io-client-simple (1.0.0)
5
5
  event_emitter
6
6
  httparty
7
7
  json
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 1.0.0 2014-07-03
2
+
3
+ * support Socket.IO v1.0.x #4
4
+ * add parameter Client#auto_reconnection to disable auto-reconneciton
5
+
1
6
  === 0.0.6 2014-06-08
2
7
 
3
8
  * use 2nd argment of method "SocketIO::Client::Simple.connect" as handshake query parameter #5
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # SocketIO::Client::Simple
2
- A simple ruby client for node.js's socket.io. Supports only WebSocket.
2
+ A simple ruby client for Node.js's Socket.IO v1.0.x, Supports only WebSocket.
3
3
 
4
4
  - https://github.com/shokai/ruby-socket.io-client-simple
5
5
  - https://rubygems.org/gems/socket.io-client-simple
@@ -8,7 +8,15 @@ A simple ruby client for node.js's socket.io. Supports only WebSocket.
8
8
 
9
9
  ## Install
10
10
 
11
- % gem install socket.io-client-simple
11
+ gem intsall
12
+
13
+ % gem install socket.io-client-simple # latest version, supports Socket.IO v1.0.x
14
+
15
+
16
+ or use `Gemfile` with Bundler
17
+
18
+ gem 'socket.io-client-simple', '< 1.0' # for Socket.IO v0.9.x
19
+ gem 'socket.io-client-simple' # for Socket.IO v1.0.x
12
20
 
13
21
 
14
22
  ## Usage
@@ -47,12 +55,12 @@ loop do
47
55
  end
48
56
  ```
49
57
 
50
- sample.rb works with [samples/chat_server.js](https://github.com/shokai/ruby-socket.io-client-simple/blob/master/samples/chat_server.js).
58
+ sample.rb works with [samples/chat_server.coffee](https://github.com/shokai/ruby-socket.io-client-simple/blob/master/samples/chat_server.coffee).
51
59
 
52
60
  ### start chat server
53
61
 
54
62
  % npm install
55
- % node samples/chat_server.js
63
+ % coffee samples/chat_server.coffee
56
64
 
57
65
  => chat server start at localhost:3000
58
66
 
@@ -5,58 +5,45 @@ module SocketIO
5
5
  def self.connect(url, opts={})
6
6
  client = Client.new(url, opts)
7
7
  client.connect
8
- client
8
+ return client
9
9
  end
10
10
 
11
11
  class Client
12
12
  include EventEmitter
13
13
  alias_method :__emit, :emit
14
14
 
15
- attr_reader :websocket, :session_id, :heartbeat_timeout,
16
- :connection_timeout, :transports, :url
17
- attr_accessor :last_heartbeat_at, :reconnecting
15
+ attr_accessor :auto_reconnection, :websocket, :url, :reconnecting, :state,
16
+ :session_id, :ping_interval, :ping_timeout, :last_pong_at
18
17
 
19
18
  def initialize(url, opts={})
20
19
  @url = url
21
20
  @opts = opts
21
+ @opts[:transport] = :websocket
22
22
  @reconnecting = false
23
+ @state = :disconnect
24
+ @auto_reconnection = true
23
25
 
24
26
  Thread.new do
25
27
  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
28
+ if @state == :connect
29
+ @websocket.send "2" ## ping
30
+ @last_ping_at = Time.now
31
+ sleep @ping_interval/1000
32
+ else
33
+ sleep 1
32
34
  end
33
35
  end
34
36
  end
35
37
 
36
38
  end
37
39
 
40
+
38
41
  def connect
39
- res = nil
40
- begin
41
- res = HTTParty.get "#{@url}/socket.io/1", :query => @opts
42
- rescue Errno::ECONNREFUSED => 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
42
+ query = @opts.map{|k,v| "#{k}=#{v}" }.join '&'
57
43
  begin
58
- @websocket = WebSocket::Client::Simple.connect "#{@url}/socket.io/1/websocket/#{@session_id}"
44
+ @websocket = WebSocket::Client::Simple.connect "#{@url}/socket.io/?#{query}"
59
45
  rescue Errno::ECONNREFUSED => e
46
+ @state = :disconnect
60
47
  @reconnecting = false
61
48
  reconnect
62
49
  return
@@ -65,43 +52,50 @@ module SocketIO
65
52
  this = self
66
53
 
67
54
  @websocket.on :error do |err|
55
+ if err.kind_of? Errno::ECONNRESET and this.state == :connect
56
+ this.state = :disconnect
57
+ this.__emit :disconnect
58
+ this.reconnect
59
+ next
60
+ end
68
61
  this.__emit :error, err
69
62
  end
70
63
 
71
64
  @websocket.on :message do |msg|
72
- code, body = msg.data.scan(/^(\d+):{2,3}(.*)$/)[0]
65
+ next unless msg.data =~ /^\d+/
66
+ code, body = msg.data.unpack('U*').pack('C*').force_encoding('utf-8').scan(/^(\d+)(.*)$/)[0]
73
67
  code = code.to_i
74
68
  case code
75
- when 0
76
- this.websocket.close if this.websocket.open?
77
- this.__emit :disconnect
78
- this.reconnect
79
- when 1 ## socket.io connect
80
- this.last_heartbeat_at = Time.now
69
+ when 0 ## socket.io connect
81
70
  this.reconnecting = false
71
+ body = JSON.parse body rescue next
72
+ this.session_id = body["sid"]
73
+ this.ping_interval = body["pingInterval"]
74
+ this.ping_timeout = body["pingTimeout"]
75
+ this.state = :connect
82
76
  this.__emit :connect
83
- when 2
84
- this.last_heartbeat_at = Time.now
85
- send "2::" # socket.io heartbeat
86
- when 3
87
- when 4
88
- when 5
89
- data = JSON.parse body
90
- this.__emit data['name'], *data['args']
91
- when 6
92
- when 7
93
- this.__emit :error
77
+ when 3 ## pong
78
+ this.last_pong_at = Time.now
79
+ when 41 ## disconnect from server
80
+ this.websocket.close if this.websocket.open?
81
+ this.state = :disconnect
82
+ this.__emit :disconnect
83
+ reconnect
84
+ when 42 ## data
85
+ data = JSON.parse body rescue next
86
+ event_name = data.shift
87
+ this.__emit event_name, *data
94
88
  end
95
89
  end
96
90
 
97
- @websocket.send "1::#{@opts[:path]}"
98
- return
91
+ return self
99
92
  end
100
93
 
101
94
  def reconnect
95
+ return unless @auto_reconnection
102
96
  return if @reconnecting
103
97
  @reconnecting = true
104
- sleep rand(20)+20
98
+ sleep rand(10)+5
105
99
  connect
106
100
  end
107
101
 
@@ -111,8 +105,9 @@ module SocketIO
111
105
 
112
106
  def emit(event_name, *data)
113
107
  return unless open?
114
- emit_data = {:name => event_name, :args => data}.to_json
115
- @websocket.send "5:::#{emit_data}"
108
+ return unless @state == :connect
109
+ data.unshift event_name
110
+ @websocket.send "42#{data.to_json}".unpack('C*').pack('U*')
116
111
  end
117
112
 
118
113
  end
@@ -1,7 +1,7 @@
1
1
  module SocketIO
2
2
  module Client
3
3
  module Simple
4
- VERSION = "0.0.6"
4
+ VERSION = "1.0.0"
5
5
  end
6
6
  end
7
7
  end
data/package.json CHANGED
@@ -6,11 +6,13 @@
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/shokai/ruby-socket.io-client-simple",
8
8
  "dependencies": {
9
- "socket.io": "~0.9.16"
9
+ "socket.io": "~1.0",
10
+ "socket.io-client": "~1.0",
11
+ "coffee-script": "*"
10
12
  },
11
13
  "devDependencies": {},
12
14
  "scripts": {
13
- "start": "node samples/chat_server.js"
15
+ "start": "coffee samples/chat_server.coffee"
14
16
  },
15
17
  "repository": {
16
18
  "type": "git",
@@ -0,0 +1,19 @@
1
+ ## socket.io chat server
2
+
3
+ process.env.PORT ||= 3000;
4
+
5
+ io = require('socket.io').listen process.env.PORT
6
+ console.log "socket.io server start - port: #{process.env.PORT}"
7
+
8
+ io.sockets.on 'connection', (socket) ->
9
+ user = socket.handshake.query.user
10
+ socket.emit 'chat', {msg: 'hello world (from server)'}
11
+ socket.on 'chat', (data) ->
12
+ console.log data
13
+ data.user = user
14
+ io.sockets.emit 'chat', data; # broadcast echo
15
+
16
+ if process.env.EXIT_AT
17
+ setTimeout ->
18
+ process.exit();
19
+ , process.env.EXIT_AT-0
data/samples/sample.rb CHANGED
@@ -4,6 +4,8 @@ require 'socket.io-client-simple'
4
4
 
5
5
  socket = SocketIO::Client::Simple.connect 'http://localhost:3000'
6
6
 
7
+ # socket.auto_reconnection = false
8
+
7
9
  #socket.websocket.on :message do |msg| ## inspect websocket data
8
10
  # p msg.data
9
11
  #end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = SocketIO::Client::Simple::VERSION
9
9
  spec.authors = ["Sho Hashimoto"]
10
10
  spec.email = ["hashimoto@shokai.org"]
11
- spec.description = "A simple ruby client for node.js's socket.io. Supports only WebSocket."
11
+ spec.description = "A simple ruby client for Node.js's Socket.IO v1.0.x, Supports only WebSocket."
12
12
  spec.summary = spec.description
13
13
  spec.homepage = "https://github.com/shokai/ruby-socket.io-client-simple"
14
14
  spec.license = "MIT"
@@ -2,9 +2,7 @@ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
2
 
3
3
  class TestSocketIOClientSimple < MiniTest::Test
4
4
 
5
- def setup
6
- TestServer.start
7
- end
5
+ TestServer.start
8
6
 
9
7
  def test_connect
10
8
  socket = SocketIO::Client::Simple.connect TestServer.url
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.6
4
+ version: 1.0.0
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-06-07 00:00:00.000000000 Z
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,7 +108,7 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- description: A simple ruby client for node.js's socket.io. Supports only WebSocket.
111
+ description: A simple ruby client for Node.js's Socket.IO v1.0.x, Supports only WebSocket.
112
112
  email:
113
113
  - hashimoto@shokai.org
114
114
  executables: []
@@ -128,7 +128,7 @@ files:
128
128
  - lib/socket.io-client-simple/error.rb
129
129
  - lib/socket.io-client-simple/version.rb
130
130
  - package.json
131
- - samples/chat_server.js
131
+ - samples/chat_server.coffee
132
132
  - samples/sample.coffee
133
133
  - samples/sample.rb
134
134
  - socket.io-client-simple.gemspec
@@ -158,7 +158,7 @@ rubyforge_project:
158
158
  rubygems_version: 2.0.14
159
159
  signing_key:
160
160
  specification_version: 4
161
- summary: A simple ruby client for node.js's socket.io. Supports only WebSocket.
161
+ summary: A simple ruby client for Node.js's Socket.IO v1.0.x, Supports only WebSocket.
162
162
  test_files:
163
163
  - test/server.rb
164
164
  - test/test_helper.rb
@@ -1,22 +0,0 @@
1
- // socket.io chat server
2
- var port = (process.env.PORT || 3000) - 0;
3
-
4
- var io = require('socket.io').listen(port);
5
- console.log("socket.io server start - port:"+port);
6
-
7
- io.sockets.on('connection', function(socket){
8
- var user = socket.manager.handshaken[socket.id].query.user;
9
- socket.emit('chat', {msg: 'hello world (from server)'});
10
- socket.on('chat', function(data){
11
- console.log(data);
12
- data.user = user;
13
- socket.emit('chat', data); // echo
14
- });
15
- });
16
-
17
-
18
- if(!!process.env.EXIT_AT){
19
- setTimeout(function(){
20
- process.exit();
21
- }, process.env.EXIT_AT-0);
22
- }