简体   繁体   中英

Unable to emit event when a checkbox is clicked. (socket.io)

I am using web sockets , where I want to perform an action when the checkbox in my form is clicked. But I am facing some issues ie the emit event is not working and the server is not responding.

Here is my code :

1.app.js

 io.on('connection',(socket)=>{
    socket.on("approve",(s,callback)=>{
        console.log(s);
        callback();
    })
})

2. index.ejs

<input class="approvedStatus" name="approve" type="checkbox"> //this is the checkbox



<script type="text/javascript">
var socket = io();
    $(".approvedStatus").on("click",function(){
        socket.emit("approve",{
            status: 2
        },function(){
            console.log("Data recieved by the server");
        })
    })
</script>

Make sure socket.io is inserted into index.ejs :

<script src="/socket.io/socket.io.js"></script>

And socket.io server started in app.js :

io = ws.listen(server);

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