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 +4 -0
- data/lib/sinatra-websocketio/version.rb +1 -1
- data/lib/sinatra-websocketio/websocketio.rb +22 -14
- data/sample/main.rb +1 -1
- metadata +1 -1
data/History.txt
CHANGED
@@ -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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
self.emit :
|
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
|
-
|
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
|
-
|
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
|
|