var app = require("express")();
var server = require("http").Server(app);
var io = require("socket.io")(server);
var requestIp = require('request-ip');
server.listen(3000);
var ipMiddleware = function(req, res) {
return requestIp.getClientIp(req);
};
var ip = null;
app.get("/", function (req, res) {
ip = ipMiddleware(req, res);
res.sendFile(__dirname + "/index.html");
});
io.on("connection", function (socket) {
// send the ip to user
});
我的问题是,我想用快递获取客户端的IP地址并向客户端发出ip地址,ips是不同的那么应该是,我怎样才能发出我用快递获得的ip?谢谢
答案 0 :(得分:9)
socket.io
方法获取客户端IP地址。
io.on("connection", function (socket) {
var clientIp = socket.request.connection.remoteAddress;
socket.emit('eventName',{ip : clientIp}); //emit it back to client
});
检查此stackoverflow thread以了解如何获取不同socket.io
版本的客户端IP。