【问题标题】:How to connect node server with vue socket如何使用 vue 套接字连接节点服务器
【发布时间】:2021-09-23 08:51:07
【问题描述】:

我正在使用 vue.js(Laravel) 连接我的聊天节点服务器,它的连接套接字但发出的功能不起作用。

      var socket = io.connect('http://localhost:3000/api/message');

        socket.on('connect', function () {

           var mapdata = {chatId: this.chatIdd, userId: this.userIdd}

           socket.emit("joinChat", JSON.stringify(mapdata) ,function(confirmation){
               alert('Work');

                      console.log(confirmation);
              });

        }.bind(this));
           socket.on('msgReceive', function () {


        }.bind(this));

alert('Work') 未显示。我该如何解决这个问题?

这是服务端函数

        socket.on("joinChat", async msg => {
            let objectValue = await JSON.parse(msg);
            chatId = objectValue["chatId"];
            socket.join(chatId);
        });

【问题讨论】:

  • 我用服务器端代码更新问题

标签: node.js vue.js websocket socket.io


【解决方案1】:

要使用(确认)确认,您必须触发该函数。

客户

socket.on('connect', function() {
  socket.emit("joinChat", {
    chatId: this.chatIdd,
    userId: this.userIdd
  }, function(confirmation) {
    //alert('Work');
    console.log(confirmation); // From server
  });
}.bind(this));

服务器

socket.on('joinChat', (msg, ack) => {
  if (msg.chatId) {
    socket.join(msg.chatId);
    if (typeof ack === 'function') ack('From server')
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 2019-12-10
    • 2018-10-11
    • 2021-01-28
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多