试图为整个套接字连接以及所有连接到该套接字的客户端发出事件。
io.sockets.emit('toALL',{msg: 'this is demo'});
并使用以下代码行在index.html
中进行处理:
socket.on("toALL",function(){
// If console is putted here still not able to get any msg or response from emit.
});
答案 0 :(得分:0)
您发送到套接字的对象可用作let isValidPassword = function (password) {
if (password.length <= 8 && password.includes('password')) {
console.log('Password is not according to Company Policy.');
} else {
console.log('Password is accepatble!');
}
}
isValidPassword();
的回调函数的参数。您可以使用.on
广播给所有客户端。
io.emit
// server
io.emit("toALL", {msg: "this is demo"});
答案 1 :(得分:0)
我从以下位置获得了解决方案:https://socket.io/get-started/chat#Integrating-Socket-IO
对于 server.js:
io.on('connection', function(socket){
socket.on('toALL', function(msg){
io.emit('toALL', msg);
});
});
对于Client.html
socket.emit('toALL', 'event');
socket.on('toALL', function(msg){
alert(msg);
});