Sinatra-websocket is installed on my Ubuntu system and can be used to communicate with a JavaScript client in a browser using a text string.
Since it works for text, I want to transfer some binary data from a JavaScript client in a browser that receives image data from the canvas and sends it to the Sinatra server using WebSocket. When the server receives data from the client, it simply sends it back. An error has occurred when the server sends what it receives. After trying to fix it for one night, I still don't know how to defeat her.
JavaScript client code:
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 Server Code:
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)}}
end
ws.onclose do
puts "img_socket closed"
settings.img_sockets.delete(ws)
end
end
end
end
Server side error information:
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'
Can anyone help?
source
share