Skip to content

Commit 33ced09

Browse files
committed
Prevent the server from crashing when receiving uploaded files.
Currently uploaded files will just be treated as invalid events. On the next release I would like to add support for uploading files through the WebSocket connection utilizing the client dispatcher.
1 parent 0c4d87d commit 33ced09

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/websocket_rails/event.rb

+19-4
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,32 @@ def new_on_invalid_event_received(connection,data=nil)
5858
# :channel =>
5959
# The name of the channel that this event is destined for.
6060
class Event
61+
62+
class UnknownDataType < StandardError; end;
63+
6164
extend Logging
6265

6366
def self.log_header
6467
"Event"
6568
end
6669

6770
def self.new_from_json(encoded_data, connection)
68-
event_name, data = JSON.parse encoded_data
69-
data = data.merge(:connection => connection).with_indifferent_access
70-
Event.new event_name, data
71-
rescue JSON::ParserError => ex
71+
case encoded_data
72+
when String
73+
event_name, data = JSON.parse encoded_data
74+
data = data.merge(:connection => connection).with_indifferent_access
75+
Event.new event_name, data
76+
# when Array
77+
# TODO: Handle file
78+
#File.open("/tmp/test#{rand(100)}.jpg", "wb") do |file|
79+
# encoded_data.each do |byte|
80+
# file << byte.chr
81+
# end
82+
#end
83+
else
84+
raise UnknownDataType
85+
end
86+
rescue JSON::ParserError, UnknownDataType => ex
7287
warn "Invalid Event Received: #{ex}"
7388
debug "Event Data: #{encoded_data}"
7489
log_exception(ex)

0 commit comments

Comments
 (0)