README
adds a little abstraction to websocket ws-json-browser
EventEmitter like API for WebSocket, in browser-side.
Usage
This module is writtern in CommonJS protocol. You need to use Browserify to package code in browsers.
npm install ws-json-browser
client = require "ws-json-browser"
client.connect "localhost", 3000, (ws) ->
ws.emit 'greet', 'hello server', (data) ->
console.log 'server returns:', data
ws.on 'welcome', (data, res) ->
console.log 'server say:', data
res 'got it'
ws.on "repeat", (data, res) ->
setTimeout (-> res data), 2000
console.log "repeat", data
ws.emit 'repeat', 20
ws.onclose ->
console.log 'server disturbed'
API
Connect to server:
client.connect
:domain, port, (ws) ->
,domain
is optional and defaults to belocation.hostname
Communication is based on [key, value, id]
:
ws.emit
:key, value, (data) ->
,data
is what server callbacked, bothvalue
and data are optional.ws.on
:key, (value, res) ->
,res
can be used likeres value
to send data back
When close:
ws.onclose
:->
ws.closed
Since there might be lots of connections and many instances of ws
.
ws
objects may fail to be garbage collected.
To stay away from memory leak, a solution from Backbone.
ws.listenTo
:source, eventName, (value) ->
, it uses.removeListener()
internally
Development
Read make.coffee
for more.
Changelog
0.0.6
- Fix mutiple listeners on the same event name