简体   繁体   English

Socket.io socket.emit/socket.on 不工作

[英]Socket.io socket.emit/socket.on not working

I'm writing an app where i have multiple socket.io custom events, and all work fine, except this one:我正在编写一个应用程序,其中我有多个 socket.io 自定义事件,并且所有工作正常,除了这个:

socket.on("incomingImg", function(data) {
          var newimagesrc = data.image
          alert("new image");
         $("#messages").append(

                $("<img id='image' src='" + newimagesrc +"' >").text(
                     data.sender + ": [IMAGE]: " +
                    data.image           
                )

              );
         //$("#image").attr("src", newimagesrc);
        });

I've confirmed all the JQuery works, the only issue here is that on every client except the one I send this from, nothing happens.我已经确认了所有 JQuery 的工作原理,这里唯一的问题是,除了我发送这个的客户端之外,在每个客户端上都没有任何反应。 It may be worth noting that the client that initially sends the message is sending a different event;可能值得注意的是,最初发送消息的客户端正在发送不同的事件; my server code looks like this:我的服务器代码如下所示:

socket.on("sendImg", function(data) {
    var imgdata = data.imageurl;
    var sendername = data.uname;
    console.log(imgdata);
    console.log(sendername);
    socket.emit('incomingImg', { image: imgdata, sender: sendername 
    });
  });

for some reason, when IncomingImg is emitted, only the client that sent sendImg receives it.出于某种原因,当IncomingImg被发出时,只有发送sendImg的客户端会收到它。 Any help would be appreciated!任何帮助,将不胜感激!

So as it turns out, one of the subtle details that Pineapple Joe mentioned in his comment was the problem, although it was actually my error - there was a typo in the event name on the server.事实证明,Pineapple Joe 在他的评论中提到的一个微妙细节就是问题所在,尽管这实际上是我的错误 - 服务器上的事件名称有一个拼写错误。 Correcting that fixed the problem.纠正这个问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM