socket.io-emitter 0.0.1 → 0.1.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 +4 -1
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Rakefile +16 -1
- data/lib/socket.io-emitter.rb +1 -0
- data/lib/socket.io/emitter.rb +20 -26
- data/lib/socket.io/emitter/version.rb +1 -4
- data/socket.io-emitter.gemspec +1 -1
- data/spec/package.json +16 -0
- data/spec/redis.conf +27 -0
- data/spec/socket.io/emitter_spec.rb +37 -0
- data/spec/spec_helper.rb +30 -0
- data/spec/testapp.js +26 -0
- metadata +30 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0a74de36280d4fb1bb38c3203943bcbe3478df7
|
4
|
+
data.tar.gz: ccc8ceb16873c29cd4d5bc7fc8f56dc018d7e5ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eac67277a871d4168ca01324e77028ef0fee3bc4366cdc7d5e5004210222659f93dd0185f828eca8b84bae8c462a3dc56809470557adba54f2d555f7815e322d
|
7
|
+
data.tar.gz: b68ad524adfe3e3fe288f016a614b88168df4c0a5f9bbc5ada3539bd5ac6f0da9ff4004c3a2a07711eefc25d0916e2e74a2807e210ddff9288d4a8a93fd64473
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Rakefile
CHANGED
@@ -1,2 +1,17 @@
|
|
1
|
-
|
2
1
|
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
namespace :npm do
|
7
|
+
desc "npm package install for testing"
|
8
|
+
task :install do
|
9
|
+
dir = File.join(File.expand_path("..", __FILE__), "spec")
|
10
|
+
sh "cd '#{dir}' && npm install"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
task :spec_with_npm_install => ["npm:install", "spec"]
|
15
|
+
|
16
|
+
task :default => :spec_with_npm_install
|
17
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'socket.io/emitter'
|
data/lib/socket.io/emitter.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
require 'socket.io/emitter/version'
|
3
2
|
require 'msgpack'
|
4
3
|
require 'redis'
|
@@ -10,6 +9,8 @@ module SocketIO
|
|
10
9
|
BINARY_EVENT = 5
|
11
10
|
end
|
12
11
|
|
12
|
+
FLAGS = %w(json volatile broadcast)
|
13
|
+
|
13
14
|
def initialize(options = {})
|
14
15
|
@redis = options[:redis] || Redis.new
|
15
16
|
@key = "#{options[:key] || 'socket.io'}#emitter";
|
@@ -17,23 +18,18 @@ module SocketIO
|
|
17
18
|
@flags = {}
|
18
19
|
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
FLAGS.each do |flag|
|
22
|
+
define_method(flag) do
|
23
|
+
@flags[flag.to_sym] = true
|
24
|
+
self
|
24
25
|
end
|
25
|
-
@flags[name.to_sym] = true
|
26
|
-
self
|
27
26
|
end
|
28
27
|
|
29
28
|
def in(room)
|
30
29
|
@rooms << room unless @rooms.include?(room)
|
31
30
|
self
|
32
31
|
end
|
33
|
-
|
34
|
-
def to(room)
|
35
|
-
self.in(room)
|
36
|
-
end
|
32
|
+
alias :to :in
|
37
33
|
|
38
34
|
def of(nsp)
|
39
35
|
@flags[:nsp] = nsp
|
@@ -41,21 +37,11 @@ module SocketIO
|
|
41
37
|
end
|
42
38
|
|
43
39
|
def emit(*args)
|
44
|
-
data = []
|
45
40
|
packet = {}
|
46
|
-
packet[:type] = Type::EVENT
|
47
|
-
|
48
|
-
args.each do |arg|
|
49
|
-
data << arg.to_s
|
50
|
-
end
|
41
|
+
packet[:type] = has_binary?(args) ? Type::BINARY_EVENT : Type::EVENT
|
42
|
+
packet[:data] = args
|
51
43
|
|
52
|
-
if
|
53
|
-
packet[:type] = Type::BINARY_EVENT
|
54
|
-
end
|
55
|
-
|
56
|
-
packet[:data] = data
|
57
|
-
|
58
|
-
unless @flags[:nsp].nil?
|
44
|
+
if @flags.has_key?(:nsp)
|
59
45
|
packet[:nsp] = @flags[:nsp]
|
60
46
|
@flags.delete(:nsp)
|
61
47
|
else
|
@@ -65,10 +51,18 @@ module SocketIO
|
|
65
51
|
packed = MessagePack.pack([packet, { rooms: @rooms, flags: @flags }])
|
66
52
|
@redis.publish(@key, packed)
|
67
53
|
|
68
|
-
@rooms
|
69
|
-
@flags
|
54
|
+
@rooms.clear
|
55
|
+
@flags.clear
|
70
56
|
|
71
57
|
self
|
72
58
|
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def has_binary?(args)
|
63
|
+
args.select {|x| x.is_a?(String)}.any? {|str|
|
64
|
+
str.encoding == Encoding::ASCII_8BIT && !str.ascii_only?
|
65
|
+
}
|
66
|
+
end
|
73
67
|
end
|
74
68
|
end
|
data/socket.io-emitter.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
# coding: utf-8
|
3
2
|
|
4
3
|
lib = File.expand_path('../lib', __FILE__)
|
@@ -24,4 +23,5 @@ Gem::Specification.new do |spec|
|
|
24
23
|
|
25
24
|
spec.add_development_dependency "bundler", "~> 1.6"
|
26
25
|
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
27
|
end
|
data/spec/package.json
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
{
|
2
|
+
"name": "socket.io-ruby-emitter-test",
|
3
|
+
"version": "0.0.0",
|
4
|
+
"description": "test server",
|
5
|
+
"main": "testapp.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"author": "joker1007",
|
10
|
+
"license": "MIT",
|
11
|
+
"dependencies": {
|
12
|
+
"socket.io": "^1.0.6",
|
13
|
+
"socket.io-redis": "^0.1.3",
|
14
|
+
"socket.io-client": "~1.0.6"
|
15
|
+
}
|
16
|
+
}
|
data/spec/redis.conf
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
daemonize no
|
2
|
+
|
3
|
+
pidfile /tmp/redis-server-socket.io-emitter-test.pid
|
4
|
+
|
5
|
+
port 6380
|
6
|
+
bind 127.0.0.1
|
7
|
+
|
8
|
+
timeout 300
|
9
|
+
|
10
|
+
loglevel notice
|
11
|
+
logfile stdout
|
12
|
+
|
13
|
+
databases 16
|
14
|
+
|
15
|
+
dir /tmp
|
16
|
+
|
17
|
+
slave-serve-stale-data yes
|
18
|
+
|
19
|
+
hash-max-ziplist-entries 512
|
20
|
+
hash-max-ziplist-value 64
|
21
|
+
|
22
|
+
list-max-ziplist-entries 512
|
23
|
+
list-max-ziplist-value 64
|
24
|
+
|
25
|
+
set-max-intset-entries 512
|
26
|
+
|
27
|
+
activerehashing yes
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
describe SocketIO::Emitter do
|
5
|
+
include Timeout
|
6
|
+
|
7
|
+
let(:emitter) { SocketIO::Emitter.new(redis: Redis.new(host: "localhost", port: 6380)) }
|
8
|
+
it 'should be able to emit messages to client' do
|
9
|
+
emitter.emit('broadcast event', 'broadcast payload')
|
10
|
+
|
11
|
+
timeout(1) do
|
12
|
+
expect($child_io.gets.chomp).to eq 'broadcast payload'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should be able to emit messages to namespace' do
|
17
|
+
emitter.of('/nsp').broadcast.emit('broadcast event', 'nsp broadcast payload')
|
18
|
+
|
19
|
+
timeout(1) do
|
20
|
+
expect($child_io.gets.chomp).to eq 'nsp broadcast payload'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should not emit message to all namespace' do
|
25
|
+
emitter.of('/nsp').broadcast.emit('nsp broadcast event', 'nsp broadcast payload')
|
26
|
+
|
27
|
+
timeout(1) do
|
28
|
+
expect($child_io.gets.chomp).to eq 'GOOD'
|
29
|
+
end
|
30
|
+
|
31
|
+
expect {
|
32
|
+
timeout(2) do
|
33
|
+
$child_io.gets
|
34
|
+
end
|
35
|
+
}.to raise_error(TimeoutError)
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
pwd = File.expand_path('..', __FILE__)
|
4
|
+
require 'socket.io-emitter'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.before(:suite) do
|
8
|
+
redis_log = File.open(File.join(pwd, "redis.log"), "w")
|
9
|
+
redis_pid = fork do
|
10
|
+
exec("redis-server #{File.join(pwd, "redis.conf")}", :out => redis_log)
|
11
|
+
end
|
12
|
+
|
13
|
+
sleep 1
|
14
|
+
|
15
|
+
$child_io = IO.popen("node #{File.join(File.dirname(__FILE__), "testapp.js")}")
|
16
|
+
|
17
|
+
sleep 0.5
|
18
|
+
|
19
|
+
at_exit do
|
20
|
+
begin
|
21
|
+
Process.kill(:SIGINT, $child_io.pid)
|
22
|
+
Process.waitpid($child_io.pid)
|
23
|
+
Process.kill(:SIGINT, redis_pid)
|
24
|
+
Process.waitpid(redis_pid)
|
25
|
+
redis_log.close
|
26
|
+
rescue Errno::ESRCH, Errno::ECHILD
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/testapp.js
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
var io = require('socket.io');
|
2
|
+
var ioc = require('socket.io-client');
|
3
|
+
var redis = require('socket.io-redis');
|
4
|
+
|
5
|
+
var srv = io(8125);
|
6
|
+
srv.adapter(redis({ host: 'localhost', port: 6380 }));
|
7
|
+
|
8
|
+
var cli = ioc('ws://localhost:8125', {forceNew: true});
|
9
|
+
|
10
|
+
cli.on('broadcast event', function(payload) {
|
11
|
+
console.log(payload);
|
12
|
+
});
|
13
|
+
|
14
|
+
cli.on('nsp broadcast event', function(payload) {
|
15
|
+
console.log("BAD");
|
16
|
+
});
|
17
|
+
|
18
|
+
var cliNsp = ioc('ws://localhost:8125/nsp', {forceNew: true});
|
19
|
+
|
20
|
+
cliNsp.on('broadcast event', function(payload) {
|
21
|
+
console.log(payload);
|
22
|
+
});
|
23
|
+
|
24
|
+
cliNsp.on('nsp broadcast event', function(payload) {
|
25
|
+
console.log("GOOD");
|
26
|
+
});
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socket.io-emitter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nulltask
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description:
|
70
84
|
email:
|
71
85
|
- nulltask@gmail.com
|
@@ -74,12 +88,20 @@ extensions: []
|
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
90
|
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
77
93
|
- Gemfile
|
78
94
|
- Rakefile
|
79
95
|
- Readme.md
|
96
|
+
- lib/socket.io-emitter.rb
|
80
97
|
- lib/socket.io/emitter.rb
|
81
98
|
- lib/socket.io/emitter/version.rb
|
82
99
|
- socket.io-emitter.gemspec
|
100
|
+
- spec/package.json
|
101
|
+
- spec/redis.conf
|
102
|
+
- spec/socket.io/emitter_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/testapp.js
|
83
105
|
homepage: https://github.com/nulltask/socket.io-ruby-emitter
|
84
106
|
licenses:
|
85
107
|
- MIT
|
@@ -104,4 +126,9 @@ rubygems_version: 2.2.2
|
|
104
126
|
signing_key:
|
105
127
|
specification_version: 4
|
106
128
|
summary: Ruby Socket.IO emitter implementation.
|
107
|
-
test_files:
|
129
|
+
test_files:
|
130
|
+
- spec/package.json
|
131
|
+
- spec/redis.conf
|
132
|
+
- spec/socket.io/emitter_spec.rb
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
- spec/testapp.js
|