单个客户端中的 SockJS 多连接

SockJS multiple connection in single client

我正在尝试从单个客户端连接多个套接字。问题是我的脚本每 2 秒初始化一次新连接,但在 10 秒后(总共 5 个连接)这些连接尝试一起连接到套接字。我想要的是:

初始化连接 1-->
连接1连接到socket-->
连接1监听消息-->

初始化连接 2-->
连接2连接到socket-->
连接2监听消息-->

初始化连接 3-->
连接3连接到socket-->
连接3监听消息-->

等等..我想让他们一一联系起来。但是我做不到。

    'use strict'

    let stompClient
    class Socket{
        constructor(ip,ith) {  // Constructor
        this.ipAddress = ip;
        this.ith = ith;
        this.connected = false;
      }

    async connect(){
        const connect = (event) => {
        document.getElementById("insideText" + this.ith).innerHTML = "Connecting..";
        const socket = new SockJS('http://'+ this.ipAddress +':8081/server')
        stompClient = Stomp.over(socket)
        stompClient.connect({}, onConnected, onError)
        //event.preventDefault()
    }

    const onConnected = () => {
        stompClient.subscribe('/topic/public', onMessageReceived)
        stompClient.send("/app/chat.newUser",
            {},
            JSON.stringify({sender: "client", type: 'CONNECT'})
        )
    }

    const onError = (error) => {
        document.getElementById("insideText" + this.ith).innerHTML = "Connection failed!";
        document.getElementById("img" + this.ith).src = "Dead.png"
        //const status = document.querySelector('#status')
        //status.innerHTML = 'Could not find the connection you were looking for. Move along. Or, Refresh the page!'
        //status.style.color = 'red'
    }

    const onMessageReceived = (payload) => {
        const message = JSON.parse(payload.body);
        var date = new Date().getTime();
        var number = new Number(message.startTime);
        var difference = date - number;

        if(message.status === "BUSY"){
            document.getElementById("img" + this.ith).src = "Busy.png"
            document.getElementById("insideText" + this.ith).innerHTML = message.crawler + ", " + msToTime(difference);
        }
        if(message.status === "FREE"){
            document.getElementById("img" + this.ith).src = "Free.png"
            document.getElementById("insideText" + this.ith).innerHTML = message.crawler;
        }
    }

        connect();
    }
    }

    function msToTime(duration) {
      var milliseconds = parseInt((duration % 1000) / 100),
        seconds = Math.floor((duration / 1000) % 60),
        minutes = Math.floor((duration / (1000 * 60)) % 60),
        hours = Math.floor((duration / (1000 * 60 * 60)) % 24);

      hours = (hours < 10) ? "0" + hours : hours;
      minutes = (minutes < 10) ? "0" + minutes : minutes;
      seconds = (seconds < 10) ? "0" + seconds : seconds;

      return hours + ":" + minutes + ":" + seconds;
    }

    function sleep(ms) {
      var start = new Date().getTime(), expire = start + ms;
      while (new Date().getTime() < expire) { }
      return;
    }

    for (var i = 1; i < 6; i++) {
sleep(2000)
        if(i === 1){
            var socket = new Socket("10.10.10.43",i);
            socket.connect();
        }else if(i === 2){
            var socket = new Socket("10.10.10.43",i);
            socket.connect();
        }
        else if(i === 3){
            var socket = new Socket("10.10.10.43",i);
            socket.connect();
        }
        else if(i === 4){
            var socket = new Socket("10.10.10.43",i);
            socket.connect();
        }else{
            var socket = new Socket("10.10.10.43",i);
            socket.connect();   
        }

    }

场景:每个连接应该监听2秒,然后交给下一个

你现在要做的是用 2 秒的间隔初始化每个连接,然后......
然后当然所有连接或多或少同时检查传入数据。
所以你应该有一个像(如果这是你想要的)接收事件的例程:

// Global part
socketInit = 0;
var socket1;
....
 var socket5;

 // on doc ready run once
 if(socketInit  == 0){
socketInit = 1;
for (var i = 1; i < 6; i++) {
    var socket = "socket"+i;
    var socket = new Socket("10.10.10.43",i);
    socket.connect();
     sleep(2000);// if you need this here already
    }
 } // end run once

 // on doc ready run contiously
  for (var i = 1; i < 6; i++) {
  socketAction (i);   
  sleep(2000);
   }

// Function to be called
function action(socketNum) {
        var socket = "socket" + socketNum;
        if(socket.data != null) {   //I dont know your lib so replace it with the appropriate data receive event/ indicator
   .... do something ...
}