Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require("axios");
- const socketio = require("socket.io");
- let users = {};
- exports.setupWebsocket = server => {
- const io = socketio(server);
- console.log("Iniciando webSocket...");
- io.on("connection", socket => {
- console.log("Recebendo conexão no socket");
- const { id } = socket;
- const { channel } = socket.handshake.query;
- console.log(id, channel);
- const timestamp = Date.now();
- if (!users[channel]) {
- users[channel] = [];
- }
- users[channel].push(id);
- socket.on("ducity", async function(obj) {
- const { channel, content, user } = obj;
- console.log( obj );
- axios.post('http://localhost:3000/webservices/chats/addMessage', {
- channel: obj.channel,
- content: obj.content,
- user: obj.user,
- timestamp: timestamp
- }, {
- headers: {
- api_secret: 'duc17y@2020#r1ch4rd__'
- }
- })
- .then((res) => {
- console.log(`statusCode: ${res.statusCode}`)
- })
- .catch((error) => {
- console.error(error)
- });
- if (!!users[channel]) {
- if (users[channel].length > 0) {
- users[channel].forEach(socketId => {
- io.to(`${socketId}`).emit("ducity", {
- content,
- user,
- timestamp
- });
- });
- }
- }
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement