简体   繁体   中英

the first io.emit is never getting sent

io.on('connection',function(socket){
console.log('connected');
socket.on('disconnect',()=>{
    console.log('a user disconnected');
});
socket.on('sendMessage',function(data){
    const message = data.text;
    const cookieList = socket.handshake.headers.cookie;
    const parsedCookies = myModule.parseCookies(cookieList);
    if(parsedCookies.nickName!==undefined){
        socket.nickName = parsedCookies.nickName;
    }
    io.emit('showMessage',{
        'message':message,
        'nickName':socket.nickName
    });
});
});

this is my server code

'use strict';
 let socket = io();
 window.addEventListener('load',function(){
const button = document.querySelector('.btn');
button.addEventListener('click',function(event){
    const inputText = document.getElementById('msg').value;
    try{
        event.preventDefault();
        if(inputText.length<2){
            throw "some error";
        }else{
            const errorMessage = document.querySelector('.row p');
            if(errorMessage){
                errorMessage.remove();
            }
            socket.emit('sendMessage',{
                'text':inputText
            });
            showMessage();
        }
    }catch(error){
        const div  = document.querySelector('.row');
        console.log('error');
        if(!div.querySelector('p')){
            const errorP = document.createElement('p');
            errorP.setAttribute('style','color:red;');
            const errorMessage = document.createTextNode(error);
            errorP.appendChild(errorMessage);
            div.appendChild(errorP);
        }
    }
});
});
function showMessage(){
socket.on('showMessage',function(data){
    console.log("nickname :"+data.nickName + " message :"+data.message);
});
}

this is my client side code

Problem is the first message is never getting sent. Tried it both on chrome and firefox

From second messages communication works fine.. I cannot understand why the first message is not working

Well, that was a really simple mistake. The problem was that I placed socket.on('showMessage') inside in a function and ran the function in a try catch. Removed function and it solved the problem.

socket.on('showMessage',function(data) shouldn't be wrapped in a function or be called every time you send a message. Because of this, your socket never knows how to handle an incoming "showMessage" event until after it has already received one. Replace your client code with this and it should work:

'use strict';
let socket = io();
window.addEventListener('load', function () {
    const button = document.querySelector('.btn');


    socket.on('showMessage', function (data) {
        console.log("nickname :" + data.nickName + " message :" + data.message);
    });

    button.addEventListener('click', function (event) {
        const inputText = document.getElementById('msg').value;
        try {
            event.preventDefault();
            if (inputText.length < 2) {
                throw "some error";
            } else {
                const errorMessage = document.querySelector('.row p');
                if (errorMessage) {
                    errorMessage.remove();
                }
                socket.emit('sendMessage', {
                    'text': inputText
                });
            }
        } catch (error) {
            const div = document.querySelector('.row');
            console.log('error');
            if (!div.querySelector('p')) {
                const errorP = document.createElement('p');
                errorP.setAttribute('style', 'color:red;');
                const errorMessage = document.createTextNode(error);
                errorP.appendChild(errorMessage);
                div.appendChild(errorP);
            }
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM