websocket-rails如何关闭套接字连接?

时间:2015-02-10 18:00:09

标签: ruby-on-rails websocket

我从ActionController:Live切换到websocket-rails,我只是想知道一旦用户关闭浏览器窗口后如何关闭服务器端的连接?

使用ActionController:Live我曾经拥有:

def stream
    response.headers['Content-Type'] = 'text/event-stream'
    @redis_sub = RedisStream.new_redis_client



    # Subscribing to user's stream by session token
    @redis_sub.subscribe([ token ]) do |on|
        on.message do |channel, msg|
            ## Did stuff
            response.stream.write(msg)
        end
    end

 rescue IOError
    "\n\nIOError in controller"
 rescue ClientDisconnected
    puts "\n\nClient has disconnected\n\n"
 ensure
    @redis_sub.quit
    response.stream.close
 end

这很好用,现在我尝试做同样的事情,但是使用websockets,我想知道如何关闭连接并退出我的redis订阅。

2 个答案:

答案 0 :(得分:0)

来自websocket控制器的

connection.close!

https://github.com/websocket-rails/websocket-rails/issues/219

答案 1 :(得分:0)

要深入了解@TheNastyOne的答案,你可以在客户端关闭来自dispatcher的连接,如下所示:

// Open connection.
var dispatcher = new WebSocketRails('localhost:3000/websocket'); 

// Close connection.
dispatcher.disconnect();

Here存在描述这两种方法的问题。