File tree 3 files changed +24
-3
lines changed
3 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,16 @@ This can be used to restart the worker by calling `.fork()` again.
248
248
249
249
See [ child_process event: 'exit'] ( child_process.html#child_process_event_exit ) .
250
250
251
+ ## Event: 'message'
252
+
253
+ * ` worker ` {Worker object}
254
+ * ` message ` {Object}
255
+
256
+ Emitted when any worker receives a message.
257
+
258
+ See
259
+ [ child_process event: 'message'] ( child_process.html#child_process_event_message ) .
260
+
251
261
## Event: 'setup'
252
262
253
263
* ` settings ` {Object}
@@ -530,6 +540,8 @@ created. It is disconnected after the `disconnect` event is emitted.
530
540
531
541
* ` message ` {Object}
532
542
543
+ Similar to the ` cluster.on('message') ` event, but specific to this worker.
544
+
533
545
This event is the same as the one provided by ` child_process.fork() ` .
534
546
535
547
In a worker you can also use ` process.on('message') ` .
Original file line number Diff line number Diff line change @@ -319,6 +319,8 @@ function masterInit() {
319
319
process : workerProcess
320
320
} ) ;
321
321
322
+ worker . on ( 'message' , this . emit . bind ( this , 'message' ) ) ;
323
+
322
324
function removeWorker ( worker ) {
323
325
assert ( worker ) ;
324
326
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ var net = require('net');
6
6
7
7
function forEach ( obj , fn ) {
8
8
Object . keys ( obj ) . forEach ( function ( name , index ) {
9
- fn ( obj [ name ] , name , index ) ;
9
+ fn ( obj [ name ] , name ) ;
10
10
} ) ;
11
11
}
12
12
@@ -45,6 +45,10 @@ if (cluster.isWorker) {
45
45
else if ( cluster . isMaster ) {
46
46
47
47
var checks = {
48
+ global : {
49
+ 'receive' : false ,
50
+ 'correct' : false
51
+ } ,
48
52
master : {
49
53
'receive' : false ,
50
54
'correct' : false
@@ -76,12 +80,15 @@ else if (cluster.isMaster) {
76
80
// Spawn worker
77
81
var worker = cluster . fork ( ) ;
78
82
79
- // When a IPC message is received form the worker
83
+ // When a IPC message is received from the worker
80
84
worker . on ( 'message' , function ( message ) {
81
85
check ( 'master' , message === 'message from worker' ) ;
82
86
} ) ;
87
+ cluster . on ( 'message' , function ( message ) {
88
+ check ( 'global' , message === 'message from worker' ) ;
89
+ } ) ;
83
90
84
- // When a TCP connection is made with the worker connect to it
91
+ // When a TCP server is listening in the worker connect to it
85
92
worker . on ( 'listening' , function ( ) {
86
93
87
94
client = net . connect ( common . PORT , function ( ) {
You can’t perform that action at this time.
0 commit comments