Created
October 18, 2011 01:03
-
-
Save josephg/1294365 to your computer and use it in GitHub Desktop.
Websocket server API
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
| nextid = 0 | |
| # websocketserver takes a path to bind to, optional options object and a callback. | |
| wsserver = websocketserver '/ws', (client) -> | |
| # The callback is called when a client connects, with a handler for the client object. | |
| # The user is responsible for adding ids to the client object if they need them. | |
| client.id = nextid++ | |
| console.log "client #{client.id} connected with websocket version #{client.protocolVersion}" | |
| # HTTP Headers should also be accessible through client.headers. This lets you fetch cookies and stuff, if | |
| # thats still part of the spec and the browser supports it. | |
| console.log "Client headers:", client.headers | |
| client.on 'message', (message) -> | |
| console.log "got a message #{message} from client #{client.id}" | |
| # Copying websocket parlance, messages are just strings. The user is responsible for | |
| # JSON parsing them. | |
| # The message object is a Buffer object if websockets specified binary framing, otherwise it is a string. | |
| client.on 'close', (code, reason) -> | |
| # Called when the client times out or disconnects. This is guaranteed to be called exactly | |
| # once for every client. After close() has been received, no further messages will be received | |
| # from the client. | |
| console.log "client #{client.id} went away. Reason: #{reason}" | |
| client.ping (delay) -> | |
| # Callback called when we get a pong response. | |
| console.log "Got pong after #{delay} ms" | |
| # Send a message | |
| client.send 'hi' | |
| # It should also be possible to send binary data through Buffers. | |
| client.send new Buffer(...) | |
| # Invalid - you're responsible for JSON.stringify'ing your messages. | |
| # client.send {x:6} | |
| # Closes the session. The server 'close' event will be fired immediately. | |
| client.close() | |
| # Close can be supplied a websocket status code and a reason. Calling close with | |
| # no arguments is the equivalent of calling close(1000). | |
| client.close 1001, 'Server shutdown' | |
| # The returned object is connect middleware, to make it easy to use in connect and express: | |
| connect(wsserver).listen(5678) | |
| # Connect middleware is just a function which takes (req, res, next). So you can use it directly: | |
| s = http.createServer (req, res) -> | |
| wsserver req, res, -> | |
| # The websocket server didn't handle the request. 404 or something. | |
| res.writeHead 404 | |
| res.end '<html><body>404 Not found</body></html>' | |
| s.listen(1337) | |
| # Maybe we give it a listen() method as well to make it easy to get started: | |
| wsserver.listen(4321) |
Author
Oh? I'm confused. I want a library I can use in node-browserchannel. Surely the protocol API is already defined in the 17000 websocket spec documents?
Or is the difference that I can use a similar API in both a websocket server & client?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw, the "committee" is for the protocol API, not the Server API, there's a big difference between the two.