socket.io-client-simple 0.0.6 → 1.0.0
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/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/History.txt +5 -0
- data/README.md +12 -4
- data/lib/socket.io-client-simple/client.rb +47 -52
- data/lib/socket.io-client-simple/version.rb +1 -1
- data/package.json +4 -2
- data/samples/chat_server.coffee +19 -0
- data/samples/sample.rb +2 -0
- data/socket.io-client-simple.gemspec +1 -1
- data/test/test_socket.io.rb +1 -3
- metadata +5 -5
- data/samples/chat_server.js +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50a078ab4570719f46273add06d388b0ce58db48
|
4
|
+
data.tar.gz: 332855f1fe15af38a2594b020897ea6880025633
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf78878c9ca070fe802161173f70ee694ac809474247536236eea26e22e42e8d58bafcc9e210d7edeb8bd4542c1e0cf6b1074b6bf042677aad32e791670ab499
|
7
|
+
data.tar.gz: 5f3889fbbd2c2f5f2a5f5f2289d4c20675779f1ff9214372f5b2b010f3f5630881a8f83afefd82aef11f7ba810f302c9410a1f20fa530910e9aa37972d65111e
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# SocketIO::Client::Simple
|
2
|
-
A simple ruby client for
|
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
|
-
|
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.
|
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
|
-
%
|
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
|
-
|
16
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
@
|
30
|
-
|
31
|
-
|
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
|
-
|
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
|
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
|
-
|
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
|
84
|
-
this.
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
this.__emit
|
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
|
-
|
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(
|
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
|
-
|
115
|
-
|
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
|
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
|
+
"socket.io": "~1.0",
|
10
|
+
"socket.io-client": "~1.0",
|
11
|
+
"coffee-script": "*"
|
10
12
|
},
|
11
13
|
"devDependencies": {},
|
12
14
|
"scripts": {
|
13
|
-
"start": "
|
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
@@ -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
|
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"
|
data/test/test_socket.io.rb
CHANGED
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: 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-
|
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
|
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.
|
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
|
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
|
data/samples/chat_server.js
DELETED
@@ -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
|
-
}
|