@@ -55,10 +55,10 @@ Any serializable data structures can be emitted, including:
55
55
Sample code:
56
56
57
57
``` js
58
- io .on (' connection' , function ( socket ) {
59
- socket .emit (' request' , /* */ ); // emit an event to the socket
60
- io .emit (' broadcast' , /* */ ); // emit an event to all connected sockets
61
- socket .on (' reply' , function () { /* */ }); // listen to the event
58
+ io .on (' connection' , socket => {
59
+ socket .emit (' request' , /* … */ ); // emit an event to the socket
60
+ io .emit (' broadcast' , /* … */ ); // emit an event to all connected sockets
61
+ socket .on (' reply' , () => { /* … */ }); // listen to the event
62
62
});
63
63
```
64
64
@@ -84,7 +84,7 @@ This is a useful feature to send notifications to a group of users, or to a give
84
84
## Installation
85
85
86
86
``` bash
87
- npm install socket.io --save
87
+ npm install socket.io
88
88
```
89
89
90
90
## How to use
@@ -93,20 +93,20 @@ The following example attaches socket.io to a plain Node.JS
93
93
HTTP server listening on port ` 3000 ` .
94
94
95
95
``` js
96
- var server = require (' http' ).createServer ();
97
- var io = require (' socket.io' )(server);
98
- io .on (' connection' , function ( client ) {
99
- client .on (' event' , function ( data ){ });
100
- client .on (' disconnect' , function (){ });
96
+ const server = require (' http' ).createServer ();
97
+ const io = require (' socket.io' )(server);
98
+ io .on (' connection' , client => {
99
+ client .on (' event' , data => { /* … */ });
100
+ client .on (' disconnect' , () => { /* … */ });
101
101
});
102
102
server .listen (3000 );
103
103
```
104
104
105
105
### Standalone
106
106
107
107
``` js
108
- var io = require (' socket.io' )();
109
- io .on (' connection' , function ( client ){ });
108
+ const io = require (' socket.io' )();
109
+ io .on (' connection' , client => { ... });
110
110
io .listen (3000 );
111
111
```
112
112
@@ -118,10 +118,10 @@ to pass the `Server` to `socket.io`, and not the express application
118
118
function. Also make sure to call ` .listen ` on the ` server ` , not the ` app ` .
119
119
120
120
``` js
121
- var app = require (' express' )();
122
- var server = require (' http' ).createServer (app);
123
- var io = require (' socket.io' )(server);
124
- io .on (' connection' , function () { /* … */ });
121
+ const app = require (' express' )();
122
+ const server = require (' http' ).createServer (app);
123
+ const io = require (' socket.io' )(server);
124
+ io .on (' connection' , () => { /* … */ });
125
125
server .listen (3000 );
126
126
```
127
127
@@ -131,10 +131,10 @@ Like Express.JS, Koa works by exposing an application as a request
131
131
handler function, but only by calling the ` callback ` method.
132
132
133
133
``` js
134
- var app = require (' koa' )();
135
- var server = require (' http' ).createServer (app .callback ());
136
- var io = require (' socket.io' )(server);
137
- io .on (' connection' , function () { /* … */ });
134
+ const app = require (' koa' )();
135
+ const server = require (' http' ).createServer (app .callback ());
136
+ const io = require (' socket.io' )(server);
137
+ io .on (' connection' , () => { /* … */ });
138
138
server .listen (3000 );
139
139
```
140
140
0 commit comments