ERROR -- : undefined method `close' for #<Iodine::Http::Response:0x007fad464d6160> (NoMethodError)
ccniuj opened this issue · comments
This error message keeps showing when I use websocket api to send message....
here is the whole stack:
E, [2016-01-29T13:13:05.480107 #8539] ERROR -- : undefined method `close' for #<Iodine::Http::Response:0x007fad464d6160> (NoMethodError)
/Users/davidjuin0519/Projects/chatty/config/initializers/websocket.rb:27:in `rescue in on_message'
/Users/davidjuin0519/Projects/chatty/config/initializers/websocket.rb:23:in `on_message'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/plezi-0.12.21/lib/plezi/handlers/ws_object.rb:65:in `on_message'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/plezi-0.12.21/lib/plezi/handlers/controller_core.rb:54:in `on_message'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/iodine-0.1.21/lib/iodine/http/websockets.rb:306:in `complete_frame'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/iodine-0.1.21/lib/iodine/http/websockets.rb:271:in `extract_message'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/iodine-0.1.21/lib/iodine/http/websockets.rb:15:in `on_message'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/iodine-0.1.21/lib/iodine/protocol.rb:188:in `call'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/iodine-0.1.21/lib/iodine/core.rb:65:in `work'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/iodine-0.1.21/lib/iodine/core.rb:55:in `cycle'
/Users/davidjuin0519/.rvm/gems/ruby-2.2.3/gems/iodine-0.1.21/lib/iodine/core.rb:75:in `block (2 levels) in startup'
here is the code:
def on_message data
begin
data = JSON.parse data
rescue Exception => e
response << {event: :error, message: "Unknown Error"}.to_json
response.close
return false
end
broadcast :_send_message, {event: :chat, from: params[:id], message: data["message"], at: Time.now}.to_json
end
How can I solve this problem? Thanks!
The #close
method is a controller method, not a response object method.
Try this:
def on_message data
begin
data = JSON.parse data
rescue => e
write {event: :error, message: "Unknown Error"}.to_json
close
return false
end
broadcast :_send_message, {event: :chat, from: params[:id], message: data["message"], at: Time.now}.to_json
end
Explanation:
The response
object mainly manages the HTTP response. When the websocket Upgrade completes, it can still be used for sending data... but that was a fallback to prevent issues, not the main way for sending websocket messages.
The Controller#write
method will send messages directly to the websocket protocol layer instead of having the messages moved through the response.
The websocket protocol has a "close sequence" which includes sending a specific close packet before closing the connection - so Controller#close
is actually a controller method.
You can see some example code here, but I obviously need to write better documentation for the websocket methods and place it in the controller's documentation (I'm just focusing on re-writing the server layer in C).
Exception
will rescue errors that you really don't want to rescue, such as system errors (no memory, etc') and signals (the server's shutdown signal sent by the OS).
The default exception rescued (if non provided) is the general StandardError. You can read more about it here.
P.S.
If you're sure that you're using JSON, you can use Plezi's auto_dispatch
feature. It automatically routes JSON messages to Ruby methods according to the event
property (so an event with the name "foo" will be routed to a controller method called "foo").
Davide, Please let me know if this helps. and Good Luck! :)
I'm closing this, as the next release (coming soon) is a total rewrite, for Plezi as well as Iodine.
If the new version (v. 0.14.0, unreleased just yet) invokes a similar issue, feel free to open a new issue.
Thanks.