@@ -197,10 +197,10 @@ app.use(function (req, res) {
197
197
const server = http .createServer (app);
198
198
const wss = new WebSocket.Server ({ server });
199
199
200
- wss .on (' connection' , function connection (ws ) {
201
- const location = url .parse (ws . upgradeReq .url , true );
200
+ wss .on (' connection' , function connection (ws , req ) {
201
+ const location = url .parse (req .url , true );
202
202
// You might use location.query.access_token to authenticate or share sessions
203
- // or ws.upgradeReq .headers.cookie (see http://stackoverflow.com/a/16395220/151312)
203
+ // or req .headers.cookie (see http://stackoverflow.com/a/16395220/151312)
204
204
205
205
ws .on (' message' , function incoming (message ) {
206
206
console .log (' received: %s' , message);
@@ -280,17 +280,17 @@ const WebSocket = require('ws');
280
280
281
281
const wss = new WebSocket.Server ({ port: 8080 });
282
282
283
- wss .on (' connection' , function connection (ws ) {
284
- const ip = ws . upgradeReq .connection .remoteAddress ;
283
+ wss .on (' connection' , function connection (ws , req ) {
284
+ const ip = req .connection .remoteAddress ;
285
285
});
286
286
```
287
287
288
288
When the server runs behing a proxy like NGINX, the de-facto standard is to use
289
289
the ` X-Forwarded-For ` header.
290
290
291
291
``` js
292
- wss .on (' connection' , function connection (ws ) {
293
- const ip = ws . upgradeReq .headers [' x-forwarded-for' ];
292
+ wss .on (' connection' , function connection (ws , req ) {
293
+ const ip = req .headers [' x-forwarded-for' ];
294
294
});
295
295
```
296
296
0 commit comments