[英]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. 我正在使用Web套接字 ,单击表单中的复选框时要在其中执行操作。 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 1.app.js
io.on('connection',(socket)=>{
socket.on("approve",(s,callback)=>{
console.log(s);
callback();
})
})
2. index.ejs 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
: 确保将socket.io插入到index.ejs
:
<script src="/socket.io/socket.io.js"></script>
And socket.io server started in app.js
: 然后在app.js
启动socket.io服务器:
io = ws.listen(server);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.