Permalink
Browse files

Get joystick events passing to server

  • Loading branch information...
1 parent cf2ca11 commit f2a5446a2d01128573e0e77c8ff44c3494922fc4 @creationix committed Feb 1, 2012
Showing with 119 additions and 5 deletions.
  1. +39 −5 joyclient.lua
  2. +1 −0 joystick.lua
  3. +66 −0 jsonstream.lua
  4. +13 −0 server.lua
View
@@ -1,14 +1,48 @@
local Joystick = require('./joystick')
+local JsonStream = require('./jsonstream')
+local Json = require('json')
+local Emitter = require('emitter')
+local TCP = require('tcp')
if not process.argv[1] then
print("Please pass in joystick number as first argument")
process.exit(1)
end
+local host = process.argv[2] or "0.0.0.0"
+local port = process.argv[3] or 5000
+
+local function connect(host, port, callback)
+ local client = TCP:new()
+ client:connect(host, port)
+ client:on("error", callback)
+ client:on("connect", function ()
+ client:remove_listener("error", callback)
+ client:read_start()
+ local emitter = Emitter:new()
+ function emitter.send(message)
+ client:write(Json.stringify(message))
+ end
+ local parser = JsonStream(function (value)
+ emitter:emit("message", value)
+ end)
+ client:on('data', function (chunk)
+ parser:parse(chunk)
+ end)
+ client:on('error', function (err)
+ emitter:emit("error", err)
+ end)
+ callback(null, emitter)
+ end)
+end
+
local js = Joystick:new(process.argv[1])
-js:on('button', p);
-js:on('axis', p);
-js:on('error', function (err)
- debug("Error", err)
+js:on('opened', function ()
+ debug("Joystick opened")
+ connect(host, port, function (err, server)
+ if err then error(tostring(err)) end
+ js:on('button', server.send)
+ js:on('axis', server.send)
+ server:on('message', debug)
+ end)
end)
-
View
@@ -29,6 +29,7 @@ end
function Joystick.prototype:on_open(fd)
self.fd = fd
+ self:emit("opened")
self:start_read()
end
View
@@ -0,0 +1,66 @@
+local Yajl = require('yajl')
+local Table = require('table')
+return function (callback, options)
+ local current
+ local key
+ local stack = {}
+ local options = options or {}
+ options.allow_multiple_values = true
+ local null = options.use_null and Yajl.null
+ local function emit(value)
+ if not current then
+ callback(value)
+ else
+ current[key or #current + 1] = value
+ end
+ end
+ local parser = Yajl.new_parser({
+ on_null = function ()
+ emit(null)
+ end,
+ on_boolean = function (value)
+ emit(value)
+ end,
+ on_number = function (value)
+ emit(value)
+ end,
+ on_string = function (value)
+ emit(value)
+ end,
+ on_start_map = function ()
+ local new = {}
+ Table.insert(stack, current)
+ key = nil
+ current = new
+ end,
+ on_map_key = function (value)
+ key = value
+ end,
+ on_end_map = function ()
+ key = nil
+ local map = current
+ current = Table.remove(stack)
+ emit(map)
+ end,
+ on_start_array = function ()
+ local new = {}
+ key = nil
+ current = new
+ end,
+ on_end_array = function ()
+ local array = current
+ current = Table.remove(stack)
+ emit(array)
+ end
+ })
+ if options then
+ options.use_null = nil
+ if options then
+ for k,v in pairs(options) do
+ parser:config(k, v)
+ end
+ end
+ end
+ return parser
+end
+
View
@@ -0,0 +1,13 @@
+local Tcp = require('tcp')
+local JsonStream = require('./jsonstream')
+
+-- A server for joystick clients to connect to
+Tcp:create_server("0.0.0.0", 5000, function (client)
+ debug("client", client)
+ local parser = JsonStream(function (message)
+ debug("message from client", message)
+ end)
+ client:on('data', function (chunk)
+ parser:parse(chunk)
+ end)
+end)

0 comments on commit f2a5446

Please sign in to comment.