Sinatra-websocket已安装在我的Ubuntu系统上,可用于使用文本字符串在浏览器中与JavaScript客户端通信。
由于它适用于文本,我想从浏览器中的JavaScript客户端传输一些二进制数据,该客户端从画布获取图像数据,并使用WebSocket将其发送到Sinatra服务器。当服务器从客户端获取数据时,它只是将其发回。 服务器发送回收的内容时发生错误。在尝试修复它一晚后,我仍然不知道如何征服它。
客户端JavaScript代码:
var imgSocket = new WebSocket("ws://" + window.location.host + "/img");
var sendImageData = function(){
var e = document.getElementById("canvas1");
var c = e.getContext('2d');
var img = c.getImageData(0,0,200,200);
var binary = new Uint8Array(img.data.length);
for (var i = 0; i < img.data.length; i++) {
binary[i] = img.data[i];
}
imgSocket.send(binary.buffer);
};
服务器端Ruby代码:
require 'sinatra'
require 'sinatra-websocket'
set :bind, "0.0.0.0"
set :img_sockets, []
get '/img' do
if !request.websocket?
"hello"
else
request.websocket do |ws|
ws.onopen do
puts "img_socket opened"
settings.img_sockets << ws
end
ws.onmessage do |msg|
EM.next_tick {settings.img_sockets.each {|s| s.send(msg)}} #error occurs here
#EM.next_tick {settings.img_sockets.each {|s| s.send("message from server")}} #if I just send back a text message, it’s OK.
end
ws.onclose do
puts "img_socket closed"
settings.img_sockets.delete(ws)
end
end
end
end
来自服务器端的错误信息:
Stopping ...
/home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/eventmachine-1.0.3/lib/em/connection.rb:266:in `close_connection': eventmachine not initialized: evma_close_connection (RuntimeError)
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/eventmachine-1.0.3/lib/em/connection.rb:266:in `close_connection'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/backends/base.rb:95:in `block in stop!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/backends/base.rb:95:in `each_value'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/backends/base.rb:95:in `stop!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/server.rb:190:in `stop!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/sinatra-1.4.4/lib/sinatra/base.rb:1406:in `quit!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/sinatra-1.4.4/lib/sinatra/base.rb:1431:in `ensure in run!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/sinatra-1.4.4/lib/sinatra/base.rb:1431:in `run!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/sinatra-1.4.4/lib/sinatra/main.rb:25:in `block in <module:Sinatra>'
Stopping ...
/home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/eventmachine-1.0.3/lib/em/connection.rb:266:in `close_connection': eventmachine not initialized: evma_close_connection (RuntimeError)
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/eventmachine-1.0.3/lib/em/connection.rb:266:in `close_connection'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/backends/base.rb:95:in `block in stop!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/backends/base.rb:95:in `each_value'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/backends/base.rb:95:in `stop!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/server.rb:190:in `stop!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/sinatra-1.4.4/lib/sinatra/base.rb:1406:in `quit!'
from /home/uncutstone/.rvm/gems/ruby-2.0.0-p353/gems/sinatra-1.4.4/lib/sinatra/base.rb:1505:in `block in setup_traps'
有人可以帮忙吗?