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}
@@ -528,6 +538,8 @@ created. It is disconnected after the `disconnect` event is emitted.
528
538
529
539
* ` message ` {Object}
530
540
541
+ Similar to the ` cluster.on('message') ` event, but specific to this worker.
542
+
531
543
This event is the same as the one provided by ` child_process.fork() ` .
532
544
533
545
In a worker you can also use ` process.on('message') ` .
Original file line number Diff line number Diff line change @@ -318,6 +318,8 @@ function masterInit() {
318
318
process : workerProcess
319
319
} ) ;
320
320
321
+ worker . on ( 'message' , this . emit . bind ( this , 'message' ) ) ;
322
+
321
323
function removeWorker ( worker ) {
322
324
assert ( worker ) ;
323
325
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ var net = require('net');
5
5
6
6
function forEach ( obj , fn ) {
7
7
Object . keys ( obj ) . forEach ( function ( name , index ) {
8
- fn ( obj [ name ] , name , index ) ;
8
+ fn ( obj [ name ] , name ) ;
9
9
} ) ;
10
10
}
11
11
@@ -44,6 +44,10 @@ if (cluster.isWorker) {
44
44
else if ( cluster . isMaster ) {
45
45
46
46
var checks = {
47
+ global : {
48
+ 'receive' : false ,
49
+ 'correct' : false
50
+ } ,
47
51
master : {
48
52
'receive' : false ,
49
53
'correct' : false
@@ -75,12 +79,15 @@ else if (cluster.isMaster) {
75
79
// Spawn worker
76
80
var worker = cluster . fork ( ) ;
77
81
78
- // When a IPC message is received form the worker
82
+ // When a IPC message is received from the worker
79
83
worker . on ( 'message' , function ( message ) {
80
84
check ( 'master' , message === 'message from worker' ) ;
81
85
} ) ;
86
+ cluster . on ( 'message' , function ( message ) {
87
+ check ( 'global' , message === 'message from worker' ) ;
88
+ } ) ;
82
89
83
- // When a TCP connection is made with the worker connect to it
90
+ // When a TCP server is listening in the worker connect to it
84
91
worker . on ( 'listening' , function ( ) {
85
92
86
93
client = net . connect ( common . PORT , function ( ) {
You can’t perform that action at this time.
0 commit comments