Created
June 17, 2013 19:23
-
-
Save glurp/5799539 to your computer and use it in GitHub Desktop.
push mouse event to any browser connected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra' | |
| require 'sinatra' | |
| require 'sinatra-websocket' | |
| set :server, 'thin' | |
| set :sockets, [] | |
| get '/' do | |
| if !request.websocket? | |
| erb :index | |
| else | |
| request.websocket do |ws| | |
| ws.onopen do | |
| puts "================ Connected!!!" | |
| #puts ws.inspect[0..100] | |
| settings.sockets << ws | |
| end | |
| ws.onmessage do |msg| | |
| EM.next_tick { settings.sockets.each{|s| | |
| #puts "send to #{s} #{msg}" | |
| s.send(msg) } } | |
| end | |
| ws.onclose do | |
| warn("wetbsocket closed") | |
| settings.sockets.delete(ws) | |
| end | |
| end | |
| end | |
| end | |
| __END__ | |
| @@ index | |
| <html> | |
| <body> | |
| <div id='point' style='position:absolute;left: 0;top:0;background: red;'>X</div> | |
| <h1>Simple Mouse push</h1> | |
| <div id="msgs"></div> | |
| </body> | |
| <script type='application/javascript'> | |
| var idInterval=null; | |
| var nbError=0; | |
| var wsocket=null; | |
| var show = function(el){ | |
| return function(msg){ el.innerHTML = msg + '<br/>' + el.innerHTML; } | |
| }(document.getElementById('msgs')); | |
| var show2 = function(el){ | |
| return function(msg){ el.innerHTML = "<h3>"+msg + '</h3>' + el.innerHTML; } ; | |
| }(document.getElementById('msgs')); | |
| window.onload = function(){ | |
| function initWS() { | |
| nbError=0; | |
| if (wsocket!=null) wsocket.close(); | |
| wsocket= new WebSocket('ws://' + window.location.host + window.location.pathname); | |
| wsocket.onmessage = function(m) { | |
| console.log(m.data); | |
| wsreceive(JSON.parse(m.data)) ; | |
| }; | |
| wsocket.onclose = function() { show2('websocket closed'); } | |
| wsocket.onopen = function() { show2("websocket opened"); } | |
| } | |
| initWS(); | |
| setInterval(function() { | |
| if (wsocket.readyState!=1) nbError++; | |
| if (nbError>3) initWS(); | |
| },2000); | |
| var lastDateSending= (new Date()).getTime(); | |
| var wssend=function(json) { | |
| var now= (new Date()).getTime(); | |
| if (wsocket && wsocket.readyState==1 && (now-lastDateSending)>0) { | |
| //console.log("sending : " + txt); | |
| json.date=now; | |
| wsocket.send(JSON.stringify(json)); | |
| lastDateSending=now; | |
| } | |
| }; | |
| wsreceive=function(o) { | |
| //console.log('received : ' + o.x + " , " + o.y); | |
| var n=document.getElementById('point'); | |
| if (n) { | |
| n.style.left=o.x+'px'; n.style.top=o.y+'px'; | |
| document.title= 'Delta =' + ((new Date()).getTime() - o.date) + ' ms'; | |
| } | |
| }; | |
| window.onmousemove=function(evt) { | |
| wssend( {x: evt.clientX, y:evt.clientY} ) ; | |
| }; | |
| } | |
| </script> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment