Skip to content

Commit cb6a199

Browse files
committed
add namespace of
1 parent 68fc0da commit cb6a199

File tree

10 files changed

+309
-86
lines changed

10 files changed

+309
-86
lines changed

.idea/jsLibraryMappings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/socketio_with_pm2_node_modules.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/socketio-with-pm2.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 190 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

master.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1616

1717
require('colors');
1818
var Pm2Socket = require('./socket');
19+
var Pm2Namespace = require('./namespace');
1920
module.exports = function () {
2021
(0, _createClass3.default)(Pm2SocketIO, null, [{
2122
key: 'getIp',
@@ -79,7 +80,6 @@ module.exports = function () {
7980
for (var i = 0; i < (process.env.instances || 1); i++) {
8081
processList.push(i);
8182
}
82-
8383
this.addresses.map(function (ip) {
8484
processList.map(function (id) {
8585
setTimeout(function () {
@@ -93,15 +93,15 @@ module.exports = function () {
9393
} catch (e) {
9494
console.error(e);
9595
}
96-
connect.on('@toServer', function (event) {
97-
for (var _len = arguments.length, data = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
98-
data[_key - 1] = arguments[_key];
96+
connect.on('@toServer', function (event, namespace) {
97+
for (var _len = arguments.length, data = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
98+
data[_key - 2] = arguments[_key];
9999
}
100100

101101
if (_this.instanceId != id || ip != _this.localIp) {
102-
var _io$sockets;
102+
var _io$of$sockets;
103103

104-
(_io$sockets = _this.io.sockets).emit.apply(_io$sockets, [event].concat(data));
104+
(_io$of$sockets = _this.io.of(namespace || '/').sockets).emit.apply(_io$of$sockets, [event].concat(data));
105105
}
106106
});
107107
}, 0);
@@ -126,14 +126,19 @@ module.exports = function () {
126126
}, {
127127
key: 'emit',
128128
value: function emit(event) {
129-
var _io$sockets2, _io$sockets3;
129+
var _io$sockets, _io$sockets2;
130130

131131
for (var _len2 = arguments.length, data = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
132132
data[_key2 - 1] = arguments[_key2];
133133
}
134134

135-
(_io$sockets2 = this.io.sockets).emit.apply(_io$sockets2, ['@toServer', event].concat(data));
136-
(_io$sockets3 = this.io.sockets).emit.apply(_io$sockets3, [event].concat(data));
135+
(_io$sockets = this.io.sockets).emit.apply(_io$sockets, ['@toServer', event, '/'].concat(data));
136+
(_io$sockets2 = this.io.sockets).emit.apply(_io$sockets2, [event].concat(data));
137+
}
138+
}, {
139+
key: 'of',
140+
value: function of(namespace) {
141+
return new Pm2Namespace(this.io, namespace);
137142
}
138143
}]);
139144
return Pm2SocketIO;

namespace.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
4+
5+
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
6+
7+
var _createClass2 = require('babel-runtime/helpers/createClass');
8+
9+
var _createClass3 = _interopRequireDefault(_createClass2);
10+
11+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12+
13+
var Pm2Socket = require('./socket');
14+
module.exports = function () {
15+
function Namespace(io, namespace) {
16+
(0, _classCallCheck3.default)(this, Namespace);
17+
18+
this.io = io;
19+
this.namespace = namespace;
20+
}
21+
22+
(0, _createClass3.default)(Namespace, [{
23+
key: 'on',
24+
value: function on(event, cb) {
25+
var _this = this;
26+
27+
if (event == 'connection') {
28+
this.io.of(this.namespace).on(event, function (socket) {
29+
var s = new Pm2Socket(socket, _this.io.of(_this.namespace));
30+
cb(s);
31+
});
32+
} else this.io.of(this.namespace).on(event, function () {
33+
cb.apply(undefined, arguments);
34+
});
35+
}
36+
}, {
37+
key: 'emit',
38+
value: function emit(event) {
39+
var _io$sockets, _io$of$sockets;
40+
41+
for (var _len = arguments.length, data = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
42+
data[_key - 1] = arguments[_key];
43+
}
44+
45+
(_io$sockets = this.io.sockets).emit.apply(_io$sockets, ['@toServer', event, this.namespace].concat(data));
46+
(_io$of$sockets = this.io.of(this.namespace).sockets).emit.apply(_io$of$sockets, [event].concat(data));
47+
}
48+
}]);
49+
return Namespace;
50+
}();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "socketio-with-pm2",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "This is an nodejs plugin for socketio, which support socketio run with the cluster mode of pm2",
55
"main": "index.js",
66
"scripts": {

src/master.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
require('colors');
55
var Pm2Socket = require('./socket')
6+
var Pm2Namespace = require('./namespace');
67
module.exports = class Pm2SocketIO {
78
static getIp() {
89
var os = require('os');
@@ -60,7 +61,6 @@ module.exports = class Pm2SocketIO {
6061
for (let i = 0; i < (process.env.instances || 1); i++) {
6162
processList.push(i);
6263
}
63-
6464
this.addresses.map(ip=> {
6565
processList.map(id=> {
6666
setTimeout(()=> {
@@ -74,9 +74,9 @@ module.exports = class Pm2SocketIO {
7474
} catch (e) {
7575
console.error(e)
7676
}
77-
connect.on('@toServer', (event, ...data)=> {
77+
connect.on('@toServer', (event, namespace, ...data)=> {
7878
if (this.instanceId != id || ip != this.localIp) {
79-
this.io.sockets.emit(event, ...data);
79+
this.io.of(namespace || '/').sockets.emit(event, ...data);
8080
}
8181
})
8282
}, 0)
@@ -99,8 +99,12 @@ module.exports = class Pm2SocketIO {
9999
}
100100

101101
emit(event, ...data) {
102-
this.io.sockets.emit('@toServer', event, ...data);
102+
this.io.sockets.emit('@toServer', event, '/', ...data);
103103
this.io.sockets.emit(event, ...data);
104104
}
105105

106+
of(namespace) {
107+
return new Pm2Namespace(this.io, namespace);
108+
}
109+
106110
}

src/namespace.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Created by denniszhang on 17/2/23.
3+
*/
4+
var Pm2Socket = require('./socket')
5+
module.exports = class Namespace {
6+
constructor(io, namespace) {
7+
this.io = io;
8+
this.namespace = namespace;
9+
10+
}
11+
12+
on(event, cb) {
13+
if (event == 'connection') {
14+
this.io.of(this.namespace).on(event, socket=> {
15+
var s = new Pm2Socket(socket, this.io.of(this.namespace));
16+
cb(s);
17+
})
18+
}
19+
else this.io.of(this.namespace).on(event, function (...data) {
20+
cb(...data);
21+
})
22+
}
23+
24+
emit(event, ...data) {
25+
this.io.sockets.emit('@toServer', event,this.namespace, ...data);
26+
this.io.of(this.namespace).sockets.emit(event, ...data);
27+
}
28+
29+
}

test/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
var Pm2Socketio = require('../index');
55
var io = new Pm2Socketio();
66
io.listen(3001);
7-
io.on('connection', socket=> {
7+
io.of('/chat').on('connection', socket=> {
88
console.log('connect success!')
99
socket.on('eventSave', data=> {
1010
socket.broadcast.emit('eventUpdate', data);

0 commit comments

Comments
 (0)