statscloud.io-ruby-socket.io-client-simple 1.2.1.pre.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/History.txt +62 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +8 -0
- data/lib/statscloud.io-ruby-socket.io-client-simple/byte_buffer.rb +21 -0
- data/lib/statscloud.io-ruby-socket.io-client-simple/client.rb +159 -0
- data/lib/statscloud.io-ruby-socket.io-client-simple/error.rb +12 -0
- data/lib/statscloud.io-ruby-socket.io-client-simple/version.rb +9 -0
- data/lib/statscloud.io-ruby-socket.io-client-simple.rb +19 -0
- data/package.json +24 -0
- data/samples/chat_server.coffee +19 -0
- data/samples/sample.coffee +20 -0
- data/samples/sample.rb +35 -0
- data/statscloud.io-ruby-socket.io-client-simple.gemspec +29 -0
- data/test/server.rb +16 -0
- data/test/test_helper.rb +8 -0
- data/test/test_socket.io.rb +62 -0
- metadata +165 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4f454e59ca54831fe5027cfab76698df7093e04dc30209c0ec995ce14054e995
|
4
|
+
data.tar.gz: 727ae5656df991443b67d1004e5a73296b1dc4e4bad74bd526116b99ab6310c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 096b1ef313266d8c01504e5cf8a23e2c16ee6a28bc1f4661b6d6f44a5cccf74aa3b027d74ed355c6ddcc9212354ccf42c9944ca48b29ee33d214a42df212b4a6
|
7
|
+
data.tar.gz: 6e882a7c8a8ad18b7f47e8a29ebb033d4bf8f878dc441035d1d966eff90d475b02966e9ed943c5caf1b7cfafe99fcfa90792d3c726547877e4bd34ca70fbc914
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*~
|
2
|
+
*#*
|
3
|
+
.DS_Store
|
4
|
+
*.gem
|
5
|
+
*.rbc
|
6
|
+
.bundle
|
7
|
+
.config
|
8
|
+
.yardoc
|
9
|
+
InstalledFiles
|
10
|
+
_yardoc
|
11
|
+
coverage
|
12
|
+
doc/
|
13
|
+
lib/bundler/man
|
14
|
+
pkg
|
15
|
+
rdoc
|
16
|
+
spec/reports
|
17
|
+
test/tmp
|
18
|
+
test/version_tmp
|
19
|
+
tmp
|
20
|
+
node_modules
|
21
|
+
*.log
|
22
|
+
vendor
|
23
|
+
.idea/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
statscloud.io-ruby-socket.io-client-simple (1.2.1.pre.1)
|
5
|
+
event_emitter
|
6
|
+
httparty
|
7
|
+
json
|
8
|
+
websocket-client-simple (~> 0.3.0)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
event_emitter (0.2.6)
|
14
|
+
httparty (0.16.2)
|
15
|
+
multi_xml (>= 0.5.2)
|
16
|
+
json (2.1.0)
|
17
|
+
minitest (5.8.4)
|
18
|
+
multi_xml (0.6.0)
|
19
|
+
rake (10.5.0)
|
20
|
+
websocket (1.2.8)
|
21
|
+
websocket-client-simple (0.3.0)
|
22
|
+
event_emitter
|
23
|
+
websocket
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.3)
|
30
|
+
minitest
|
31
|
+
rake
|
32
|
+
statscloud.io-ruby-socket.io-client-simple!
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.3
|
data/History.txt
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
=== 1.2.1-1 2018-10-27
|
2
|
+
|
3
|
+
* implement namespaces (statscloud) v1.2.1-1
|
4
|
+
* implement simple binary support (statscloud) v1.2.1-1
|
5
|
+
|
6
|
+
=== 1.2.1 2016-02-21
|
7
|
+
|
8
|
+
* update websocket-client-simple v0.3.x
|
9
|
+
* update socket.io 1.4.x
|
10
|
+
|
11
|
+
=== 1.2.0 2016-02-19
|
12
|
+
|
13
|
+
* add "disconnect" method #16
|
14
|
+
* thank you for contributing @julianbei
|
15
|
+
|
16
|
+
=== 1.1.3 2015-02-03
|
17
|
+
|
18
|
+
* update for socket.io 1.3.x
|
19
|
+
|
20
|
+
=== 1.1.2 2014-10-20
|
21
|
+
|
22
|
+
* bugfix reconnection on websocket proxy #9
|
23
|
+
* fix samples
|
24
|
+
* bundle update, use websocket-client-simple v0.2.1 #9
|
25
|
+
* bugfix encoding handshake query parameter #5
|
26
|
+
|
27
|
+
=== 1.1.0 2014-10-04
|
28
|
+
|
29
|
+
* support Socket.IO v1.1x
|
30
|
+
|
31
|
+
=== 1.0.0 2014-07-03
|
32
|
+
|
33
|
+
* support Socket.IO v1.0.x #4
|
34
|
+
* add parameter Client#auto_reconnection to disable auto-reconneciton
|
35
|
+
|
36
|
+
=== 0.0.6 2014-06-08
|
37
|
+
|
38
|
+
* use 2nd argment of method "SocketIO::Client::Simple.connect" as handshake query parameter #5
|
39
|
+
* thank you for contributing @mallowlabs
|
40
|
+
|
41
|
+
=== 0.0.5 2014-04-03
|
42
|
+
|
43
|
+
* emit "error" event
|
44
|
+
* rescue only Errno::ECONNREFUSED
|
45
|
+
|
46
|
+
=== 0.0.4 2014-01-20
|
47
|
+
|
48
|
+
* add accessor Client#url
|
49
|
+
|
50
|
+
=== 0.0.3 2014-01-20
|
51
|
+
|
52
|
+
* reconnect on socket.io disconnect
|
53
|
+
* add Tests, use Travis-CI
|
54
|
+
|
55
|
+
=== 0.0.2 2014-01-18
|
56
|
+
|
57
|
+
* bugfix "disconnect" event
|
58
|
+
|
59
|
+
=== 0.0.1 2014-01-15
|
60
|
+
|
61
|
+
* first release
|
62
|
+
* implemented "emit" and "on"
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Sho Hashimoto
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# statscloud.io-ruby-socket.io-client-simple
|
2
|
+
|
3
|
+
A simple ruby client for Node.js's Socket.IO v1.4.x, Supports only WebSocket.
|
4
|
+
|
5
|
+
v 1.2.1-1 Implement namespaces and simple binary support.
|
6
|
+
|
7
|
+
- https://gitlab.com/agiliumlabs/statscloud.io-ruby-socket.io-client-simple
|
8
|
+
|
9
|
+
|
10
|
+
## Install
|
11
|
+
|
12
|
+
gem install
|
13
|
+
|
14
|
+
% gem install statscloud.io-ruby-socket.io-client-simple
|
15
|
+
|
16
|
+
|
17
|
+
or use `Gemfile` with Bundler
|
18
|
+
|
19
|
+
gem 'statscloud.io-ruby-socket.io-client-simple'
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'rubygems'
|
26
|
+
require 'statscloud.io-ruby-socket.io-client-simple'
|
27
|
+
|
28
|
+
def client
|
29
|
+
StatsCloudIO::SocketIO::Client::Simple
|
30
|
+
end
|
31
|
+
|
32
|
+
def configure
|
33
|
+
url = ARGV.shift || 'http://localhost:3000'
|
34
|
+
socket = client.connect url
|
35
|
+
|
36
|
+
socket.on :connect do
|
37
|
+
puts "connect!!!"
|
38
|
+
end
|
39
|
+
|
40
|
+
socket.on :disconnect do
|
41
|
+
puts "disconnected!!"
|
42
|
+
end
|
43
|
+
|
44
|
+
socket.on :error do |err|
|
45
|
+
p err
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def send_data(chat, data)
|
50
|
+
socket.emit chat, data
|
51
|
+
end
|
52
|
+
|
53
|
+
def send_binary_data(chat, data)
|
54
|
+
socket.emit chat, client.as_byte_buffer(data)
|
55
|
+
end
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
## Test
|
60
|
+
|
61
|
+
% gem install bundler
|
62
|
+
% bundle install
|
63
|
+
% npm install
|
64
|
+
% bundle exec rake test
|
65
|
+
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module StatsCloudIO
|
2
|
+
module SocketIO
|
3
|
+
module Client
|
4
|
+
module Simple
|
5
|
+
|
6
|
+
def self.as_byte_buffer(data)
|
7
|
+
ByteBuffer.new(data)
|
8
|
+
end
|
9
|
+
|
10
|
+
class ByteBuffer
|
11
|
+
attr_accessor :buffer
|
12
|
+
|
13
|
+
def initialize(byte_array)
|
14
|
+
return unless byte_array.is_a?(Array)
|
15
|
+
@buffer = byte_array.unshift(4).pack('C*')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
module StatsCloudIO
|
2
|
+
module SocketIO
|
3
|
+
module Client
|
4
|
+
module Simple
|
5
|
+
|
6
|
+
def self.connect(url, opts={})
|
7
|
+
client = Client.new(url, opts)
|
8
|
+
client.connect
|
9
|
+
return client
|
10
|
+
end
|
11
|
+
|
12
|
+
class Client
|
13
|
+
include EventEmitter
|
14
|
+
alias_method :__emit, :emit
|
15
|
+
|
16
|
+
attr_accessor :auto_reconnection, :websocket, :url, :reconnecting, :state,
|
17
|
+
:session_id, :ping_interval, :ping_timeout, :last_pong_at, :last_ping_at
|
18
|
+
|
19
|
+
def initialize(url, opts={})
|
20
|
+
@url = url
|
21
|
+
@opts = opts
|
22
|
+
@opts[:transport] = :websocket
|
23
|
+
@path = @opts.delete(:path) || "/socket.io"
|
24
|
+
@reconnecting = false
|
25
|
+
@state = :disconnect
|
26
|
+
@auto_reconnection = true
|
27
|
+
|
28
|
+
Thread.new do
|
29
|
+
loop do
|
30
|
+
if @websocket
|
31
|
+
if @state == :connect
|
32
|
+
if Time.now.to_i - @last_ping_at > @ping_interval/1000
|
33
|
+
@websocket.send "2" ## ping
|
34
|
+
@last_ping_at = Time.now.to_i
|
35
|
+
end
|
36
|
+
end
|
37
|
+
if @websocket.open? && Time.now.to_i - @last_pong_at > @ping_timeout/100
|
38
|
+
@websocket.close
|
39
|
+
@state = :disconnect
|
40
|
+
__emit :disconnect
|
41
|
+
reconnect
|
42
|
+
end
|
43
|
+
end
|
44
|
+
sleep 1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def connect
|
52
|
+
query = @opts.map{|k,v| URI.encode "#{k}=#{v}" }.join '&'
|
53
|
+
begin
|
54
|
+
@websocket = WebSocket::Client::Simple.connect "#{@url}#{@path}/?#{query}"
|
55
|
+
rescue Errno::ECONNREFUSED => e
|
56
|
+
@state = :disconnect
|
57
|
+
@reconnecting = false
|
58
|
+
reconnect
|
59
|
+
return
|
60
|
+
end
|
61
|
+
@reconnecting = false
|
62
|
+
|
63
|
+
this = self
|
64
|
+
|
65
|
+
@websocket.on :error do |err|
|
66
|
+
if err.kind_of? Errno::ECONNRESET and this.state == :connect
|
67
|
+
this.state = :disconnect
|
68
|
+
this.__emit :disconnect
|
69
|
+
this.reconnect
|
70
|
+
next
|
71
|
+
end
|
72
|
+
this.__emit :error, err
|
73
|
+
end
|
74
|
+
|
75
|
+
@websocket.on :message do |msg|
|
76
|
+
next unless msg.data =~ /^\d+/
|
77
|
+
code, body = msg.data.scan(/^(\d+)(.*)$/)[0]
|
78
|
+
code = code.to_i
|
79
|
+
case code
|
80
|
+
when 0 ## socket.io connect
|
81
|
+
body = JSON.parse body rescue next
|
82
|
+
this.session_id = body["sid"] || "no_sid"
|
83
|
+
this.ping_interval = body["pingInterval"] || 25000
|
84
|
+
this.ping_timeout = body["pingTimeout"] || 60000
|
85
|
+
this.last_ping_at = Time.now.to_i
|
86
|
+
this.last_pong_at = Time.now.to_i
|
87
|
+
this.state = :connect
|
88
|
+
this.__emit :connect
|
89
|
+
when 3 ## pong
|
90
|
+
this.last_pong_at = Time.now.to_i
|
91
|
+
when 41 ## disconnect from server
|
92
|
+
this.websocket.close if this.websocket.open?
|
93
|
+
this.state = :disconnect
|
94
|
+
this.__emit :disconnect
|
95
|
+
reconnect
|
96
|
+
when 42 ## data
|
97
|
+
data = JSON.parse body rescue next
|
98
|
+
event_name = data.shift
|
99
|
+
this.__emit event_name, *data
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
return self
|
104
|
+
end
|
105
|
+
|
106
|
+
def reconnect
|
107
|
+
return unless @auto_reconnection
|
108
|
+
return if @reconnecting
|
109
|
+
@reconnecting = true
|
110
|
+
sleep rand(5) + 5
|
111
|
+
connect
|
112
|
+
end
|
113
|
+
|
114
|
+
def open?
|
115
|
+
@websocket and @websocket.open?
|
116
|
+
end
|
117
|
+
|
118
|
+
def emit(event_name, *data)
|
119
|
+
return unless open?
|
120
|
+
return unless @state == :connect
|
121
|
+
data.each do |packet|
|
122
|
+
is_binary?(packet) ? send_binary(event_name, packet) : send_data(event_name, packet)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def disconnect
|
127
|
+
@auto_reconnection = false
|
128
|
+
@websocket.close
|
129
|
+
@state = :disconnect
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def is_binary?(packet)
|
135
|
+
packet.is_a?(SocketIO::Client::Simple::ByteBuffer)
|
136
|
+
end
|
137
|
+
|
138
|
+
def send_data(event_name, *data)
|
139
|
+
data.unshift event_name
|
140
|
+
@websocket.send "42#{data.to_json}"
|
141
|
+
end
|
142
|
+
|
143
|
+
def send_binary(event_name, packet)
|
144
|
+
header = binary_header.unshift event_name
|
145
|
+
@websocket.send "451-#{header.to_json}"
|
146
|
+
@websocket.send packet.buffer, type: :binary
|
147
|
+
end
|
148
|
+
|
149
|
+
def binary_header
|
150
|
+
[{
|
151
|
+
_placeholder: true,
|
152
|
+
num: 0
|
153
|
+
}]
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'json'
|
3
|
+
require 'httparty'
|
4
|
+
require 'websocket-client-simple'
|
5
|
+
require 'event_emitter'
|
6
|
+
|
7
|
+
require 'statscloud.io-ruby-socket.io-client-simple/version'
|
8
|
+
require 'statscloud.io-ruby-socket.io-client-simple/error'
|
9
|
+
require 'statscloud.io-ruby-socket.io-client-simple/client'
|
10
|
+
require 'statscloud.io-ruby-socket.io-client-simple/byte_buffer'
|
11
|
+
|
12
|
+
module StatsCloudIO
|
13
|
+
module SocketIO
|
14
|
+
module Client
|
15
|
+
module Simple
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/package.json
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
{
|
2
|
+
"name": "statscloud.io-ruby-socket.io-client-simple/version",
|
3
|
+
"description": "A simple ruby client for node.js's socket.io. Supports only WebSocket.",
|
4
|
+
"private": true,
|
5
|
+
"author": "Sho Hashimoto <hashimoto@shokai.org>",
|
6
|
+
"license": "MIT",
|
7
|
+
"homepage": "https://gitlab.com/agiliumlabs/statscloud.io-ruby-socket.io-client-simple",
|
8
|
+
"dependencies": {
|
9
|
+
"socket.io": "^1.4",
|
10
|
+
"socket.io-client": "^1.4",
|
11
|
+
"coffee-script": "^1.10"
|
12
|
+
},
|
13
|
+
"devDependencies": {},
|
14
|
+
"scripts": {
|
15
|
+
"start": "coffee samples/chat_server.coffee"
|
16
|
+
},
|
17
|
+
"repository": {
|
18
|
+
"type": "git",
|
19
|
+
"url": "git@gitlab.com:agiliumlabs/statscloud.io-ruby-socket.io-client-simple.git"
|
20
|
+
},
|
21
|
+
"bugs": {
|
22
|
+
"url": "https://gitlab.com/agiliumlabs/statscloud.io-ruby-socket.io-client-simple"
|
23
|
+
}
|
24
|
+
}
|
@@ -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
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env coffee
|
2
|
+
## npm insatll socket.io-client coffee-script
|
3
|
+
|
4
|
+
url = process.argv[2] or 'http://localhost:3000'
|
5
|
+
socket = require('socket.io-client').connect url, {transports: ['websocket']}
|
6
|
+
|
7
|
+
socket.on 'connect', ->
|
8
|
+
console.log 'connect!!'
|
9
|
+
|
10
|
+
socket.on 'disconnect', ->
|
11
|
+
console.log 'disconnected'
|
12
|
+
|
13
|
+
socket.on 'chat', (data) ->
|
14
|
+
console.log "> " + data.msg
|
15
|
+
|
16
|
+
process.stdin.setEncoding 'utf8'
|
17
|
+
process.stdin.on 'data', (data) ->
|
18
|
+
msg = data.replace(/[\r\n]/g, '')
|
19
|
+
return if msg.length < 1
|
20
|
+
socket.emit 'chat', {msg: msg, at: Date.now()}
|
data/samples/sample.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
2
|
+
require 'rubygems'
|
3
|
+
require 'statscloud.io-ruby-socket.io-client-simple'
|
4
|
+
|
5
|
+
url = ARGV.shift || 'http://localhost:3000'
|
6
|
+
socket = StatsCloudIO::SocketIO::Client::Simple.connect url
|
7
|
+
|
8
|
+
# socket.auto_reconnection = false
|
9
|
+
|
10
|
+
#socket.websocket.on :message do |msg| ## inspect websocket data
|
11
|
+
# p msg.data
|
12
|
+
#end
|
13
|
+
|
14
|
+
socket.on :connect do
|
15
|
+
puts "connect!!!"
|
16
|
+
end
|
17
|
+
|
18
|
+
socket.on :disconnect do
|
19
|
+
puts "disconnected!!"
|
20
|
+
end
|
21
|
+
|
22
|
+
socket.on :chat do |data|
|
23
|
+
puts "> " + data['msg']
|
24
|
+
end
|
25
|
+
|
26
|
+
socket.on :error do |err|
|
27
|
+
p err
|
28
|
+
end
|
29
|
+
|
30
|
+
puts "please input and press Enter key"
|
31
|
+
loop do
|
32
|
+
msg = STDIN.gets.strip
|
33
|
+
next if msg.empty?
|
34
|
+
socket.emit :chat, {:msg => msg, :at => Time.now}
|
35
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'statscloud.io-ruby-socket.io-client-simple/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "statscloud.io-ruby-socket.io-client-simple"
|
8
|
+
spec.version = StatsCloudIO::SocketIO::Client::Simple::VERSION
|
9
|
+
spec.authors = ["Sho Hashimoto"]
|
10
|
+
spec.email = ["hashimoto@shokai.org"]
|
11
|
+
spec.description = "A simple ruby client for Node.js's Socket.IO v1.1.x, Supports only WebSocket."
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://gitlab.com/agiliumlabs/statscloud.io-ruby-socket.io-client-simple"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
|
25
|
+
spec.add_dependency "json"
|
26
|
+
spec.add_dependency "websocket-client-simple", '~> 0.3.0'
|
27
|
+
spec.add_dependency "httparty"
|
28
|
+
spec.add_dependency "event_emitter"
|
29
|
+
end
|
data/test/server.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class TestServer
|
2
|
+
|
3
|
+
def self.port
|
4
|
+
(ENV['PORT'] || 13000).to_i
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.start(exit_at=4000)
|
8
|
+
system "PORT=#{port} EXIT_AT=#{exit_at} npm start > /dev/null &"
|
9
|
+
sleep 1
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.url
|
13
|
+
"http://localhost:#{port}"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class TestSocketIOClientSimple < MiniTest::Test
|
4
|
+
|
5
|
+
TestServer.start
|
6
|
+
|
7
|
+
sleep 1
|
8
|
+
|
9
|
+
def test_connect
|
10
|
+
socket = StatsCloudIO::SocketIO::Client::Simple.connect TestServer.url
|
11
|
+
result = false
|
12
|
+
socket.on :connect do
|
13
|
+
result = true
|
14
|
+
end
|
15
|
+
sleep 0.5
|
16
|
+
assert result
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_emit_on
|
20
|
+
socket = StatsCloudIO::SocketIO::Client::Simple.connect TestServer.url
|
21
|
+
result = nil
|
22
|
+
socket.on :chat do |data| ## receive echo
|
23
|
+
result = data
|
24
|
+
end
|
25
|
+
|
26
|
+
post_data = {"msg" => "hello", "at" => Time.now.to_s, "あいさつ" => "こんにちは"}
|
27
|
+
socket.on :connect do
|
28
|
+
socket.emit :chat, post_data
|
29
|
+
end
|
30
|
+
|
31
|
+
sleep 0.5
|
32
|
+
assert_equal post_data, result
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_connect_with_query_parameter
|
36
|
+
user = "hashimoto.shokai"
|
37
|
+
socket = StatsCloudIO::SocketIO::Client::Simple.connect TestServer.url, :user => user
|
38
|
+
|
39
|
+
result = nil
|
40
|
+
|
41
|
+
socket.on :chat do |data|
|
42
|
+
result = data['user']
|
43
|
+
end
|
44
|
+
|
45
|
+
socket.on :connect do
|
46
|
+
socket.emit :chat, {"msg" => "hello (query parameter test)", "at" => Time.now.to_s}
|
47
|
+
end
|
48
|
+
sleep 0.5
|
49
|
+
|
50
|
+
assert_equal result, user
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_disconnect
|
54
|
+
socket = StatsCloudIO::SocketIO::Client::Simple.connect TestServer.url
|
55
|
+
|
56
|
+
socket.on :connect do
|
57
|
+
socket.disconnect
|
58
|
+
end
|
59
|
+
sleep 0.5
|
60
|
+
assert_equal socket.open?, false
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: statscloud.io-ruby-socket.io-client-simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1.pre.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sho Hashimoto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: websocket-client-simple
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.3.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.3.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: httparty
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: event_emitter
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: A simple ruby client for Node.js's Socket.IO v1.1.x, Supports only WebSocket.
|
112
|
+
email:
|
113
|
+
- hashimoto@shokai.org
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- Gemfile
|
120
|
+
- Gemfile.lock
|
121
|
+
- History.txt
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- lib/statscloud.io-ruby-socket.io-client-simple.rb
|
126
|
+
- lib/statscloud.io-ruby-socket.io-client-simple/byte_buffer.rb
|
127
|
+
- lib/statscloud.io-ruby-socket.io-client-simple/client.rb
|
128
|
+
- lib/statscloud.io-ruby-socket.io-client-simple/error.rb
|
129
|
+
- lib/statscloud.io-ruby-socket.io-client-simple/version.rb
|
130
|
+
- package.json
|
131
|
+
- samples/chat_server.coffee
|
132
|
+
- samples/sample.coffee
|
133
|
+
- samples/sample.rb
|
134
|
+
- statscloud.io-ruby-socket.io-client-simple.gemspec
|
135
|
+
- test/server.rb
|
136
|
+
- test/test_helper.rb
|
137
|
+
- test/test_socket.io.rb
|
138
|
+
homepage: https://gitlab.com/agiliumlabs/statscloud.io-ruby-socket.io-client-simple
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">"
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: 1.3.1
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.7.7
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: A simple ruby client for Node.js's Socket.IO v1.1.x, Supports only WebSocket.
|
162
|
+
test_files:
|
163
|
+
- test/server.rb
|
164
|
+
- test/test_helper.rb
|
165
|
+
- test/test_socket.io.rb
|