Sending large file in websocket before timeout

我怕爱的太早我们不能终老 提交于 2019-12-12 02:27:14

问题


I'm using Faye and EventMachine to open a socket to another server. The server times out if it doesn't detect activity. How do I send the file (binary encoded) so the server doesn't time out?

Here's what I have:

media_path = "/path/to/media/file"

EM.run {
  ws = Faye::WebSocket::Client.new(uri)

  ws.on :open do |event|
    puts "Opening socket"
    ws.send(File.read(media_path))
  end

  ws.on :message do |event|
    puts "Recieving message"
  end

  ws.on :close do |event|
    ws = nil
    EM.stop
  end
}

回答1:


You need to send your file in batches. Something like that:

ws.on :open do |event|
  puts "Opening socket"
  File.open('filename','r') do |f|
    ws.send(f.gets)
  end
end

You solve another problem this way: you wouldn't place your whole file in RAM + you'll send it faster. You can read about nodejs streams, there is a similar principle of operation.




回答2:


This is slightly of a duplicate for How to send binary file over Web Sockets with Rails Although this question did come first...

Please see my answer to that question for another option that won't block your application while you send your file.



来源:https://stackoverflow.com/questions/32976055/sending-large-file-in-websocket-before-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!