簡體   English   中英

socket.io-客戶端通過同一服務器連接到兩個套接字

[英]socket.io - Client connects to two sockets by same server

我正在嘗試在React客戶端和Node.js服務器端之間建立套接字連接。 但是服務器將托管兩個套接字。 這是服務器端代碼

var app = express();
var server = http.createServer(app);
var io = require('socket.io')(2893, {
    path: "/ws",
    resource: "/ws",
    transports: ['websocket'],
    pingTimeout: 5000
});
var redis = require('redis');
const subscriber = redis.createClient();
require('./server/route')(app, io);
require('./server/lib/subscriber')(require('socket.io').listen(server), subscriber);

第一個套接字連接正常,但我想知道為什么第二個套接字不起作用(它與listen(server) 。這是我編寫的subscriber模塊:

module.exports = (io, subscriber) => {

    io.sockets.on('connection', (socket) => {
        console.log(socket);
        socket.on('room', (room) => {
            socket.join(room);
        });
    });

    subscriber.on('pmessage', (pattern, channel, message) => {
        const msg = JSON.parse(message);
        const idCallcenter = msg.idCallcenter;
        return io.to(idCallcenter).emit('message', { type: channel, message: msg });
    });

    subscriber.psubscribe('*');

};

和客戶端React模塊

var socketOption = { path: "/ws", transports: ['websocket'] };
var socket = io("http://localhost:2893", socketOption);
var socket2 = io.connect("http://localhost:4004");
export default function (user) {
  debugger
  socket.user = user;
  contact(socket);
  notify(socket);
  socket.on('connect', function () {
    debug('socket connect', socket.id);
    store.dispatch(connectNetworkSuccess());
    socket.emit('user-online', {
      idUser: user._id,
      idCallcenter: user.idCallcenter,
      email: user.email
    });
  });
  socket2.on('connect', () => {
    debug('Socket connected');
    socket2.emit('room', user.idCallcenter);

  });
  socket2.on('message', (data) => {
    debugger
    debug('Socket message');
    debug(data);
    const type = data.type;
    const message = data.message;
    if (type === 'recordFetched') {
    }

  });
  socket.emit('user-online', {
    idUser: user._id,
    idCallcenter: user.idCallcenter,
    email: user.email
  });
  socket.on('disconnect', function (reason) {
    debug('socket disconnect', reason);
    store.dispatch(connectNetworkFailed());
  });
} 

第一個socket (端口2893中)正常運行。 同時, socket2 (在端口4004中)未連接。 它不會跳入服務器端和客戶端的connection回調。 我在這里做錯了什么?

我自己解決了這個案子。 客戶端的工作代碼是:

export default function (user) {
    debugger
    var socketOption = { path: "/ws", transports: ['websocket'] };
    var socket = env === "local" ? io("http://localhost:2893", socketOption) : io(window.location.origin, socketOption);
    var socket2 = io.connect();
    socket.user = user;
    contact(socket);
    notify(socket);
    socket.on('connect', function () {
        debug('socket connect', socket.id);
        store.dispatch(connectNetworkSuccess());
        socket.emit('user-online', {
            idUser: user._id,
            idCallcenter: user.idCallcenter,
            email: user.email
        });

    });
    socket2.on('connect', () => {
        console.log('Socket connected');
        socket2.emit('room', user.idCallcenter);

    });
    socket2.on('message', (data) => {
        debugger
        console.log('Socket message', data);
        const type = data.type;
        const message = data.message;
        if (type === 'recordFetched') {
        }

    });
    socket.emit('user-online', {
        idUser: user._id,
        idCallcenter: user.idCallcenter,
        email: user.email
    });
    socket.on('disconnect', function (reason) {
        debug('socket disconnect', reason);
        store.dispatch(connectNetworkFailed());
    });
} 

服務器確實跳入了connection回調,但沒有進入room回調。 我想這是因為客戶端的connect回調是在建立連接之后定義的,因此它無法跳入該連接。 這是我的可能性。 我對嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM