Created
May 18, 2011 14:12
-
-
Save maccman/978644 to your computer and use it in GitHub Desktop.
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
class WebSocketRPC | |
constructor: (host) -> | |
@callbacks = {} | |
@id = 0 | |
@socket = new WebSocket(host) | |
@socket.onopen = @onopen | |
@socket.onmessage = @onmessage | |
@socket.onclose = @onclose | |
onopen: => | |
onclose: => | |
onmessage: (data) => | |
message = JSON.parse(data) | |
@callbacks[message.id]?(message.result) | |
rpc: (method, args...) => | |
id = @id += 1 | |
if typeof args[args.length - 1] == "function" | |
callback = args.pop() | |
@callbacks[id] = callback if callback | |
message = | |
id: id, | |
method: method, | |
args: args | |
@socket.send(JSON.stringify(message)) | |
# Result: | |
# => {"method": "ga", "args": ["ga1", "ga2"], "id": 1} | |
# <= {"id": 1, "result": "blah"} |
Author
maccman
commented
May 18, 2011
via email
uppercase is a constant, that's all.
On Wed, May 18, 2011 at 10:39 AM, weepy < ***@***.***>wrote:
how about @ick
!
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/978644
##
Alex MacCaw
+12147175129
@maccman
http://alexmaccaw.co.uk | http://www.leadthinking.com | http://socialmod.com
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment