websocket-client 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/websocket_client/client.rb +33 -6
  2. metadata +3 -3
@@ -29,6 +29,7 @@ module WebSocketClient
29
29
  @handshake_class = handshake_class
30
30
  @socket = nil
31
31
  @on_message_handler = nil
32
+ @on_disconnect_handler = nil
32
33
  @handler_thread = nil
33
34
  @close_state = nil
34
35
  case ( uri )
@@ -80,10 +81,20 @@ module WebSocketClient
80
81
  @on_message_handler.call( msg ) if @on_message_handler
81
82
  end
82
83
  end
84
+
85
+ def on_disconnect(&block)
86
+ if ( block )
87
+ @on_disconnect_handler = block
88
+ else
89
+ puts "> on_disconnect"
90
+ @on_disconnect_handler.call( msg ) if @on_disconnect_handler
91
+ end
92
+ end
83
93
 
84
94
  def start_handler
85
95
  @handler_thread = Thread.new(@socket) do |socket|
86
96
  msg = ''
97
+ msg_state = :none
87
98
  while ( ! socket.eof? )
88
99
  c = nil
89
100
  if ( socket.respond_to?( :getbyte ) )
@@ -93,25 +104,41 @@ module WebSocketClient
93
104
  end
94
105
  puts "> #{c} #{c.class} #{c == 0xff}"
95
106
  if ( c == 0x00 )
96
- if ( @close_state == :half_closed )
107
+ if ( msg_state == :half_closed )
108
+ puts "> full-closed by server"
97
109
  socket.close
98
- break;
110
+ break
99
111
  else
100
- msg = ''
112
+ if ( @close_state == :half_closed )
113
+ socket.close
114
+ break
115
+ else
116
+ msg = ''
117
+ msg_state = :none
118
+ end
101
119
  end
102
120
  elsif ( c == 0xFF )
103
- if ( @close_state == :requested )
104
- @close_state = :half_closed
121
+ if ( msg_state == :none )
122
+ msg_state = :half_closed
123
+ puts "> half-closed by server"
105
124
  else
106
- on_message( msg )
125
+ if ( @close_state == :requested )
126
+ @close_state = :half_closed
127
+ else
128
+ on_message( msg )
129
+ msg = ''
130
+ msg_state = :none
131
+ end
107
132
  end
108
133
  else
109
134
  if ( @close_state != nil )
110
135
  @close_state = nil
111
136
  end
137
+ msg_state = :in_progress
112
138
  msg << c
113
139
  end
114
140
  end
141
+ on_disconnect
115
142
  end
116
143
  end
117
144
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - The TorqueBox Team