Skip to content

Commit 68fc0da

Browse files
committed
fix getIp bug
1 parent b0b50f5 commit 68fc0da

File tree

4 files changed

+75
-91
lines changed

4 files changed

+75
-91
lines changed

.idea/workspace.xml

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

master.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ module.exports = function () {
2222
value: function getIp() {
2323
var os = require('os');
2424
var interfaces = os.networkInterfaces();
25-
module.exports = function () {
26-
for (var k in interfaces) {
27-
for (var k2 in interfaces[k]) {
28-
var address = interfaces[k][k2];
29-
if (address.family === 'IPv4' && !address.internal) {
30-
return address.address;
31-
}
25+
for (var k in interfaces) {
26+
for (var k2 in interfaces[k]) {
27+
var address = interfaces[k][k2];
28+
if (address.family === 'IPv4' && !address.internal) {
29+
return address.address;
3230
}
3331
}
34-
return 'localhost';
35-
};
32+
}
33+
return 'localhost';
3634
}
3735
}, {
3836
key: 'getInstanceId',

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.3",
3+
"version": "0.1.4",
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: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@
44
require('colors');
55
var Pm2Socket = require('./socket')
66
module.exports = class Pm2SocketIO {
7-
static getIp(){
7+
static getIp() {
88
var os = require('os');
99
var interfaces = os.networkInterfaces();
10-
module.exports = function (){
11-
for (var k in interfaces) {
12-
for (var k2 in interfaces[k]) {
13-
var address = interfaces[k][k2];
14-
if (address.family === 'IPv4' && !address.internal) {
15-
return address.address;
16-
}
10+
for (var k in interfaces) {
11+
for (var k2 in interfaces[k]) {
12+
var address = interfaces[k][k2];
13+
if (address.family === 'IPv4' && !address.internal) {
14+
return address.address;
1715
}
1816
}
19-
return 'localhost';
2017
}
18+
return 'localhost';
2119
}
22-
static getInstanceId(){
20+
21+
static getInstanceId() {
2322
return parseInt(process.env.NODE_APP_INSTANCE || 0);
2423
}
25-
constructor(opts){
26-
opts = Object.assign({},{
27-
ips:[],
28-
localIp:'localhost'
29-
},opts)
24+
25+
constructor(opts) {
26+
opts = Object.assign({}, {
27+
ips: [],
28+
localIp: 'localhost'
29+
}, opts)
3030
this.addresses = opts.ips;
3131
this.localIp = opts.localIp;
3232
this.instanceId = Pm2SocketIO.getInstanceId();
3333
try {
34-
if(opts.localIp!='localhost') {
34+
if (opts.localIp != 'localhost') {
3535
this.localIp = Pm2SocketIO.getIp();
3636
}
3737
if (this.addresses.indexOf(this.localIp) == -1 && this.addresses.length >= 2) {
@@ -45,7 +45,8 @@ module.exports = class Pm2SocketIO {
4545
console.error(e)
4646
}
4747
}
48-
listen(port){
48+
49+
listen(port) {
4950
const server = require("http").createServer((req, res)=> {
5051
});
5152
server.listen(port + this.instanceId);
@@ -73,7 +74,7 @@ module.exports = class Pm2SocketIO {
7374
} catch (e) {
7475
console.error(e)
7576
}
76-
connect.on('@toServer', (event,...data)=> {
77+
connect.on('@toServer', (event, ...data)=> {
7778
if (this.instanceId != id || ip != this.localIp) {
7879
this.io.sockets.emit(event, ...data);
7980
}
@@ -84,20 +85,22 @@ module.exports = class Pm2SocketIO {
8485
})
8586
return this;
8687
}
87-
on(event,cb){
88-
if(event == 'connection') {
89-
this.io.on(event,socket=>{
90-
var s = new Pm2Socket(socket,this.io);
88+
89+
on(event, cb) {
90+
if (event == 'connection') {
91+
this.io.on(event, socket=> {
92+
var s = new Pm2Socket(socket, this.io);
9193
cb(s);
9294
})
9395
}
94-
else this.io.on(event,function(...data){
96+
else this.io.on(event, function (...data) {
9597
cb(...data);
9698
})
9799
}
98-
emit(event,...data) {
99-
this.io.sockets.emit('@toServer',event,...data);
100-
this.io.sockets.emit(event,...data);
100+
101+
emit(event, ...data) {
102+
this.io.sockets.emit('@toServer', event, ...data);
103+
this.io.sockets.emit(event, ...data);
101104
}
102105

103106
}

0 commit comments

Comments
 (0)