socket io address给出了ffff值

时间:2015-06-08 03:32:36

标签: javascript jquery node.js sockets

如何获取地址值(192.168.0.number),因为当我控制'地址'时它会给我一个额外的值':: ffff:address'

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

http.listen(3000, function() {
  console.log('listening on *:3000');
});

app.use(express.static(__dirname + '/public'));

var usernames = {};
var numUsers = 0;

io.sockets.on('connection', function (socket) {
  var addedUser = false;
  var address = socket.handshake.address;

  console.log("New connection from " + address);
}

1 个答案:

答案 0 :(得分:2)

这是IPv4映射的IPv6地址。我想提取IPv4部分的最简单方法是执行以下操作:

// ...
var idx = address.lastIndexOf(':');
if (~idx && ~address.indexOf('.'))
  address = address.slice(idx + 1);