net-ws 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/net/ws/version.rb +2 -1
- data/lib/net/ws.rb +13 -10
- data/test/support/echo_server.py +28 -0
- data/test/test_helper.rb +2 -1
- data/test/unit/ws_spec.rb +40 -0
- metadata +6 -4
data/lib/net/ws/version.rb
CHANGED
data/lib/net/ws.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
1
2
|
require "net/http"
|
2
3
|
require "uri"
|
3
4
|
require "base64"
|
@@ -145,7 +146,9 @@ module Net
|
|
145
146
|
end
|
146
147
|
|
147
148
|
def extract_unmasked_payload(length)
|
148
|
-
|
149
|
+
# Use force_encoding here, as there's no way to know what's coming in
|
150
|
+
# from the socket, and ruby will assume ASCII-8BIT.
|
151
|
+
@socket.read(length).force_encoding("UTF-8")
|
149
152
|
end
|
150
153
|
|
151
154
|
# Control frames are identified by opcodes where the most significant bit
|
@@ -211,16 +214,16 @@ module Net
|
|
211
214
|
|
212
215
|
if payload.nil?
|
213
216
|
@socket.write [mask_flag].pack("C")
|
214
|
-
elsif payload.
|
215
|
-
@socket.write [mask_flag + payload.
|
216
|
-
elsif payload.
|
217
|
+
elsif payload.bytes.count < LENGTH_IS_16BIT
|
218
|
+
@socket.write [mask_flag + payload.bytes.count].pack("C")
|
219
|
+
elsif payload.bytes.count <= UNSIGNED_16BIT_MAX
|
217
220
|
@socket.write [mask_flag + 126].pack("C")
|
218
|
-
@socket.write [payload.
|
219
|
-
elsif payload.
|
220
|
-
@socket.write [mask_flag + 127, payload.
|
221
|
-
@socket.write [payload.
|
221
|
+
@socket.write [payload.bytes.count].pack("n*")
|
222
|
+
elsif payload.bytes.count <= UNSIGNED_64BIT_MAX
|
223
|
+
@socket.write [mask_flag + 127, payload.bytes.count].pack("C")
|
224
|
+
@socket.write [payload.bytes.count].pack("n*")
|
222
225
|
else
|
223
|
-
raise Error, "Unhandled payload size: #{payload.
|
226
|
+
raise Error, "Unhandled payload size: #{payload.bytes.count.inspect}"
|
224
227
|
end
|
225
228
|
end
|
226
229
|
|
@@ -234,7 +237,7 @@ module Net
|
|
234
237
|
return unless payload
|
235
238
|
|
236
239
|
# FIXME we only support text for now
|
237
|
-
@socket.write(mask(payload.unpack("
|
240
|
+
@socket.write(mask(payload.unpack("C*"), masking_key).pack("C*"))
|
238
241
|
end
|
239
242
|
|
240
243
|
def mask(payload, key)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/env python2.7 -u -Wall
|
2
|
+
|
3
|
+
import sys
|
4
|
+
from twisted.internet import reactor
|
5
|
+
from twisted.python import log
|
6
|
+
from autobahn.websocket import WebSocketServerFactory, \
|
7
|
+
WebSocketServerProtocol, \
|
8
|
+
listenWS
|
9
|
+
|
10
|
+
|
11
|
+
class EchoServerProtocol(WebSocketServerProtocol):
|
12
|
+
|
13
|
+
def onMessage(self, msg, binary):
|
14
|
+
self.sendMessage(msg, binary)
|
15
|
+
|
16
|
+
def onClose(self, *args):
|
17
|
+
reactor.stop()
|
18
|
+
|
19
|
+
|
20
|
+
if __name__ == '__main__':
|
21
|
+
factory = WebSocketServerFactory("ws://localhost:9001", debug = True, debugCodePaths = True)
|
22
|
+
log.startLogging(sys.stdout)
|
23
|
+
factory.protocol = EchoServerProtocol
|
24
|
+
listenWS(factory)
|
25
|
+
print "Here we go"
|
26
|
+
sys.stdout.flush() # flush the line so that tests know we're up
|
27
|
+
sys.stderr.flush()
|
28
|
+
reactor.run()
|
data/test/test_helper.rb
CHANGED
data/test/unit/ws_spec.rb
CHANGED
@@ -1,9 +1,49 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
1
2
|
require File.expand_path("../test_helper", File.dirname(__FILE__))
|
3
|
+
require "open3"
|
4
|
+
|
2
5
|
|
3
6
|
describe Net::WS do
|
4
7
|
|
8
|
+
def with_echo_server(&block)
|
9
|
+
echo_server_path = File.expand_path("../support/echo_server.py",
|
10
|
+
File.dirname(__FILE__))
|
11
|
+
|
12
|
+
Open3.popen3(echo_server_path) do |stdin, stdout, stderr|
|
13
|
+
stdout.readline # the readline tells us the server is up
|
14
|
+
yield "localhost", 9001
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
5
18
|
it "should be sane" do
|
6
19
|
true.must_equal true
|
7
20
|
end
|
8
21
|
|
22
|
+
it "can send and receive message" do
|
23
|
+
msg = "foo"
|
24
|
+
|
25
|
+
with_echo_server do |host, port|
|
26
|
+
@ws = Net::WS.new("ws://#{host}:#{port}")
|
27
|
+
|
28
|
+
@ws.open("/")
|
29
|
+
@ws.send_text(msg)
|
30
|
+
@ws.receive_message.must_equal(msg)
|
31
|
+
@ws.close
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "can send and receive a UTF-8 message" do
|
36
|
+
msg = "∆AIMON"
|
37
|
+
|
38
|
+
host = "localhost"; port = 9001
|
39
|
+
with_echo_server do |host, port|
|
40
|
+
@ws = Net::WS.new("ws://#{host}:#{port}")
|
41
|
+
|
42
|
+
@ws.open("/")
|
43
|
+
@ws.send_text(msg)
|
44
|
+
@ws.receive_message.must_equal(msg)
|
45
|
+
@ws.close
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
9
49
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-ws
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
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:
|
12
|
+
date: 2013-01-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/net/ws.rb
|
108
108
|
- lib/net/ws/version.rb
|
109
109
|
- net-ws.gemspec
|
110
|
+
- test/support/echo_server.py
|
110
111
|
- test/test_helper.rb
|
111
112
|
- test/unit/ws_spec.rb
|
112
113
|
homepage: ''
|
@@ -123,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
124
|
version: '0'
|
124
125
|
segments:
|
125
126
|
- 0
|
126
|
-
hash:
|
127
|
+
hash: -2090776782556819373
|
127
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
129
|
none: false
|
129
130
|
requirements:
|
@@ -132,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
133
|
version: '0'
|
133
134
|
segments:
|
134
135
|
- 0
|
135
|
-
hash:
|
136
|
+
hash: -2090776782556819373
|
136
137
|
requirements: []
|
137
138
|
rubyforge_project:
|
138
139
|
rubygems_version: 1.8.23
|
@@ -140,5 +141,6 @@ signing_key:
|
|
140
141
|
specification_version: 3
|
141
142
|
summary: A websocket client built on top of Net::HTTP.
|
142
143
|
test_files:
|
144
|
+
- test/support/echo_server.py
|
143
145
|
- test/test_helper.rb
|
144
146
|
- test/unit/ws_spec.rb
|