D
D
Dimash Kenzhegaliev2021-05-24 18:22:48
Node.js
Dimash Kenzhegaliev, 2021-05-24 18:22:48

A message is not sent from the back to the front via web socket, what's the problem?

i have this code on the front

connection = new WebSocket('ws://localhost:1000/' + this.phoneNumber)
    connection.onmessage = async msg => {
      msg = JSON.parse(msg.data)
      console.log(msg)
      if (msg.action === 'registeredNewFire' && this.userData.typeOfUser === 'fireman') {
        const message = {
          action: 'takeCall',
          agent: 'fireMan',
          data: {
            fireManId: this.userData._id,
            causing: msg.data.resultRegistrationNewFire.causing,
            currentFireId: msg.data.resultRegistrationNewFire._id
          }
        }

        connection.send(JSON.stringify(message))
      }

      if (msg.action === 'fireTruckDispatched') {
        this.activeFireManPhoneNumber = msg.data.fireManPhoneNumber
        console.log(this.activeFireManPhoneNumber, 'dispatched')
      }

      if (msg.action === 'startGeoDataTransfering') {
        const geoDataTranfering = {
          action: 'geoDataTransfering',
          agent: 'fireMan',
          data: {
            phoneNumber: msg.data.causingPhoneNumber,
            message: {
              latitude: Math.floor(Math.random() * 100) + 1,
              altitude: Math.floor(Math.random() * 100) + 1
            }
          }
        }
        this.activeCausingPhoneNumber = msg.data.causingPhoneNumber
        this.loopKey = setInterval(() => connection.send(JSON.stringify(geoDataTranfering)), 500)
        console.log(this.loopKey)
      }

      if (msg.action === 'fireBrigadeArrived') {
        console.log(loopKey, true)
        clearInterval(loopKey)
      }
    }
  },
methods: {
brigadeArrived () {
      const message = {
        action: 'fireBrigadeArrived',
        agent: 'client',
        data: {
          fireManPhoneNumber: this.activeFireManPhoneNumber,
          loopKey: this.loopKey
        }
      }
      console.log(message)
      connection.send(JSON.stringify(message))
    }
}

and such a piece from the back
// fire brigade Arrived
      if (msg.action === 'fireBrigadeArrived') {
        const messageForFireBrigadeArrived = {
          action: 'fireBrigadeArrived',
          agent: 'server',
          data: {
            result: true
          }
        }
        for (let clientForFireBrrigadeArrived of clients) {
          if (clientForFireBrrigadeArrived.phoneNumber === msg.data.fireManPhoneNumber) {
            clientForFireBrrigadeArrived.connection.send(JSON.stringify(messageForFireBrigadeArrived))
          }
        }
      }

in general, when receiving a certain message on the front via a websocket, I turn on setInterval and in this way I kind of constantly send messages to the back with an interval of 500ms, the problem is that when I send another message from the front to the back, so that he sends this message to the front and stopped setInterval, it happens here if (msg.action === 'fireBrigadeArrived'), the fact is that it doesn't even go into this if although I sort of checked everything the message is sent, the action is the same it should go there but nothing works I don’t understand anything anymore, I think maybe it’s because of setInterval, it doesn’t allow you to receive other messages, but I don’t, if so, how can I get around this, please help

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question