websocket-td 0.0.2 → 0.0.3

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +24 -0
  3. data/VERSION +1 -1
  4. data/lib/websocket_td.rb +56 -24
  5. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 549ad5df2619ccc64b9be924d85d1bb34e7d5601
4
- data.tar.gz: 8e3a9c62a78aab4095f71e8fa26c0cf922127ee3
3
+ metadata.gz: 8804a66bcfbd10f025de18c6c29b6cc44ce304f3
4
+ data.tar.gz: a0c8d9d49c56ddcf17361638bf80b25f2c06eedc
5
5
  SHA512:
6
- metadata.gz: d64310c37daa3d8783c82249d76b5933e5d31c191c9b2fb837f0d49edd59a684dd5e87abe8abc0281d23459cb203f1299ae258addace1c8df3775b7dc0d00dbb
7
- data.tar.gz: b17d9279fa42be544bad826b647c49ac0dd269db0379806aa53b32e42decb045b417ca9c7fcb434998951af81540e3fb1cf7834cc6f4bc6eb5c0df479e4a7d97
6
+ metadata.gz: 904fed1fbf29f9445ab06fe1d65d571db86336e2994b7405f1c23a3bab66efe1b20383bd6924857141119eb8e42934b95d885fca86132e5d3ab812b56ac34b93
7
+ data.tar.gz: 51a140921ec9f4be73144983d361a345990151eb31e7e32ebf701d81fa699c4e0d2786ba54ba4b768dfba68bb360505922da035a3181c9bfdee5feddd0d88567
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ websocket-td (0.0.2)
5
+ websocket (~> 1.1.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ json (1.8.0)
11
+ rdoc (4.0.1)
12
+ json (~> 1.4)
13
+ shoulda (2.11.3)
14
+ test-unit (2.5.5)
15
+ websocket (1.1.1)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ rdoc (> 0)
22
+ shoulda (~> 2.11.3)
23
+ test-unit (>= 2.5.5)
24
+ websocket-td!
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/lib/websocket_td.rb CHANGED
@@ -4,6 +4,7 @@ require '../lib/errors'
4
4
 
5
5
  module WebsocketTD
6
6
  class Websocket
7
+ IS_WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
7
8
  # max length bytes to try to read from a socket per attempt
8
9
  @read_buffer = 0
9
10
  #true when reading data from a socket
@@ -11,6 +12,7 @@ module WebsocketTD
11
12
  #the tread currently being used to read data
12
13
  @read_thread = nil
13
14
  @auto_pong = true
15
+
14
16
  ##
15
17
  # +host+:: Host of request. Required if no :url param was provided.
16
18
  # +path+:: Path of request. Should start with '/'. Default: '/'
@@ -48,7 +50,51 @@ module WebsocketTD
48
50
  else
49
51
  @socket = tcp_socket
50
52
  end
53
+ perform_handshake
54
+ end
55
+
56
+ attr_reader :read_thread, :read_buffer, :socket, :active, :auto_pong
57
+ attr_writer :read_buffer, :auto_pong, :on_ping, :on_error, :on_message # :on_open, :on_close
58
+
59
+ ##
60
+ #Send the data given by the data param
61
+ #if running on a posix system this uses Ruby's fork method to send
62
+ #if on windows fork won't be attempted.
63
+ #+data+:: the data to send
64
+ #+type+:: :text or :binary, defaults to :text
65
+ def send(data, type = :text)
66
+ if IS_WINDOWS
67
+ do_send(data, type) #fork not supported on windows
68
+ else
69
+ fork do
70
+ do_send(data, type)
71
+ end
72
+ end
73
+ end
74
+
75
+ ##
76
+ #sets a Proc to be executed when the connection is opened and ready for writing
77
+ #+p+:: the Proc to execute
78
+ def on_open=(p)
79
+ @on_open = p
80
+ if @opened
81
+ fire_on_open
82
+ end
83
+ end
84
+
85
+ ##
86
+ #sets a Proc to be executed when the connection is closed and ready for writing
87
+ #+p+:: the Proc to execute
88
+ def on_close=(p)
89
+ @on_close = p
90
+ if @closed
91
+ fire_on_close
92
+ end
93
+ end
51
94
 
95
+ #protected methods after this
96
+ protected
97
+ def perform_handshake
52
98
  @socket.write @handshake.to_s
53
99
  buf = ''
54
100
  headers = ''
@@ -89,24 +135,6 @@ module WebsocketTD
89
135
  # ignored
90
136
  end
91
137
  end
92
- puts headers
93
- end
94
-
95
- attr_reader :read_thread, :read_buffer, :socket, :active, :auto_pong
96
- attr_writer :read_buffer, :auto_pong, :on_ping, :on_error, :on_message # :on_open, :on_close
97
-
98
- def on_open=(p)
99
- @on_open = p
100
- if @opened
101
- fire_on_open
102
- end
103
- end
104
-
105
- def on_close=(p)
106
- @on_close = p
107
- if @closed
108
- fire_on_close
109
- end
110
138
  end
111
139
 
112
140
  #Use one thread to perform blocking read on the socket
@@ -125,10 +153,15 @@ module WebsocketTD
125
153
  @active = false
126
154
  fire_on_close
127
155
  else
128
- frame << @socket.readpartial(@read_buffer)
129
- if (message = frame.next) != nil
130
- #"text", "binary", "ping", "pong" and "close" (according to websocket/base.rb)
131
- determine_message_type(message)
156
+ begin
157
+ frame << @socket.readpartial(@read_buffer)
158
+ if (message = frame.next) != nil
159
+ #"text", "binary", "ping", "pong" and "close" (according to websocket/base.rb)
160
+ determine_message_type(message)
161
+ end
162
+ rescue EOFError => eof
163
+ fire_on_error(eof)
164
+ fire_on_close
132
165
  end
133
166
  end
134
167
  end
@@ -151,8 +184,7 @@ module WebsocketTD
151
184
  end
152
185
  end
153
186
 
154
-
155
- def send(data, type = :text)
187
+ def do_send(data, type=:text)
156
188
  frame = WebSocket::Frame::Outgoing::Client.new(:version => @handshake.version, :data => data, :type => type)
157
189
  begin
158
190
  @socket.write frame #_nonblock
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket-td
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Courtney Robinson
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - .gitignore
77
77
  - Gemfile
78
+ - Gemfile.lock
78
79
  - LICENSE
79
80
  - README.md
80
81
  - VERSION