WEBSocket 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Rakefile +33 -0
- data/WEBSocket.gemspec +1 -1
- data/lib/WEBSocket.rb +1 -0
- data/lib/WEBSocket/Celluloid/acceptedSocket.rb +34 -0
- data/lib/WEBSocket/Celluloid/server.rb +1 -103
- data/lib/WEBSocket/Celluloid/socket.rb +1 -51
- data/lib/WEBSocket/Standard/acceptedSocket.rb +14 -0
- data/lib/WEBSocket/Standard/server.rb +1 -107
- data/lib/WEBSocket/Standard/socket.rb +1 -67
- data/lib/WEBSocket/acceptedSocket.rb +96 -0
- data/lib/WEBSocket/server.rb +22 -0
- data/lib/WEBSocket/socket.rb +83 -0
- data/lib/WEBSocket/version.rb +1 -1
- data/test/Rakefile +15 -0
- data/test/celluloid/clientTest.rb +16 -0
- data/test/celluloid/serverTest.rb +32 -0
- data/test/standard/clientTest.rb +15 -0
- data/test/standard/serverTest.rb +31 -0
- metadata +18 -5
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1 +1,34 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
task :default => [:build]
|
4
|
+
|
5
|
+
def rake rule
|
6
|
+
Process.fork do
|
7
|
+
Process.exec 'rake', rule
|
8
|
+
end
|
9
|
+
Process.wait
|
10
|
+
end
|
11
|
+
|
12
|
+
task :standardServer do
|
13
|
+
desc "Launch a echo server to make tests. Uses standard Ruby sockets"
|
14
|
+
Dir.chdir 'test'
|
15
|
+
rake "standardServer"
|
16
|
+
end
|
17
|
+
|
18
|
+
task :standardClient do
|
19
|
+
desc "Launch a hello world client to make tests. Uses standard Ruby sockets"
|
20
|
+
Dir.chdir 'test'
|
21
|
+
rake "standardClient"
|
22
|
+
end
|
23
|
+
|
24
|
+
task :celluloidServer do
|
25
|
+
desc "Launch a echo server to make tests. Uses Celluloid::IO sockets"
|
26
|
+
Dir.chdir 'test'
|
27
|
+
rake "celluloidServer"
|
28
|
+
end
|
29
|
+
|
30
|
+
task :celluloidClient do
|
31
|
+
desc "Launch a hello world client to make tests. Uses Celluloid::IO sockets"
|
32
|
+
Dir.chdir 'test'
|
33
|
+
rake "celluloidClient"
|
34
|
+
end
|
data/WEBSocket.gemspec
CHANGED
@@ -19,5 +19,5 @@ EOS
|
|
19
19
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
20
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
21
|
gem.require_paths = ["lib"]
|
22
|
-
gem.add_runtime_dependency 'websocket', '~> 1.0
|
22
|
+
gem.add_runtime_dependency 'websocket', '~> 1.0', '>= 1.0.6'
|
23
23
|
end
|
data/lib/WEBSocket.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
module WEBSocket
|
2
|
+
class AcceptedSocket < Base::AcceptedSocket
|
3
|
+
extend Forwardable
|
4
|
+
attr_reader :status
|
5
|
+
|
6
|
+
def_delegators :@socket, :close, :closed?
|
7
|
+
def_delegators :@socket, :addr, :peeraddr, :setsockopt
|
8
|
+
|
9
|
+
def acquire_ownership type
|
10
|
+
@socket.acquire_ownership type
|
11
|
+
end
|
12
|
+
|
13
|
+
def evented?
|
14
|
+
@socket.evented?
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize socket
|
18
|
+
@socket = socket
|
19
|
+
@status = :disconnected
|
20
|
+
end
|
21
|
+
|
22
|
+
def release_ownership type
|
23
|
+
@socket.release_ownership type
|
24
|
+
end
|
25
|
+
|
26
|
+
def wait_readable
|
27
|
+
@socket.wait_readable
|
28
|
+
end
|
29
|
+
|
30
|
+
def wait_writable
|
31
|
+
@socket.wait_writable
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,114 +1,12 @@
|
|
1
1
|
module WEBSocket
|
2
|
-
class Server
|
2
|
+
class Server < Base::Server
|
3
3
|
extend Forwardable
|
4
4
|
def_delegators :@server, :listen, :sysaccept, :close, :closed?, :addr
|
5
5
|
|
6
|
-
class AcceptedSocket
|
7
|
-
extend Forwardable
|
8
|
-
attr_reader :status
|
9
|
-
|
10
|
-
def_delegators :@socket, :close, :closed?
|
11
|
-
def_delegators :@socket, :addr, :peeraddr, :setsockopt
|
12
|
-
|
13
|
-
def acquire_ownership type
|
14
|
-
@socket.acquire_ownership type
|
15
|
-
end
|
16
|
-
|
17
|
-
def connect
|
18
|
-
@handshake = WebSocket::Handshake::Server.new
|
19
|
-
msg = @socket.readpartial 4096
|
20
|
-
@handshake << msg
|
21
|
-
until @handshake.finished?
|
22
|
-
msg = @socket.readpartial 4096
|
23
|
-
@handshake << msg
|
24
|
-
end
|
25
|
-
if @handshake.valid?
|
26
|
-
@socket.write @handshake.to_s
|
27
|
-
@status = :connected
|
28
|
-
end
|
29
|
-
self
|
30
|
-
end
|
31
|
-
|
32
|
-
def evented?
|
33
|
-
@socket.evented?
|
34
|
-
end
|
35
|
-
|
36
|
-
def initialize socket
|
37
|
-
@socket = socket
|
38
|
-
@status = :disconnected
|
39
|
-
end
|
40
|
-
|
41
|
-
def read length = nil, buffer = nil
|
42
|
-
s = @socket.read length, buffer
|
43
|
-
frame = WebSocket::Frame::Incoming::Server.new :version => @handshake.version
|
44
|
-
frame << s
|
45
|
-
frame.next.to_s
|
46
|
-
end
|
47
|
-
|
48
|
-
def read_nonblock length, buffer = nil
|
49
|
-
buffer ||= ''
|
50
|
-
|
51
|
-
s = @socket.read length, buffer
|
52
|
-
frame = WebSocket::Frame::Incoming::Server.new :version => @handshake.version
|
53
|
-
frame << s
|
54
|
-
frame.next.to_s
|
55
|
-
end
|
56
|
-
|
57
|
-
def readpartial length, buffer = nil
|
58
|
-
s = @socket.readpartial length, buffer
|
59
|
-
frame = WebSocket::Frame::Incoming::Server.new :version => @handshake.version
|
60
|
-
frame << s
|
61
|
-
frame.next.to_s
|
62
|
-
end
|
63
|
-
|
64
|
-
def release_ownership type
|
65
|
-
@socket.release_ownership type
|
66
|
-
end
|
67
|
-
|
68
|
-
def to_io
|
69
|
-
@socket
|
70
|
-
end
|
71
|
-
|
72
|
-
def wait_readable
|
73
|
-
@socket.wait_readable
|
74
|
-
end
|
75
|
-
|
76
|
-
def wait_writable
|
77
|
-
@socket.wait_writable
|
78
|
-
end
|
79
|
-
|
80
|
-
def write s
|
81
|
-
frame = WebSocket::Frame::Outgoing::Server.new :version => @handshake.version, :data => s, :type => :text
|
82
|
-
@socket.write frame.to_s
|
83
|
-
end
|
84
|
-
|
85
|
-
def write_nonblock s
|
86
|
-
frame = WebSocket::Frame::Outgoing::Server.new :version => @handshake.version, :data => s, :type => :text
|
87
|
-
@socket.write_nonblock frame.to_s
|
88
|
-
end
|
89
|
-
|
90
|
-
def << s
|
91
|
-
frame = WebSocket::Frame::Outgoing::Server.new :version => @handshake.version, :data => s, :type => :text
|
92
|
-
@socket << frame.to_s
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
6
|
def initialize hostname, port
|
97
7
|
@server = Celluloid::IO::TCPServer.new hostname, port
|
98
8
|
end
|
99
9
|
|
100
|
-
def accept
|
101
|
-
AcceptedSocket.new(@server.accept).connect
|
102
|
-
end
|
103
|
-
|
104
|
-
def accept_nonblock
|
105
|
-
AcceptedSocket.new(@server.accept_nonblock).connect
|
106
|
-
end
|
107
|
-
|
108
|
-
def to_io
|
109
|
-
@server
|
110
|
-
end
|
111
|
-
|
112
10
|
def evented?
|
113
11
|
@server.evented?
|
114
12
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module WEBSocket
|
2
|
-
class Socket
|
2
|
+
class Socket < Base::Socket
|
3
3
|
extend Forwardable
|
4
4
|
attr_reader :status
|
5
5
|
|
@@ -10,14 +10,6 @@ module WEBSocket
|
|
10
10
|
@socket.acquire_ownership type
|
11
11
|
end
|
12
12
|
|
13
|
-
def connect host, port
|
14
|
-
@handshake = WebSocket::Handshake::Client.new :url => "ws://#{host}:#{port}"
|
15
|
-
@socket.write @handshake.to_s
|
16
|
-
msg = @socket.readpartial 4096
|
17
|
-
@handshake << msg
|
18
|
-
@status = :connected
|
19
|
-
end
|
20
|
-
|
21
13
|
def evented?
|
22
14
|
@socket.evented?
|
23
15
|
end
|
@@ -28,37 +20,10 @@ module WEBSocket
|
|
28
20
|
connect rhost, rport
|
29
21
|
end
|
30
22
|
|
31
|
-
def read length = nil, buffer = nil
|
32
|
-
s = @socket.read length, buffer
|
33
|
-
frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
|
34
|
-
frame << s
|
35
|
-
frame.to_s
|
36
|
-
end
|
37
|
-
|
38
|
-
def read_nonblock length, buffer = nil
|
39
|
-
buffer ||= ''
|
40
|
-
|
41
|
-
@socket.read_nonblock length, buffer
|
42
|
-
frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
|
43
|
-
frame << buffer
|
44
|
-
frame.to_s
|
45
|
-
end
|
46
|
-
|
47
|
-
def readpartial length, buffer = nil
|
48
|
-
s = @socket.readpartial length, buffer
|
49
|
-
frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
|
50
|
-
frame << s
|
51
|
-
frame.to_s
|
52
|
-
end
|
53
|
-
|
54
23
|
def release_ownership type
|
55
24
|
@socket.release_ownership type
|
56
25
|
end
|
57
26
|
|
58
|
-
def to_io
|
59
|
-
@socket
|
60
|
-
end
|
61
|
-
|
62
27
|
def wait_readable
|
63
28
|
@socket.wait_readable
|
64
29
|
end
|
@@ -66,20 +31,5 @@ module WEBSocket
|
|
66
31
|
def wait_writable
|
67
32
|
@socket.wait_writable
|
68
33
|
end
|
69
|
-
|
70
|
-
def write s
|
71
|
-
frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => s, :type => :text
|
72
|
-
@socket.write frame.to_s
|
73
|
-
end
|
74
|
-
|
75
|
-
def write_nonblock s
|
76
|
-
frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => s, :type => :text
|
77
|
-
@socket.write_nonblock frame.to_s
|
78
|
-
end
|
79
|
-
|
80
|
-
def << s
|
81
|
-
frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => s, :type => :text
|
82
|
-
@socket << frame.to_s
|
83
|
-
end
|
84
34
|
end
|
85
35
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module WEBSocket
|
2
|
+
class AcceptedSocket < Base::AcceptedSocket
|
3
|
+
extend Forwardable
|
4
|
+
attr_reader :status
|
5
|
+
|
6
|
+
def_delegators :@socket, :close, :closed?
|
7
|
+
def_delegators :@socket, :addr, :peeraddr, :setsockopt
|
8
|
+
|
9
|
+
def initialize socket
|
10
|
+
@socket = socket
|
11
|
+
@status = :disconnected
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,116 +1,10 @@
|
|
1
1
|
module WEBSocket
|
2
|
-
|
2
|
+
class Server < Base::Server
|
3
3
|
extend Forwardable
|
4
4
|
def_delegators :@server, :listen, :sysaccept, :close, :closed?, :addr
|
5
5
|
|
6
|
-
class AcceptedSocket
|
7
|
-
extend Forwardable
|
8
|
-
attr_reader :status
|
9
|
-
|
10
|
-
def_delegators :@socket, :close, :closed?
|
11
|
-
def_delegators :@socket, :addr, :peeraddr, :setsockopt
|
12
|
-
|
13
|
-
def acquire_ownership type
|
14
|
-
@socket.acquire_ownership type
|
15
|
-
end
|
16
|
-
|
17
|
-
def connect
|
18
|
-
@handshake = WebSocket::Handshake::Server.new
|
19
|
-
msg = @socket.readpartial 4096
|
20
|
-
@handshake << msg
|
21
|
-
until @handshake.finished?
|
22
|
-
msg = @socket.readpartial 4096
|
23
|
-
@handshake << msg
|
24
|
-
end
|
25
|
-
if @handshake.valid?
|
26
|
-
@socket.write @handshake.to_s
|
27
|
-
@status = :connected
|
28
|
-
end
|
29
|
-
self
|
30
|
-
end
|
31
|
-
|
32
|
-
def evented?
|
33
|
-
false
|
34
|
-
end
|
35
|
-
|
36
|
-
def initialize socket
|
37
|
-
@socket = socket
|
38
|
-
@status = :disconnected
|
39
|
-
end
|
40
|
-
|
41
|
-
def read length = nil, buffer = nil
|
42
|
-
s = @socket.read length, buffer
|
43
|
-
frame = WebSocket::Frame::Incoming::Server.new :version => @handshake.version
|
44
|
-
frame << s
|
45
|
-
frame.next.to_s
|
46
|
-
end
|
47
|
-
|
48
|
-
def read_nonblock length, buffer = nil
|
49
|
-
buffer ||= ''
|
50
|
-
|
51
|
-
s = @socket.read length, buffer
|
52
|
-
frame = WebSocket::Frame::Incoming::Server.new :version => @handshake.version
|
53
|
-
frame << s
|
54
|
-
frame.next.to_s
|
55
|
-
end
|
56
|
-
|
57
|
-
def readpartial length, buffer = nil
|
58
|
-
s = @socket.readpartial length, buffer
|
59
|
-
frame = WebSocket::Frame::Incoming::Server.new :version => @handshake.version
|
60
|
-
frame << s
|
61
|
-
frame.next.to_s
|
62
|
-
end
|
63
|
-
|
64
|
-
def release_ownership type
|
65
|
-
@socket.release_ownership type
|
66
|
-
end
|
67
|
-
|
68
|
-
def to_io
|
69
|
-
@socket
|
70
|
-
end
|
71
|
-
|
72
|
-
def wait_readable
|
73
|
-
@socket.wait_readable
|
74
|
-
end
|
75
|
-
|
76
|
-
def wait_writable
|
77
|
-
@socket.wait_writable
|
78
|
-
end
|
79
|
-
|
80
|
-
def write s
|
81
|
-
frame = WebSocket::Frame::Outgoing::Server.new :version => @handshake.version, :data => s, :type => :text
|
82
|
-
@socket.write frame.to_s
|
83
|
-
end
|
84
|
-
|
85
|
-
def write_nonblock s
|
86
|
-
frame = WebSocket::Frame::Outgoing::Server.new :version => @handshake.version, :data => s, :type => :text
|
87
|
-
@socket.write_nonblock frame.to_s
|
88
|
-
end
|
89
|
-
|
90
|
-
def << s
|
91
|
-
frame = WebSocket::Frame::Outgoing::Server.new :version => @handshake.version, :data => s, :type => :text
|
92
|
-
@socket << frame.to_s
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
6
|
def initialize hostname, port
|
97
7
|
@server = TCPServer.new hostname, port
|
98
8
|
end
|
99
|
-
|
100
|
-
def accept
|
101
|
-
AcceptedSocket.new(@server.accept).connect
|
102
|
-
end
|
103
|
-
|
104
|
-
def accept_nonblock
|
105
|
-
AcceptedSocket.new(@server.accept_nonblock).connect
|
106
|
-
end
|
107
|
-
|
108
|
-
def to_io
|
109
|
-
@server
|
110
|
-
end
|
111
|
-
|
112
|
-
def evented?
|
113
|
-
false
|
114
|
-
end
|
115
9
|
end
|
116
10
|
end
|
@@ -1,81 +1,15 @@
|
|
1
1
|
module WEBSocket
|
2
|
-
class Socket
|
2
|
+
class Socket < Base::Socket
|
3
3
|
extend Forwardable
|
4
4
|
attr_reader :status
|
5
5
|
|
6
6
|
def_delegators :@socket, :close, :closed?
|
7
7
|
def_delegators :@socket, :addr, :peeraddr, :setsockopt
|
8
8
|
|
9
|
-
def acquire_ownership type
|
10
|
-
end
|
11
|
-
|
12
|
-
def connect host, port
|
13
|
-
@handshake = WebSocket::Handshake::Client.new :url => "ws://#{host}:#{port}"
|
14
|
-
@socket.write @handshake.to_s
|
15
|
-
msg = @socket.readpartial 4096
|
16
|
-
@handshake << msg
|
17
|
-
@status = :connected
|
18
|
-
end
|
19
|
-
|
20
|
-
def evented?
|
21
|
-
false
|
22
|
-
end
|
23
|
-
|
24
9
|
def initialize rhost, rport, lhost = nil, lport = nil
|
25
10
|
@status = :disconnected
|
26
11
|
@socket = TCPSocket.new rhost, rport, lhost, lport
|
27
12
|
connect rhost, rport
|
28
13
|
end
|
29
|
-
|
30
|
-
def read length = nil, buffer = nil
|
31
|
-
s = @socket.read length, buffer
|
32
|
-
frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
|
33
|
-
frame << s
|
34
|
-
frame.to_s
|
35
|
-
end
|
36
|
-
|
37
|
-
def read_nonblock length, buffer = nil
|
38
|
-
buffer ||= ''
|
39
|
-
|
40
|
-
@socket.read_nonblock length, buffer
|
41
|
-
frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
|
42
|
-
frame << buffer
|
43
|
-
frame.to_s
|
44
|
-
end
|
45
|
-
|
46
|
-
def readpartial length, buffer = nil
|
47
|
-
s = @socket.readpartial length, buffer
|
48
|
-
frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
|
49
|
-
frame << s
|
50
|
-
frame.to_s
|
51
|
-
end
|
52
|
-
|
53
|
-
def release_ownership type
|
54
|
-
end
|
55
|
-
|
56
|
-
def to_io
|
57
|
-
@socket
|
58
|
-
end
|
59
|
-
|
60
|
-
def wait_readable
|
61
|
-
end
|
62
|
-
|
63
|
-
def wait_writable
|
64
|
-
end
|
65
|
-
|
66
|
-
def write s
|
67
|
-
frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => s, :type => :text
|
68
|
-
@socket.write frame.to_s
|
69
|
-
end
|
70
|
-
|
71
|
-
def write_nonblock s
|
72
|
-
frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => s, :type => :text
|
73
|
-
@socket.write_nonblock frame.to_s
|
74
|
-
end
|
75
|
-
|
76
|
-
def << s
|
77
|
-
frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => s, :type => :text
|
78
|
-
@socket << frame.to_s
|
79
|
-
end
|
80
14
|
end
|
81
15
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module WEBSocket
|
2
|
+
module Base
|
3
|
+
class AcceptedSocket
|
4
|
+
def acquire_ownership type
|
5
|
+
end
|
6
|
+
|
7
|
+
def connect
|
8
|
+
@handshake = WebSocket::Handshake::Server.new
|
9
|
+
msg = @socket.readpartial 4096
|
10
|
+
@handshake << msg
|
11
|
+
until @handshake.finished?
|
12
|
+
msg = @socket.readpartial 4096
|
13
|
+
@handshake << msg
|
14
|
+
end
|
15
|
+
if @handshake.valid?
|
16
|
+
@socket.write @handshake.to_s
|
17
|
+
@status = :connected
|
18
|
+
end
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def evented?
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
def from_frame_to_text str
|
27
|
+
frame = WebSocket::Frame::Incoming::Server.new :version => @handshake.version
|
28
|
+
frame << str
|
29
|
+
frame.next.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def from_text_to_frame str
|
33
|
+
frame = WebSocket::Frame::Outgoing::Server.new :version => @handshake.version, :data => str, :type => :text
|
34
|
+
frame.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
def read length = nil, buffer = nil
|
38
|
+
s = nil
|
39
|
+
if length
|
40
|
+
if length.kind_of? Numeric
|
41
|
+
s = @socket.read length + 4, buffer
|
42
|
+
elsif buffer.nil?
|
43
|
+
s = @socket.read nil, length
|
44
|
+
end
|
45
|
+
else
|
46
|
+
s = @socket.read nil, nil
|
47
|
+
end
|
48
|
+
from_frame_to_text s
|
49
|
+
end
|
50
|
+
|
51
|
+
def read_nonblock length, buffer = nil
|
52
|
+
buffer ||= ''
|
53
|
+
|
54
|
+
@socket.read_nonblock length + 4, buffer
|
55
|
+
from_frame_to_text buffer
|
56
|
+
end
|
57
|
+
|
58
|
+
def readpartial length, buffer = nil
|
59
|
+
s = @socket.readpartial length + 4, buffer
|
60
|
+
from_frame_to_text s
|
61
|
+
end
|
62
|
+
|
63
|
+
def release_ownership type
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_io
|
67
|
+
@socket
|
68
|
+
end
|
69
|
+
|
70
|
+
def wait_readable
|
71
|
+
end
|
72
|
+
|
73
|
+
def wait_writable
|
74
|
+
end
|
75
|
+
|
76
|
+
def write s
|
77
|
+
@socket.write from_text_to_frame(s.to_s)
|
78
|
+
end
|
79
|
+
|
80
|
+
def write_nonblock s
|
81
|
+
@socket.write_nonblock from_text_to_frame(s.to_s)
|
82
|
+
end
|
83
|
+
|
84
|
+
def << s
|
85
|
+
@socket << from_text_to_frame(s.to_s)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
if defined? Celluloid::IO
|
92
|
+
require 'WEBSocket/Celluloid/acceptedSocket'
|
93
|
+
else
|
94
|
+
require 'socket'
|
95
|
+
require 'WEBSocket/Standard/acceptedSocket'
|
96
|
+
end
|
data/lib/WEBSocket/server.rb
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
module WEBSocket
|
2
|
+
module Base
|
3
|
+
class Server
|
4
|
+
def accept
|
5
|
+
WEBSocket::AcceptedSocket.new(@server.accept).connect
|
6
|
+
end
|
7
|
+
|
8
|
+
def accept_nonblock
|
9
|
+
WEBSocket::AcceptedSocket.new(@server.accept_nonblock).connect
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_io
|
13
|
+
@server
|
14
|
+
end
|
15
|
+
|
16
|
+
def evented?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
1
23
|
if defined? Celluloid::IO
|
2
24
|
require 'WEBSocket/Celluloid/server'
|
3
25
|
else
|
data/lib/WEBSocket/socket.rb
CHANGED
@@ -1,3 +1,86 @@
|
|
1
|
+
module WEBSocket
|
2
|
+
module Base
|
3
|
+
class Socket
|
4
|
+
def acquire_ownership type
|
5
|
+
end
|
6
|
+
|
7
|
+
def connect host, port
|
8
|
+
@handshake = WebSocket::Handshake::Client.new :url => "ws://#{host}:#{port}"
|
9
|
+
@socket.write @handshake.to_s
|
10
|
+
msg = @socket.readpartial 4096
|
11
|
+
@handshake << msg
|
12
|
+
@status = :connected
|
13
|
+
end
|
14
|
+
|
15
|
+
def evented?
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
def from_frame_to_text str
|
20
|
+
frame = WebSocket::Frame::Incoming::Client.new :version => @handshake.version
|
21
|
+
frame << str
|
22
|
+
frame.next.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def from_text_to_frame str
|
26
|
+
frame = WebSocket::Frame::Outgoing::Client.new :version => @handshake.version, :data => str, :type => :text
|
27
|
+
frame.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
def read length = nil, buffer = nil
|
31
|
+
s = nil
|
32
|
+
if length
|
33
|
+
if length.kind_of? Numeric
|
34
|
+
s = @socket.read length + 4, buffer
|
35
|
+
elsif buffer.nil?
|
36
|
+
s = @socket.read nil, length
|
37
|
+
end
|
38
|
+
else
|
39
|
+
s = @socket.read nil, nil
|
40
|
+
end
|
41
|
+
from_frame_to_text s
|
42
|
+
end
|
43
|
+
|
44
|
+
def read_nonblock length, buffer = nil
|
45
|
+
buffer ||= ''
|
46
|
+
|
47
|
+
@socket.read_nonblock length + 4, buffer
|
48
|
+
from_frame_to_text buffer
|
49
|
+
end
|
50
|
+
|
51
|
+
def readpartial length, buffer = nil
|
52
|
+
s = @socket.readpartial length + 4, buffer
|
53
|
+
from_frame_to_text s
|
54
|
+
end
|
55
|
+
|
56
|
+
def release_ownership type
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_io
|
60
|
+
@socket
|
61
|
+
end
|
62
|
+
|
63
|
+
def wait_readable
|
64
|
+
end
|
65
|
+
|
66
|
+
def wait_writable
|
67
|
+
end
|
68
|
+
|
69
|
+
def write s
|
70
|
+
@socket.write from_text_to_frame(s.to_s)
|
71
|
+
end
|
72
|
+
|
73
|
+
def write_nonblock s
|
74
|
+
@socket.write_nonblock from_text_to_frame(s.to_s)
|
75
|
+
end
|
76
|
+
|
77
|
+
def << s
|
78
|
+
@socket << from_text_to_frame(s.to_s)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
1
84
|
if defined? Celluloid::IO
|
2
85
|
require 'WEBSocket/Celluloid/socket'
|
3
86
|
else
|
data/lib/WEBSocket/version.rb
CHANGED
data/test/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
task :standardServer do
|
2
|
+
ruby "standard/serverTest.rb"
|
3
|
+
end
|
4
|
+
|
5
|
+
task :standardClient do
|
6
|
+
ruby "standard/clientTest.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
task :celluloidServer do
|
10
|
+
ruby "celluloid/serverTest.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
task :celluloidClient do
|
14
|
+
ruby "celluloid/clientTest.rb"
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
$:.unshift '../lib'
|
2
|
+
|
3
|
+
require 'celluloid/io'
|
4
|
+
require 'WEBSocket'
|
5
|
+
|
6
|
+
Host = '127.0.0.1'
|
7
|
+
Port = 4242
|
8
|
+
Size = 4096
|
9
|
+
|
10
|
+
client = WEBSocket::Socket.new Host, Port
|
11
|
+
$stderr.puts "==> Client was created : #{client}[#{Host}:#{Port}]"
|
12
|
+
s = "Hello World"
|
13
|
+
client.write s
|
14
|
+
$stderr.puts "==> Written #{s}"
|
15
|
+
s2 = client.readpartial Size
|
16
|
+
$stderr.puts "==> Read #{s2}"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift '../lib'
|
2
|
+
|
3
|
+
require 'celluloid/io'
|
4
|
+
require 'WEBSocket'
|
5
|
+
|
6
|
+
Host = '127.0.0.1'
|
7
|
+
Port = 4242
|
8
|
+
Size = 4096
|
9
|
+
|
10
|
+
Signal.trap "INT" do
|
11
|
+
puts "\rExiting"
|
12
|
+
exit 0
|
13
|
+
end
|
14
|
+
|
15
|
+
server = WEBSocket::Server.new Host, Port
|
16
|
+
$stderr.puts "==> Server was created : #{server}[#{Host}:#{Port}]"
|
17
|
+
client = server.accept
|
18
|
+
$stderr.puts "==> AcceptedSocket was created : #{client}"
|
19
|
+
_, port, host = client.peeraddr
|
20
|
+
$stderr.puts "==> Received connection from #{host}:#{port}"
|
21
|
+
loop do
|
22
|
+
s = nil
|
23
|
+
begin
|
24
|
+
s = client.readpartial Size
|
25
|
+
rescue EOFError
|
26
|
+
break
|
27
|
+
end
|
28
|
+
break if s.nil? or s == ""
|
29
|
+
$stderr.puts "==> Read #{s}"
|
30
|
+
client.write s
|
31
|
+
$stderr.puts "==> Written #{s}"
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.unshift '../lib'
|
2
|
+
|
3
|
+
require 'WEBSocket'
|
4
|
+
|
5
|
+
Host = '127.0.0.1'
|
6
|
+
Port = 4242
|
7
|
+
Size = 4096
|
8
|
+
|
9
|
+
client = WEBSocket::Socket.new Host, Port
|
10
|
+
$stderr.puts "==> Client was created : #{client}[#{Host}:#{Port}]"
|
11
|
+
s = "Hello World"
|
12
|
+
client.write s
|
13
|
+
$stderr.puts "==> Written #{s}"
|
14
|
+
s2 = client.readpartial Size
|
15
|
+
$stderr.puts "==> Read #{s2}"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
$:.unshift '../lib'
|
2
|
+
|
3
|
+
require 'WEBSocket'
|
4
|
+
|
5
|
+
Host = '127.0.0.1'
|
6
|
+
Port = 4242
|
7
|
+
Size = 4096
|
8
|
+
|
9
|
+
Signal.trap "INT" do
|
10
|
+
puts "\rExiting"
|
11
|
+
exit 0
|
12
|
+
end
|
13
|
+
|
14
|
+
server = WEBSocket::Server.new Host, Port
|
15
|
+
$stderr.puts "==> Server was created : #{server}[#{Host}:#{Port}]"
|
16
|
+
client = server.accept
|
17
|
+
$stderr.puts "==> AcceptedSocket was created : #{client}"
|
18
|
+
_, port, host = client.peeraddr
|
19
|
+
$stderr.puts "==> Received connection from #{host}:#{port}"
|
20
|
+
loop do
|
21
|
+
s = nil
|
22
|
+
begin
|
23
|
+
s = client.readpartial Size
|
24
|
+
rescue EOFError
|
25
|
+
break
|
26
|
+
end
|
27
|
+
break if s.nil? or s == ""
|
28
|
+
$stderr.puts "==> Read #{s}"
|
29
|
+
client.write s
|
30
|
+
$stderr.puts "==> Written #{s}"
|
31
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: WEBSocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: websocket
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.0
|
21
|
+
version: '1.0'
|
22
22
|
- - ! '>='
|
23
23
|
- !ruby/object:Gem::Version
|
24
24
|
version: 1.0.6
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 1.0
|
32
|
+
version: '1.0'
|
33
33
|
- - ! '>='
|
34
34
|
- !ruby/object:Gem::Version
|
35
35
|
version: 1.0.6
|
@@ -50,13 +50,21 @@ files:
|
|
50
50
|
- Rakefile
|
51
51
|
- WEBSocket.gemspec
|
52
52
|
- lib/WEBSocket.rb
|
53
|
+
- lib/WEBSocket/Celluloid/acceptedSocket.rb
|
53
54
|
- lib/WEBSocket/Celluloid/server.rb
|
54
55
|
- lib/WEBSocket/Celluloid/socket.rb
|
56
|
+
- lib/WEBSocket/Standard/acceptedSocket.rb
|
55
57
|
- lib/WEBSocket/Standard/server.rb
|
56
58
|
- lib/WEBSocket/Standard/socket.rb
|
59
|
+
- lib/WEBSocket/acceptedSocket.rb
|
57
60
|
- lib/WEBSocket/server.rb
|
58
61
|
- lib/WEBSocket/socket.rb
|
59
62
|
- lib/WEBSocket/version.rb
|
63
|
+
- test/Rakefile
|
64
|
+
- test/celluloid/clientTest.rb
|
65
|
+
- test/celluloid/serverTest.rb
|
66
|
+
- test/standard/clientTest.rb
|
67
|
+
- test/standard/serverTest.rb
|
60
68
|
homepage: https://github.com/Ezveus/WEBSocket
|
61
69
|
licenses: []
|
62
70
|
post_install_message:
|
@@ -81,4 +89,9 @@ rubygems_version: 1.8.24
|
|
81
89
|
signing_key:
|
82
90
|
specification_version: 3
|
83
91
|
summary: Establish a websocket communication
|
84
|
-
test_files:
|
92
|
+
test_files:
|
93
|
+
- test/Rakefile
|
94
|
+
- test/celluloid/clientTest.rb
|
95
|
+
- test/celluloid/serverTest.rb
|
96
|
+
- test/standard/clientTest.rb
|
97
|
+
- test/standard/serverTest.rb
|