【问题标题】:Alertifyjs alerts shown to the person who triggered it向触发它的人显示 Alertifyjs 警报
【发布时间】:2015-02-09 17:58:55
【问题描述】:

作为免责声明,我是 nodejs 和一般表达框架的新手,所以在我仍在努力学习的同时请多多包涵。
我正在使用alertifyjs library 向用户显示各种警报的通知。现在,问题是通知显示给网站上的每个人。我明白为什么会发生这种情况,这是有道理的。我该如何做才能使特定警报仅显示给触发它的人?我到底用什么来实现这一点?饼干? 感谢您的帮助,如果您需要更多信息,请告诉我。

这是一个代码示例...

//client code
socket.on('word length', function(data) {
  alertify.error('Check word length!');
});


//server code
if (word.length > 50 || word.length <= 1) { // check word length:
  io.sockets.emit('word length', word);
}

【问题讨论】:

  • 请出示您的代码。例如,您的用户将如何触发它?
  • 添加了我如何实现这一点的代码示例。我正在使用套接字来触发警报通知。

标签: javascript node.js cookies express alertifyjs


【解决方案1】:

这与alertify 无关,而与socket.io 的工作方式有关。 io.socket.emit 调用将向所有 个连接的套接字广播一条消息。

您只想在客户端套接字上调用emit

io.on('connection', function (socket) {
    io.socket.emit('message', 'to everyone');
    socket.emit('message', 'to this client only');
});

【讨论】:

  • 哇,这太容易了。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多