Skip to content

Commit

Permalink
Merge pull request #9 from plaidev/feature/8
Browse files Browse the repository at this point in the history
sub_name_spaceを簡素化、reconnect時の処理を追加
  • Loading branch information
otolab authored Aug 16, 2016
2 parents 052f4ec + c246216 commit 2358771
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 147 deletions.
43 changes: 33 additions & 10 deletions lib/client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 46 additions & 53 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,48 @@
(function() {
var DEFAULT_NAME_SPACE, DEFAULT_SUB_NAME_SPACE, Server,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
var DEFAULT_NAME_SPACE, Server;

DEFAULT_NAME_SPACE = '__';

DEFAULT_SUB_NAME_SPACE = '__';

Server = (function() {
function Server(io, methods, options) {
var connection, join_request, name_space;
var connection, join, name_space;
this.io = io;
if (methods == null) {
methods = {};
}
if (options == null) {
options = {};
}
name_space = options.name_space, connection = options.connection, join_request = options.join_request;
if (DEFAULT_SUB_NAME_SPACE in methods) {
this.methods = methods;
} else {
this.methods = {};
this.methods[DEFAULT_SUB_NAME_SPACE] = methods || {};
}
name_space = options.name_space, connection = options.connection, join = options.join;
this.methods = {};
this.name_space = name_space || DEFAULT_NAME_SPACE;
if (options.sub_name_space) {
this.name_space += '/' + options.sub_name_space;
}
this.connection = connection || function(socket, cb) {
return cb(null);
};
this.join_request = join_request || function(socket, sub_name_space, cb) {
this.join = join || function(socket, room, cb) {
return cb(null);
};
this.init();
}

Server.prototype.set = function(method_name, method, sub_name_space) {
var _base;
if (sub_name_space == null) {
sub_name_space = DEFAULT_SUB_NAME_SPACE;
}
if ((_base = this.methods)[sub_name_space] == null) {
_base[sub_name_space] = {};
}
return this.methods[sub_name_space][method_name] = method;
Server.prototype.set = function(method_name, method) {
return this.methods[method_name] = method;
};

Server.prototype.get = function(method_name, sub_name_space) {
var _ref;
if (sub_name_space == null) {
sub_name_space = DEFAULT_SUB_NAME_SPACE;
}
return (_ref = this.methods[sub_name_space]) != null ? _ref[method_name] : void 0;
Server.prototype.get = function(method_name) {
return this.methods[method_name];
};

Server.prototype.join = function(socket, sub_name_space) {
socket.join(sub_name_space);
return socket.on(sub_name_space + '_apply', (function(_this) {
Server.prototype._listen = function(socket) {
socket.on('apply', (function(_this) {
return function(req, ack_cb) {
var args, cb, e, method, _ref, _ref1;
var args, cb, e, method, _ref;
method = req.method;
args = req.args || [];
if (((_ref = _this.methods[sub_name_space]) != null ? (_ref1 = _ref[method]) != null ? _ref1.apply : void 0 : void 0) == null) {
if (((_ref = _this.methods[method]) != null ? _ref.apply : void 0) == null) {
return ack_cb({
message: 'cant find method.'
});
Expand All @@ -70,7 +53,7 @@
args.push(cb);
args.push(socket);
try {
return _this.methods[sub_name_space][method].apply(_this.methods[sub_name_space], args);
return _this.methods[method].apply(_this.methods, args);
} catch (_error) {
e = _error;
console.log('cant apply args', e.stack);
Expand All @@ -81,35 +64,45 @@
}
};
})(this));
return socket.on('join', (function(_this) {
return function(req, ack_cb) {
var room;
room = req.room;
if (!room) {
return;
}
return _this.join(socket, room, function(err) {
if (err) {
console.log('join failed', err);
return ack_cb({
message: err.message,
name: err.name
});
}
return socket.join(room, function(err) {
if (err) {
console.log('socket.io join failed', err);
return ack_cb({
message: err.message,
name: err.name
});
}
return ack_cb.apply(_this, arguments);
});
});
};
})(this));
};

Server.prototype.init = function() {
this.channel = this.io.of('/' + this.name_space);
return this.channel.on('connection', (function(_this) {
return function(socket) {
var joined;
joined = [];
return _this.connection(socket, function(err) {
socket.on('join', function(req) {
var _ref;
if ((req != null ? req.sub_name_space : void 0) == null) {
return;
}
if (_ref = req.sub_name_space, __indexOf.call(joined, _ref) >= 0) {
return;
}
return _this.join_request(socket, req.sub_name_space, function(err) {
if (err) {
console.log('sub namespace join failed', err);
return;
}
joined.push(req.sub_name_space);
return _this.join(socket, req.sub_name_space);
});
});
if (err) {
return console.log('connection error', err);
}
return _this._listen(socket);
});
};
})(this));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minimum-rpc",
"version": "1.1.0",
"version": "1.2.0",
"description": "a minimum rpc module.",
"main": "index.js",
"scripts": {
Expand Down
26 changes: 17 additions & 9 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class Client

@name_space = options.name_space || '__'

@sub_name_space = options.sub_name_space || '__'

@_socket = io_or_socket

@_joined = []
Expand All @@ -20,17 +18,27 @@ class Client
uri = @url
if not (uri of _managers)
_managers[uri] = io_or_socket.Manager(uri, options.connect_options)
@_socket = _managers[uri].socket(path)

@_manager = _managers[uri]

@_socket = @_manager.socket(path)

@_socket.on 'reconnect', =>
joined = @_joined
@_joined = []
for room in joined
@join room

else if (io_or_socket.constructor.name isnt 'Socket')
@_socket = io_or_socket.connect @url + '/' + @name_space, options.connect_options || {}

@join @sub_name_space
join: (room, cb=()->) ->
return cb?() if room in @_joined

join: (sub_name_space) ->
if not (sub_name_space in @_joined)
@_socket.emit 'join', {sub_name_space}
@_joined.push sub_name_space
@_socket.emit 'join', {room}, (err) =>
return cb?(err) if err
@_joined.push room
cb?(err)

send: () ->

Expand All @@ -57,6 +65,6 @@ class Client
args: args
}

@_socket.emit @sub_name_space + '_apply', req, ack_cb
@_socket.emit 'apply', req, ack_cb

module.exports = Client
Loading

0 comments on commit 2358771

Please sign in to comment.