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