You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `ack` argument is optional and will be called with the server answer.
434
449
435
450
```js
436
-
socket.emit('ferret', 'tobi', function(data) {
451
+
socket.emit('ferret', 'tobi', (data)=> {
437
452
console.log(data); // data will be 'woot'
438
453
});
439
454
440
455
// server:
441
-
// io.on('connection', function (socket) {
442
-
// socket.on('ferret', function (name, fn) {
456
+
// io.on('connection', (socket) => {
457
+
// socket.on('ferret', (name, fn) => {
443
458
// fn('woot');
444
459
// });
445
460
// });
@@ -454,9 +469,18 @@ socket.emit('ferret', 'tobi', function (data) {
454
469
Register a new handler for the given event.
455
470
456
471
```js
457
-
socket.on('news', function(data) {
472
+
socket.on('news', (data)=> {
458
473
console.log(data);
459
474
});
475
+
476
+
// with multiple arguments
477
+
socket.on('news', (arg1, arg2, arg3, arg4) => {
478
+
// ...
479
+
});
480
+
// with callback
481
+
socket.on('news', (cb) => {
482
+
cb(0);
483
+
});
460
484
```
461
485
462
486
The socket actually inherits every method of the [Emitter](https://github.com/component/emitter) class, like `hasListeners`, `once` or `off` (to remove an event listener).
@@ -486,50 +510,140 @@ Synonym of [socket.close()](#socketclose).
486
510
487
511
Fired upon a connection including a successful reconnection.
488
512
513
+
```js
514
+
socket.on('connect', () => {
515
+
// ...
516
+
});
517
+
518
+
// note: you should register event handlers outside of connect,
519
+
// so they are not registered again on reconnection
520
+
socket.on('myevent', () => {
521
+
// ...
522
+
});
523
+
```
524
+
489
525
#### Event: 'connect_error'
490
526
491
527
-`error`_(Object)_ error object
492
528
493
529
Fired upon a connection error.
494
530
531
+
```js
532
+
socket.on('connect_error', (error) => {
533
+
// ...
534
+
});
535
+
```
536
+
495
537
#### Event: 'connect_timeout'
496
538
497
539
Fired upon a connection timeout.
498
540
541
+
```js
542
+
socket.on('connect_timeout', (timeout) => {
543
+
// ...
544
+
});
545
+
```
546
+
499
547
#### Event: 'error'
500
548
501
549
-`error`_(Object)_ error object
502
550
503
551
Fired when an error occurs.
504
552
553
+
```js
554
+
socket.on('error', (error) => {
555
+
// ...
556
+
});
557
+
```
558
+
505
559
#### Event: 'disconnect'
506
560
561
+
-`reason`_(String)_ either 'io server disconnect' or 'io client disconnect'
0 commit comments