当我在socket.on('connection')期间记录'socket'对象时,我得到以下信息:
_peername: { address: '::1', family: 'IPv6', port: 57535 }
如何在套接字连接期间将族更改为IPv4?
答案 0 :(得分:2)
我正在测试,如果我使用
,在客户端上发现<
var socket = io('http://localhost:8000', { transports: ['websocket']});
返回socket.handshake.address
如果我使用
::1
var socket = io('http://server_ip:8000', { transports: ['websocket']});
返回socket.handshake.address
答案 1 :(得分:1)
创建http实例时,请确保将连接绑定到IPv4地址。 例如:
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
http.listen(8000, '0.0.0.0', function(){
console.log('listening on *:8000');
});
这应该可以解决问题。 :)
答案 2 :(得分:0)
这应该听IPv4;试试吧,让我知道......
var net = require('net');
var server = net.createServer();
server.on('connection', handleConnection);
server.listen({
host: 'localhost',
port: 8000,
exclusive: true
}, function() {
console.log('server listening to %j', server.address());
});
输出:
server listening to {"address":"127.0.0.1","family":"IPv4","port":8000}