@@ -196,10 +196,10 @@ app.use(function (req, res) {
196196const server = http .createServer (app);
197197const wss = new WebSocket.Server ({ server });
198198
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 );
201201 // 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)
203203
204204 ws .on (' message' , function incoming (message ) {
205205 console .log (' received: %s' , message);
@@ -279,17 +279,17 @@ const WebSocket = require('ws');
279279
280280const wss = new WebSocket.Server ({ port: 8080 });
281281
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 ;
284284});
285285```
286286
287287When the server runs behing a proxy like NGINX, the de-facto standard is to use
288288the ` X-Forwarded-For ` header.
289289
290290``` 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' ];
293293});
294294```
295295
0 commit comments