Servicio de Websocket


URL y puerto: Websocket

Carpeta con código: https://genniux.net/Socket


Importar y utilizar servicio


import { SocketService } from "src/app/services/socket.service";


constructor(private chatService: SocketService,){
chatSub$.subscribe(msg => this.newMessage(msg), (err) => console.log("Error al conectar " + err), () => console.log("Desconectado"));


newMessage(notMessage) { // Función que recibe las mensajes del websocket
if (notMessage.tipo == 'id') { // verificar el tipo de mensaje, ID significa que se hizo contacto por primera vez con el websocket

var newUser = {
tipo: "user",
id: notMessage.id,
user: this.user.usuario,
origen: this.user.origen,
users: null,
data: null,
fecha: '',
hora: '',
remitente: this.user.usuario,
app: "MasterSherpa",
id_equipo: null
};

/* console.log("ENVIADO: " + JSON.stringify(newUser));
this.chatService.send(newUser); */
this.sendMessage(newUser);

} else if (notMessage.tipo == 'newComment' || notMessage.tipo == 'newMessage' || notMessage.tipo == 'newSupport' && notMessage.app == "MasterSherpa") { // Este tipo indica que se recibió un mensaje para elchat


this.playAudio();
this.newComment = notMessage;

this.emitNotification("add", 1);
if (this.user_type == "sherpa" || this.user_type == "creador") {
console.log(notMessage.id_equipo);
let tmpTeam = this.getTeam(notMessage.id_equipo);
console.log(tmpTeam);
if (tmpTeam != null) {
console.log(JSON.stringify(this.teams[tmpTeam]));
this.teams[tmpTeam].notificaciones++;
}
}


let tmpCon = this.getConversation(notMessage.data.subeje);
console.log(tmpCon);
if (tmpCon.array != null) {
//console.log(JSON.stringify(this.conversations[tmpCon]));
//this.conversations[tmpCon].notificaciones++;
tmpCon.array[tmpCon.index].notificaciones++;
tmpCon.array[tmpCon.index].lastComment = notMessage.data.comment;
}

if (this.selectedConversation != undefined && this.selectedConversation.subeje.id_actividad == notMessage.data.subeje) {
this.selectedConversation.notifications[notMessage.data.tipo]++;
}

}
}


Preparar el envío de un mensaje en tiempo real:


var dataMessage = {
subeje: this.id,
tipo: this.grupoTipo,
usuario_supervisar: this.usuario_supervisar.wikiname,
target: null,
conversation: {
subeje: { id: this.id },
origen: "private"
},
comment: objEnviar
};


var message = {
id: res["newMessage"], // id del mensaje a enviar
tipo: "newMessage", // Tipo de mensaje "NewMessage" para grupos, "newComment" para conversaciones privadas
user: this.user.usuario, // Wikiname del autor del mensaje
users: usuarios, //Usuarios a quien va dirigido (No aplica en grupos)
data: dataMessage, // Mensaje de texto a enviar
fecha: objEnviar["date"], // fecha actual
hora: objEnviar["hour"], // hora actual
origen: this.user.origen, // id de la empresa del autor
id_grupo: this.conversation["group"]["id"], // id de la conversación grupal
tipo_grupo: this.grupoTipo, // tipo de la conversación "entregable" | "actividad"
id_pap: this.conversation["group"]["id_pap"], // id de la tabla que liga el grupo con un pap_sub_eje (actividad del programa de transformación)
id_equipo: this.id_equipo, // id del equipo que está llevando a cabo el programa de transformación
usuario_supervisar: this.usuario_supervisar.wikiname, // Usuario a quien se le da seguimiento (En caso de ser una conversación de entregable individual)
app: "MasterSherpa" // app de donde proviene la notificación
}



Enviar el mensaje


this.chatService.send(message);