diff --git a/README.md b/README.md index a97c5a6..0f56858 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,10 @@ Login to meteor server via a token - `token` **string** *required* - `callback(err, result)` **method** +### logout(callback) + +Logout from meteor server + #### Warning You can only do one subscription on a same collection at one time diff --git a/index.js b/index.js index d45ff99..c4d9859 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ var queue = require('./queue'); var ddp; var subscriptions = []; +var logoutId, + logoutCb; var loginWithEmailId, loginWithEmailCb; var loginWithUsernameId, @@ -16,6 +18,10 @@ var methods = []; module.exports = { on: queue.on, + logout: function (callback) { + logoutCb = callback; + logoutId = ddp.method("logout"); + }, loginWithToken: function (token, callback) { loginWithTokenCb = callback; loginWithTokenId = ddp.method("login", [{ resume: token }]); @@ -39,6 +45,11 @@ module.exports = { }]); }, method: function (event, param, callback) { + if(callback===undefined) { + callback = param; + param = []; + } + var id = ddp.method(event, param); methods.push({ id: id, @@ -110,7 +121,15 @@ module.exports = { } }, - + disconnect: function () { + //ddp.disconnect(); + }, + reconnect: function () { + ddp = new DDP({ + endpoint: endpoint, + SocketConstructor: WebSocket + }); + }, connect: function (endpoint) { ddp = new DDP({ endpoint: endpoint, @@ -123,7 +142,17 @@ module.exports = { queue.emit('disconnected'); }); - ddp.on("result", function (message) { + ddp.on("result", (message) => { + + if (message.id === logoutId && typeof logoutCb == 'function') { + if(message.error) { + return logoutCb(message.error); + } + logoutCb(); + this.connect(endpoint); + return; + } + if (message.id === loginWithEmailId && typeof loginWithEmailCb == 'function') { if(message.error) { return loginWithEmailCb(message.error); @@ -145,6 +174,7 @@ module.exports = { loginWithTokenCb(null, message.result); return; } + //console.log('RESULT FROM METEOR METHOD'); var index; for(var i in methods) { var method = methods[i]; diff --git a/package.json b/package.json index 6e0509f..7e6d040 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-meteor", - "version": "0.3.1", + "version": "0.3.2", "description": "DDP React-native Client", "main": "index.js", "scripts": {