Permalink
Browse files

json-rpc compliant server

  • Loading branch information...
1 parent 3c9abf4 commit 8548f84e669af9d39d30f6879cf4d2a04203ce66 @contra contra committed Apr 14, 2012
Showing with 81 additions and 79 deletions.
  1. +17 −16 client/vein.coffee
  2. +27 −26 examples/chat/vein.js
  3. +9 −10 lib/Vein.coffee
  4. +27 −26 vein.js
  5. +1 −1 vein.min.js
View
@@ -32,7 +32,7 @@ class Vein
@options.sessionName ?= "VEINSESSID-#{@options.prefix}"
@socket = new SockJS "#{@options.host}/#{@options.prefix}", null, @options
- @callbacks['services'] = @handleServices
+ @callbacks['methods'] = @handleMethods
@callbacks['session'] = @setSession
@socket.onmessage = @handleMessage
@socket.onclose = @handleClose
@@ -57,32 +57,33 @@ class Vein
handleClose: => @callbacks['close']?()
handleMessage: (e) =>
- {id, service, args} = JSON.parse e.data
- if @subscribe[service] and @subscribe[service].listeners
- fn args... for fn in @subscribe[service].listeners
+ {id, method, params, err} = JSON.parse e.data
+ params = [params] unless Array.isArray params
+ if @subscribe[method] and @subscribe[method].listeners
+ fn params... for fn in @subscribe[method].listeners
return unless @callbacks[id]
- keep = @callbacks[id] args...
+ keep = @callbacks[id] params...
delete @callbacks[id] unless keep is true
return
- handleServices: (services...) =>
- @[service] = @getSender service for service in services
- @subscribe[service] = @getListener service for service in services
- @callbacks['ready']? services
+ handleMethods: (methods...) =>
+ @[method] = @getSender method for method in methods
+ @subscribe[method] = @getListener method for method in methods
+ @callbacks['ready']? methods
delete @callbacks['ready']
return
# Utilities
- getListener: (service) => (cb) =>
- @subscribe[service].listeners ?= []
- @subscribe[service].listeners.push cb
+ getListener: (method) => (cb) =>
+ @subscribe[method].listeners ?= []
+ @subscribe[method].listeners.push cb
return
- getSender: (service) =>
- (args..., cb) =>
+ getSender: (method) =>
+ (params..., cb) =>
id = @getId()
@callbacks[id] = cb
- @socket.send JSON.stringify id: id, service: service, args: args, session: @getSession()
+ @socket.send JSON.stringify id: id, method: method, params: params, session: @getSession()
return
getId: ->
@@ -92,4 +93,4 @@ class Vein
if typeof define is 'function'
define -> Vein
else
- window.Vein = Vein
+ window.Vein = Vein
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -10,7 +10,7 @@ class Vein extends EventEmitter
@server = sockjs.createServer @opts
@server.on 'connection', (socket) =>
- socket.write JSON.stringify id: 'services', args: Object.keys @routes # send down list of services
+ socket.write JSON.stringify id: 'methods', params: Object.keys @routes # send down list of methods
socket.on 'data', (msg) => @route socket, msg
unless @opts.noTrack
@clients[socket.id] = socket
@@ -29,19 +29,18 @@ class Vein extends EventEmitter
route: (socket, msg) ->
return unless typeof msg is 'string' and socket?
try
- {id, service, args, session} = JSON.parse msg
+ {id, method, params, session} = JSON.parse msg
catch err
return
- return unless service? and args? and id? and @routes[service]?
-
- write = (sock, args...) -> sock.write JSON.stringify id: id, service: service, args: args
- send = (args...) -> write socket, args...
+ return unless method? and params? and id? and @routes[method]?
+ write = (sock, params...) -> sock.write JSON.stringify id: id, method: method, params: params, error: null
+ send = (params...) -> write socket, params...
unless @opts.noTrack
- send.all = (args...) => write sock, args... for id, sock of @clients
+ send.all = (params...) => write sock, params... for id, sock of @clients
send.session = (sess) =>
socket.session = sess
- socket.write JSON.stringify id: 'session', args: [sess]
+ socket.write JSON.stringify id: 'session', params: [sess]
socket.session = session
- @routes[service] send, socket, args...
+ @routes[method] send, socket, params...
-module.exports = Vein
+module.exports = Vein
View
53 vein.js

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 8548f84

Please sign in to comment.