Commit 93989fb4f2182e5c9a2df88cc6f8dc7efb421a06
update to use latest pull-ws-server api
Dominic Tarr committed on 3/6/2015, 10:52:17 AMParent: 4062644a7f2a55d2f9039f4151d26d2d30c06bcf
Files changed
index.js | changed |
index.js | |||
---|---|---|---|
@@ -26,43 +26,49 @@ | |||
26 | 26 … | cb = addr, addr = null | |
27 | 27 … | ||
28 | 28 … | addr = address(config || addr) | |
29 | 29 … | if (wsStream) { | |
30 | - wsStream.socket.close() | ||
30 … | + wsStream.close() | ||
31 | 31 … | client._emit('reconnecting') | |
32 | 32 … | } | |
33 | 33 … | ||
34 … | + var called = false | ||
35 … | + | ||
34 | 36 … | client.addr = addr | |
35 | - wsStream = ws.connect(addr) | ||
36 | - rpcStream = client.createStream() | ||
37 | - pull(wsStream, rpcStream, wsStream) | ||
38 | 37 … | ||
39 | - var onopen_ = wsStream.socket.onopen | ||
40 | - wsStream.socket.onopen = function() { | ||
41 | - onopen_() | ||
42 | - client._emit('connect') | ||
38 … | + //if auth is not the first method called, | ||
39 … | + //then the other methods will get auth errors. | ||
40 … | + //since rpc calls are queued, we can just do it here. | ||
41 … | + client.auth(function (err, authed) { | ||
42 … | + if (err) | ||
43 … | + client._emit('error', err) | ||
44 … | + else | ||
45 … | + client._emit('authed', authed) | ||
46 … | + if(called) return | ||
47 … | + called = true; cb && cb(err, authed) | ||
48 … | + }) | ||
43 | 49 … | ||
44 | - client.auth(function (err, authed) { | ||
45 | - if (err) | ||
46 | - client._emit('error', err) | ||
47 | - else | ||
48 | - client._emit('authed', authed) | ||
49 | - cb && cb(err, authed) | ||
50 | - }) | ||
51 | - } | ||
50 … | + wsStream = ws.connect(addr, { | ||
51 … | + onOpen: function() { | ||
52 … | + client._emit('connect') | ||
53 … | + //cb is called after auth, just above | ||
54 … | + }, | ||
55 … | + onClose: function() { | ||
56 … | + client._emit('close') | ||
57 … | + //rpcStream will detect close on it's own. | ||
58 … | + if(called) return | ||
59 … | + called = true; cb && cb(err, authed) | ||
60 … | + } | ||
61 … | + }) | ||
52 | 62 … | ||
53 | - var onclose_ = wsStream.socket.onclose | ||
54 | - wsStream.socket.onclose = function() { | ||
55 | - onclose_ && onclose_() | ||
56 | - rpcStream.close(function(){}) | ||
57 | - client._emit('close') | ||
58 | - } | ||
63 … | + rpcStream = client.createStream() | ||
64 … | + pull(wsStream, rpcStream, wsStream) | ||
59 | 65 … | ||
60 | 66 … | return client | |
61 | 67 … | } | |
62 | 68 … | ||
63 | 69 … | client.close = function(cb) { | |
64 | - wsStream.socket.close() | ||
70 … | + wsStream.close() | ||
65 | 71 … | rpcStream.close(function () { | |
66 | 72 … | cb && cb() | |
67 | 73 … | }) | |
68 | 74 … | return client |
Built with git-ssb-web