sinatra-websocketio 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.4 2013-03-10
2
+
3
+ * close websocket if session_id duplicate
4
+
1
5
  === 0.0.3 2013-03-09
2
6
 
3
7
  * add EM::WebSocketIO::Client
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module WebSocketIO
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -10,22 +10,30 @@ class WebSocketIO
10
10
  ws.onopen do |handshake|
11
11
  params = parse_handshake_params handshake.path
12
12
  session_id = params[:session] || (create_session Socket.unpack_sockaddr_in ws.get_peername)
13
- self.sessions[session_id] = {
14
- :websocket => ws
15
- }
16
- ws.onclose do
17
- self.emit :disconnect, session_id
18
- end
19
- ws.onmessage do |msg|
20
- begin
21
- data = JSON.parse msg
22
- rescue => e
23
- self.emit :error, e
13
+
14
+ if self.sessions.include? session_id
15
+ ws.send({:type => :error, :data => "invalid session_id (#{session_id})"}.to_json)
16
+ ws.close
17
+ else
18
+ self.sessions[session_id] = {
19
+ :websocket => ws
20
+ }
21
+ ws.onclose do
22
+ self.sessions.delete session_id
23
+ self.emit :disconnect, session_id
24
24
  end
25
- self.emit data['type'], data['data'], session_id if data
25
+ ws.onmessage do |msg|
26
+ begin
27
+ data = JSON.parse msg
28
+ rescue => e
29
+ self.emit :error, e
30
+ end
31
+ self.emit data['type'], data['data'], session_id if data
32
+ end
33
+ ws.send({:type => :__session_id, :data => session_id}.to_json)
34
+ self.emit :connect, session_id
26
35
  end
27
- ws.send({:type => :__session_id, :data => session_id}.to_json)
28
- self.emit :connect, session_id
36
+
29
37
  end
30
38
  end
31
39
  end
data/sample/main.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  WebSocketIO.on :connect do |session|
2
2
  puts "new client <#{session}>"
3
- WebSocketIO.push :chat, {:name => "system", :message => "new client <#{session}>"}
3
+ WebSocketIO.push :chat, {:name => "system", :message => "new client <#{session}> / #{WebSocketIO.sessions.size} clients connecting"}
4
4
  WebSocketIO.push :chat, {:name => "system", :message => "welcome <#{session}>"}, {:to => session}
5
5
  end
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-websocketio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: