{"action":"closed","issue":{"url":"https://api.github.com/repos/websockets/ws/issues/1128","repository_url":"https://api.github.com/repos/websockets/ws","labels_url":"https://api.github.com/repos/websockets/ws/issues/1128/labels{/name}","comments_url":"https://api.github.com/repos/websockets/ws/issues/1128/comments","events_url":"https://api.github.com/repos/websockets/ws/issues/1128/events","html_url":"https://github.com/websockets/ws/issues/1128","id":232483449,"number":1128,"title":"Client Ip address","user":{"login":"sherikapotein","id":26794472,"avatar_url":"https://avatars0.githubusercontent.com/u/26794472?v=3","gravatar_id":"","url":"https://api.github.com/users/sherikapotein","html_url":"https://github.com/sherikapotein","followers_url":"https://api.github.com/users/sherikapotein/followers","following_url":"https://api.github.com/users/sherikapotein/following{/other_user}","gists_url":"https://api.github.com/users/sherikapotein/gists{/gist_id}","starred_url":"https://api.github.com/users/sherikapotein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sherikapotein/subscriptions","organizations_url":"https://api.github.com/users/sherikapotein/orgs","repos_url":"https://api.github.com/users/sherikapotein/repos","events_url":"https://api.github.com/users/sherikapotein/events{/privacy}","received_events_url":"https://api.github.com/users/sherikapotein/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-05-31T07:55:23Z","updated_at":"2017-05-31T08:51:17Z","closed_at":"2017-05-31T08:51:16Z","body":"Not sure if this qualifies as an '\"Issue\", but more of a question (there should a forum for such a popular suite).\r\n\r\nBeing new to web programming pardon my ignorance, but I write this down after spending a couple days in attempt towards resolution.\r\n\r\nMy setup has a nginx server configured as a reverse proxy redirecting websocket clients. \r\nThe server script is as given in the documentation [here](https://github.com/websockets/ws#broadcast-example). \r\n\r\nThe server and application seem to be setup properly as i can send data to and fro from the local as well as other clients in the LAN.\r\n\r\nHowever,when trying to get the IP address of the client I always end up in an \"undefined\" using the suggested script for retrieving client IP when running behind a proxy, documented [here](https://github.com/websockets/ws#how-to-get-the-ip-address-of-the-client).\r\n\r\nEDIT: on using the script (in the same section) to retrieve the client IP from raw socket, I get results as\r\n> ::ffff:127.0.0.1\r\n\r\nMy server script, which is a little tweaked version of the linked script looks like this\r\n```\r\nconst WebSocket = require('ws');\r\nconst wss = new WebSocket.Server({ port: 8010 });\r\n\r\n// Broadcast to all.\r\nwss.broadcast = function broadcast(data) {\r\n  wss.clients.forEach(function each(client) {\r\n    if (client.readyState === WebSocket.OPEN) {\r\n      client.send(data);\r\n\tconsole.log(client);\r\n    }\r\n  });\r\n};\r\n\r\nwss.on('connection', function connection(ws,req) {\r\n  console.log('New client connected from IP');\r\n\tconst ip = req.headers['x-forwarded-for'];\r\n\tconsole.log(ip);\r\n  ws.on('message', function incoming(data) {\r\n    // Broadcast to everyone else.\r\n\tconsole.log(data);\r\n\r\n\r\n    wss.clients.forEach(function each(client) {\r\n      if (client !== ws && client.readyState === WebSocket.OPEN) {\r\n        console.log('Sending..');\r\n\tconsole.log(data);\r\n\tconsole.log(client.remoteAddress);\r\n\tclient.send(data);\r\n\r\n      }\r\n    });\r\n  });\r\n});\r\n```\r\n\r\n\r\nThe client side files are \r\n`index.html`\r\n```\r\n<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n\t<meta charset=\"utf-8\">\r\n\t<title>WebSockets Demo</title>\r\n\r\n\t<link rel=\"stylesheet\" href=\"style.css\">\r\n</head>\r\n<body>\r\n\t<div id=\"page-wrapper\">\r\n\t\t<h1>WebSockets Demo</h1>\r\n\r\n\t\t<div id=\"status\">Connecting...</div>\r\n\r\n\t\t<ul id=\"messages\"></ul>\r\n\r\n\t\t<form id=\"message-form\" action=\"#\" method=\"post\">\r\n\t\t\t<textarea id=\"message\" placeholder=\"Write your message here...\" required></textarea>\r\n\t\t\t<button type=\"submit\">Send Message</button>\r\n\t\t\t<button type=\"button\" id=\"close\">Close Connection</button>\r\n\t\t</form>\r\n\t</div>\r\n\r\n\t<script src=\"app.js\"></script>\r\n</body>\r\n</html>\r\n```\r\nand `app.js`\r\n```\r\nwindow.onload = function() {\r\n//alert('sadsads');\r\n\r\n//const WebSocket = require('/usr/local/lib/ws');\r\n//alert('sdfsdfdsfdhgdhgfhfg');\r\n  // Get references to elements on the page.\r\n  var form = document.getElementById('message-form');\r\n  var messageField = document.getElementById('message');\r\n  var messagesList = document.getElementById('messages');\r\n  var socketStatus = document.getElementById('status');\r\n  var closeBtn = document.getElementById('close');\r\n\r\nalert('sdfsdfdsfdhgdhgfhfg');\r\n  // Create a new WebSocket.\r\n  var socket = new WebSocket('ws://192.168.1.105:8020'); //LAN Ip of host port 8020 is nginx reverse proxy \r\n\r\n  // Handle any errors that occur.\r\n  socket.onerror = function(error) {\r\n    socketStatus.innerHTML = 'WebSocket Error: ' + error;\r\n  };\r\n\r\n  // Show a connected message when the WebSocket is opened.\r\n  socket.onopen = function(event) {\r\n    socketStatus.innerHTML = 'Connected to: ' + event.currentTarget.url;\r\n    socketStatus.className = 'open';\r\n  };\r\n  // Handle messages sent by the server.socketStat\r\n  socket.onmessage = function(event) {\r\n    var message = event.data;\r\n    messagesList.innerHTML += '<li class=\"received\"><span>Received:</span>' +\r\n                               message + '</li>';\r\n  };\r\n\r\n  // Show a disconnected message when the WebSocket is closed.\r\n  socket.onclose = function(event) {\r\n    socketStatus.innerHTML = 'Disconnected from WebSocket.';\r\n    socketStatus.className = 'closed';\r\n  };\r\n  // Send a message when the form is submitted.\r\n  form.onsubmit = function(e) {\r\n    e.preventDefault();\r\n     socketStatus.innerHTML = 'PRESSED from WebSocket.';\r\n\r\n    // Retrieve the message from the textarea.\r\n    var message = messageField.value;\r\n    // Send the message through the WebSocket.\r\n    socket.send(message);\r\n\r\n    // Add the message to the messages list.\r\n    messagesList.innerHTML += '<li class=\"sent\"><span>Sent:</span>' + message +\r\n                              '</li>';\r\n\r\n    // Clear out the message field.\r\n    messageField.value = '';\r\n    return false;\r\n  };\r\n\r\n  // Close the WebSocket connection when the close button is clicked.\r\n  closeBtn.onclick = function(e) {\r\n    e.preventDefault();\r\n    // Close the WebSocket.\r\n    socket.close();\r\n    return false;\r\n  };\r\n};\r\n```\r\n\r\n\r\nCan somebody help me where am I going wrong?"}}
{"actor":{"display_login":"lpinca"}}