Answer the question
In order to leave comments, you need to log in
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))
}
}
// 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))
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question