【问题标题】:Edit message in socket.io before it is sent to the user在将消息发送给用户之前在 socket.io 中编辑消息
【发布时间】:2015-03-03 14:07:54
【问题描述】:

我想截获从接收者的角度发送的某些消息。这意味着,例如:

发送方

  1. 用户发送消息
  2. 服务器收到消息
  3. 可以在此处处理消息但我不想在这里处理它

接收方

  1. 我想在发出之前在服务器中处理它
  2. 消息已发出
  3. 用户使用 socket.on('message',(...)); 接收消息(例如,在浏览器中)

有谁知道需要在 socket.io 中更改代码的哪一部分来实现这一点?我一直在搜索模块:适配器、客户端、解析器......但没有发现任何相关......对此有什么想法吗?我已经有点绝望了xp

【问题讨论】:

  • 你可以对socket.on()进行猴子补丁,这样你就可以看到每个.on()调用,从而在实际处理程序之前看到每个传入的消息。
  • @jfriend00 我也这么认为,但我在尝试查找代码块时仍然遇到问题。我会再试一次,我会告诉你的

标签: node.js socket.io


【解决方案1】:

服务器端:

socket.on("chat", clientMsg);

function clientMsg(data){
  console.log("data received:" + data)
  var msg = data; // set the msg to be sent
  // treat the data server side or
  // if wanting to treat the data from the user's side 
  // create another socket.on() inside this function. to treat it remotely (if needed)
  
  // then, when finished send the message to all users with the treated data.
  io.sockets.emit("chat", msg)

};

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 2021-11-09
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    相关资源
    最近更新 更多