Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 33 lines (27 sloc) 0.755 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
require 'faye/websocket'
require 'rack'
require 'wall_e'

static = Rack::File.new(File.dirname(__FILE__))
wall_e = WallE::Assembler.create
claw = wall_e.Claw(10, 9)
claw.center

App = lambda do |env|
  if Faye::WebSocket.websocket?(env)
    ws = Faye::WebSocket.new(env, ['irc', 'xmpp'], :ping => 5)
    p [:open, ws.url, ws.version, ws.protocol]

    ws.onmessage = lambda do |event|
      servo, degrees = event.data.split(":")
      msg = "Moved the #{servo} #{degrees} degrees"
      what = servo == 'claw' ? :pinch : :tilt
      claw.send(what, degrees.to_i)
      ws.send(msg)
    end

    ws.onclose = lambda do |event|
      p [:close, event.code, event.reason]
      ws = nil
    end

    ws.rack_response

  else
    static.call(env)
  end
end
Something went wrong with that request. Please try again.