RN(0.59)聊天应用程序使用socket.io(2.2.0)与Nodejs(10.16)/ express(4.16)后端进行通信。有2个Android模拟器同时连接到后端进行测试,一个在端口8081上,另一个在端口9090上。这是后端服务器上用于处理套接字的代码:
const io = req.app.get("io");
const socket = req.app.get('socket');
const room = msg.event_id.toString(); //<<<=== '1'
socket.join(room);
io.to(room).emit("event message", msg);
此处io.to(room)
用于将新消息发送到连接的其他套接字客户端。客户端在端口9090上发送的消息可以在客户端在端口8081上接收。但是,客户端在端口8081上发送的消息不能在端口9090上被客户端接收。此外,该消息两次在端口8081上发送到客户端。参见下图。左边是端口9090上的客户端,右边是端口8081上的端口:
这是客户端在端口8081上的套接字输出:
socket in new message : Socket {
nsp:
Namespace {
name: '/',
server:
Server {
nsps: [Object],
parentNsps: Map {},
_path: '/socket.io',
_serveClient: true,
parser: [Object],
encoder: Encoder {},
_adapter: [Function: Adapter],
_origins: '*:*',
sockets: [Circular],
eio: [Server],
httpServer: [Server],
engine: [Server] },
sockets:
{ Akf0q7wpwolQNxNnAAAC: [Socket],
'-rD5m1TUdZ0D6_8pAAAD': [Circular] },
connected:
{ Akf0q7wpwolQNxNnAAAC: [Socket],
'-rD5m1TUdZ0D6_8pAAAD': [Circular] },
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter:
Adapter {
nsp: [Circular],
rooms: [Object],
sids: [Object],
encoder: Encoder {} },
_events: [Object: null prototype] { connection: [AsyncFunction] },
_eventsCount: 1 },
server:
Server {
nsps: { '/': [Namespace] },
parentNsps: Map {},
_path: '/socket.io',
_serveClient: true,
parser:
{ protocol: 4,
types: [Array],
CONNECT: 0,
DISCONNECT: 1,
EVENT: 2,
ACK: 3,
ERROR: 4,
BINARY_EVENT: 5,
BINARY_ACK: 6,
Encoder: [Function: Encoder],
Decoder: [Function: Decoder] },
encoder: Encoder {},
_adapter: [Function: Adapter],
_origins: '*:*',
sockets:
Namespace {
name: '/',
server: [Circular],
sockets: [Object],
connected: [Object],
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter: [Adapter],
_events: [Object],
_eventsCount: 1 },
eio:
Server {
clients: [Object],
clientsCount: 2,
wsEngine: 'ws',
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [Array],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: [Object],
httpCompression: [Object],
initialPacket: [Array],
ws: [WebSocketServer],
_events: [Object],
_eventsCount: 1 },
httpServer:
Server {
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
_connections: 3,
_handle: [TCP],
_usingWorkers: false,
_workers: [],
_unref: false,
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
keepAliveTimeout: 5000,
maxHeadersCount: null,
headersTimeout: 40000,
_connectionKey: '6::::3000',
[Symbol(IncomingMessage)]: [Function],
[Symbol(ServerResponse)]: [Function],
[Symbol(asyncId)]: 9 },
engine:
Server {
clients: [Object],
clientsCount: 2,
wsEngine: 'ws',
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [Array],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: [Object],
httpCompression: [Object],
initialPacket: [Array],
ws: [WebSocketServer],
_events: [Object],
_eventsCount: 1 } },
adapter:
Adapter {
nsp:
Namespace {
name: '/',
server: [Server],
sockets: [Object],
connected: [Object],
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter: [Circular],
_events: [Object],
_eventsCount: 1 },
rooms:
{ '1': [Room],
Akf0q7wpwolQNxNnAAAC: [Room],
'-rD5m1TUdZ0D6_8pAAAD': [Room] },
sids:
{ Akf0q7wpwolQNxNnAAAC: [Object],
'-rD5m1TUdZ0D6_8pAAAD': [Object] },
encoder: Encoder {} },
id: '-rD5m1TUdZ0D6_8pAAAD',
client:
Client {
server:
Server {
nsps: [Object],
parentNsps: Map {},
_path: '/socket.io',
_serveClient: true,
parser: [Object],
encoder: Encoder {},
_adapter: [Function: Adapter],
_origins: '*:*',
sockets: [Namespace],
eio: [Server],
httpServer: [Server],
engine: [Server] },
conn:
Socket {
id: '-rD5m1TUdZ0D6_8pAAAD',
server: [Server],
upgrading: false,
upgraded: false,
readyState: 'open',
writeBuffer: [],
packetsFn: [],
sentCallbackFn: [],
cleanupFn: [Array],
request: [IncomingMessage],
remoteAddress: '::ffff:192.168.2.133',
checkIntervalTimer: null,
upgradeTimeoutTimer: null,
pingTimeoutTimer:
Timeout {
_called: false,
_idleTimeout: 30000,
_idlePrev: [TimersList],
_idleNext: [Timeout],
_idleStart: 3599298,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(unrefed)]: false,
[Symbol(asyncId)]: 2218,
[Symbol(triggerId)]: 399 },
transport: [WebSocket],
_events: [Object],
_eventsCount: 3 },
encoder: Encoder {},
decoder: Decoder { reconstructor: null, _callbacks: [Object] },
id: '-rD5m1TUdZ0D6_8pAAAD',
request:
IncomingMessage {
_readableState: [ReadableState],
readable: true,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
socket: [Socket],
connection: [Socket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: true,
url: '/socket.io/?EIO=3&transport=websocket',
method: 'GET',
statusCode: null,
statusMessage: null,
client: [Socket],
_consuming: false,
_dumped: false,
parser: null,
_query: [Object],
websocket: [WebSocket] },
onclose: [Function: bound ],
ondata: [Function: bound ],
onerror: [Function: bound ],
ondecoded: [Function: bound ],
sockets: { '-rD5m1TUdZ0D6_8pAAAD': [Circular] },
nsps: { '/': [Circular] },
connectBuffer: [] },
conn:
Socket {
id: '-rD5m1TUdZ0D6_8pAAAD',
server:
Server {
clients: [Object],
clientsCount: 2,
wsEngine: 'ws',
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [Array],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: [Object],
httpCompression: [Object],
initialPacket: [Array],
ws: [WebSocketServer],
_events: [Object],
_eventsCount: 1 },
upgrading: false,
upgraded: false,
readyState: 'open',
writeBuffer: [],
packetsFn: [],
sentCallbackFn: [],
cleanupFn: [ [Function], [Function] ],
request:
IncomingMessage {
_readableState: [ReadableState],
readable: true,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
socket: [Socket],
connection: [Socket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: true,
url: '/socket.io/?EIO=3&transport=websocket',
method: 'GET',
statusCode: null,
statusMessage: null,
client: [Socket],
_consuming: false,
_dumped: false,
parser: null,
_query: [Object],
websocket: [WebSocket] },
remoteAddress: '::ffff:192.168.2.133',
checkIntervalTimer: null,
upgradeTimeoutTimer: null,
pingTimeoutTimer:
Timeout {
_called: false,
_idleTimeout: 30000,
_idlePrev: [TimersList],
_idleNext: [Timeout],
_idleStart: 3599298,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(unrefed)]: false,
[Symbol(asyncId)]: 2218,
[Symbol(triggerId)]: 399 },
transport:
WebSocket {
readyState: 'open',
discarded: false,
socket: [WebSocket],
writable: true,
perMessageDeflate: [Object],
supportsBinary: true,
_events: [Object],
_eventsCount: 5,
sid: '-rD5m1TUdZ0D6_8pAAAD',
req: [IncomingMessage] },
_events:
[Object: null prototype] {
close: [Array],
data: [Function: bound ],
error: [Function: bound ] },
_eventsCount: 3 },
rooms: { '1': '1', '-rD5m1TUdZ0D6_8pAAAD': '-rD5m1TUdZ0D6_8pAAAD' },
acks: {},
connected: true,
disconnected: false,
handshake:
{ headers:
{ origin: 'http://192.168.2.133:3000',
upgrade: 'websocket',
connection: 'Upgrade',
'sec-websocket-key': '28/WtfDvCyPzZjpT52PzPA==',
'sec-websocket-version': '13',
host: '192.168.2.133:3000',
'accept-encoding': 'gzip',
'user-agent': 'okhttp/3.12.1' },
time: 'Wed Jun 12 2019 22:24:03 GMT-0700 (Pacific Daylight Time)',
address: '::ffff:192.168.2.133',
xdomain: true,
secure: false,
issued: 1560403443089,
url: '/socket.io/?EIO=3&transport=websocket',
query: { EIO: '3', transport: 'websocket' } },
fns: [],
flags: {},
_rooms: [],
my_id: 22,
_events:
[Object: null prototype] { 'event message': [Function], disconnect: [AsyncFunction] },
_eventsCount: 2 }
主要问题是端口9090上的客户端无法接收来自端口8081的来自客户端的消息。次要问题是端口8081的来自客户端的消息再次发送给自己(重复消息)。